Flutter/Dart:将参数传递给有状态的小部件?
Flutter/Dart: Pass Parameters to a Stateful Widget?
我需要将我的 title
和 oldtitle
参数传递给我的 EditPage
有状态小部件。但如果我这样做;
class EditPage extends StatefulWidget {
String title;
String oldtitle;
EditPage({this.title, this.oldtitle})
除非我将它们称为 widget.title
和 widget.oldtitle
.
,否则这些字符串不可用于构建
但我在表单中使用 textfield
如果我使用这些小部件,它似乎无法正常工作。
表单代码如下:
Container(
child: TextField(
decoration: new InputDecoration(
hintText: widget.oldtitle,
contentPadding: new EdgeInsets.all(1.0),
border: InputBorder.none,
filled: true,
fillColor: Colors.grey[300],
),
keyboardType: TextInputType.text,
autocorrect: false,
onChanged: (titleText) {
setState(() {
widget.title= titleText;
});
},
),
),
但是如果我这样做的话;
class _EditPageState extends State<EditPage> {
String title;
String oldtitle;
EditPage({this.title, this.oldtitle})
我无法从另一个屏幕向它传递标题参数。即:
`EditPage(title:mytitle, oldtitle:myoldtitle);`
那么将参数传递给有状态小部件的正确方法是什么?
切勿将变量直接传递给状态,因为它不能保证在更新状态时会重建小部件。您应该通过有状态小部件接受参数,并通过 widget.variable
.
从状态本身访问它们
示例:
class TestWidget extends StatefulWidget {
final String variable;
TestWidget({Key key, @required this.variable}) : super(key: key);
@override
_TestWidgetState createState() => _TestWidgetState();
}
class _TestWidgetState extends State<TestWidget> {
@override
Widget build(BuildContext context) {
return Container(
child: Center(
// Accessing the variables passed into the StatefulWidget.
child: Text(widget.variable),
),
);
}
}
看起来解决方案是将标题与旧标题分开;
class EditPage extends StatefulWidget {
String oldtitle;
EditPage({this.oldtitle})
然后;
class _EditPageState extends State<EditPage> {
String title;
所以现在是表格;
Container(
child: TextField(
decoration: new InputDecoration(
hintText: widget.oldtitle,
contentPadding: new EdgeInsets.all(1.0),
border: InputBorder.none,
filled: true,
fillColor: Colors.grey[300],
),
keyboardType: TextInputType.text,
autocorrect: false,
onChanged: (titleText) {
setState(() {
title= titleText;
});
},
),
),
我需要将我的 title
和 oldtitle
参数传递给我的 EditPage
有状态小部件。但如果我这样做;
class EditPage extends StatefulWidget {
String title;
String oldtitle;
EditPage({this.title, this.oldtitle})
除非我将它们称为 widget.title
和 widget.oldtitle
.
但我在表单中使用 textfield
如果我使用这些小部件,它似乎无法正常工作。
表单代码如下:
Container(
child: TextField(
decoration: new InputDecoration(
hintText: widget.oldtitle,
contentPadding: new EdgeInsets.all(1.0),
border: InputBorder.none,
filled: true,
fillColor: Colors.grey[300],
),
keyboardType: TextInputType.text,
autocorrect: false,
onChanged: (titleText) {
setState(() {
widget.title= titleText;
});
},
),
),
但是如果我这样做的话;
class _EditPageState extends State<EditPage> {
String title;
String oldtitle;
EditPage({this.title, this.oldtitle})
我无法从另一个屏幕向它传递标题参数。即:
`EditPage(title:mytitle, oldtitle:myoldtitle);`
那么将参数传递给有状态小部件的正确方法是什么?
切勿将变量直接传递给状态,因为它不能保证在更新状态时会重建小部件。您应该通过有状态小部件接受参数,并通过 widget.variable
.
示例:
class TestWidget extends StatefulWidget {
final String variable;
TestWidget({Key key, @required this.variable}) : super(key: key);
@override
_TestWidgetState createState() => _TestWidgetState();
}
class _TestWidgetState extends State<TestWidget> {
@override
Widget build(BuildContext context) {
return Container(
child: Center(
// Accessing the variables passed into the StatefulWidget.
child: Text(widget.variable),
),
);
}
}
看起来解决方案是将标题与旧标题分开;
class EditPage extends StatefulWidget {
String oldtitle;
EditPage({this.oldtitle})
然后;
class _EditPageState extends State<EditPage> {
String title;
所以现在是表格;
Container(
child: TextField(
decoration: new InputDecoration(
hintText: widget.oldtitle,
contentPadding: new EdgeInsets.all(1.0),
border: InputBorder.none,
filled: true,
fillColor: Colors.grey[300],
),
keyboardType: TextInputType.text,
autocorrect: false,
onChanged: (titleText) {
setState(() {
title= titleText;
});
},
),
),