I\'d like to supply an initial value to a text field and redraw it with an empty value to clear the text. What\'s the best approach to do that with Flutter\'s APIs?
(From the mailing list. I didn't come up with this answer.)
class _FooState extends State {
TextEditingController _controller;
@override
void initState() {
super.initState();
_controller = new TextEditingController(text: 'Initial value');
}
@override
Widget build(BuildContext context) {
return new Column(
children: [
new TextField(
// The TextField is first built, the controller has some initial text,
// which the TextField shows. As the user edits, the text property of
// the controller is updated.
controller: _controller,
),
new RaisedButton(
onPressed: () {
// You can also use the controller to manipuate what is shown in the
// text field. For example, the clear() method removes all the text
// from the text field.
_controller.clear();
},
child: new Text('CLEAR'),
),
],
);
}
}