class _FooState extends State<Foo> { TextEditingController _controller; @override void initState() { super.initState();
//setState很重要不然数据不会更新!!!
setState((){
_controller = new TextEditingController(text: '初始值');
}
); } @override Widget build(BuildContext context) { return new Column( children: <Widget>[ new TextField( //设置controller, controller: _controller, ), new RaisedButton( onPressed: () { //清除输入 _controller.clear(); }, child: new Text('清除'), ), ], ); } }
注意在初始化的时候,setState一定要调用,不然数据不会正常更新!