我需要调用将我的title
和oldtitle
参数传递给我的EditPage
有状态小部件。但如果我这样做;
class EditPage extends StatefulWidget {
String title;
String oldtitle;
EditPage({this.title, this.oldtitle})
除非我将它们称为widget.title
and ,否则这些字符串不可用于构建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})
我无法从另一个屏幕将 title 参数传递给它。IE:
`EditPage(title:mytitle, oldtitle:myoldtitle);`
那么将参数传递给有状态小部件的正确方法是什么?
看起来解决方案是将标题与旧标题分开;
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;
});
},
),
),
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句