我是flutter的新手。我想创建自定义的textfield,它会自动添加一个textcontroller。但是失败了,代码是:
class MyInput extends StatelessWidget {
MyInput(this.onChange) {
editingController = new TextEditingController();
editingController.addListener(this.onChange);
}
Function() onChange;
TextEditingController editingController;
@override
Widget build(BuildContext context) {
return TextField(controller: editingController);
}
}
所以我像这样使用MyInput:
body: new Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Text(
'You have pushed the button this many times:',
),
new Text(
'$_counter',
style: Theme.of(context).textTheme.display1,
),
TextField(controller: editingController),
MyInput(_incrementCounter)
],
),
),
但是小部件失败,我无法将任何事件输入文本字段! 我想知道小部件是否应该是有状态的,但是我尝试过,同样。