If you want to supply an initial value to a text field? Use the below code to supply an initial value to a text field.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | class _FooState extends State<Foo> { TextEditingController _controller; @override void initState() { super.initState(); _controller = new TextEditingController(text: 'Initial value'); } @override Widget build(BuildContext context) { return new Column( children: <Widget>[ 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'), ), ], ); } } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.