How do I supply an initial value to a text field?

前端 未结 10 874
南笙
南笙 2021-01-30 12:15

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?

10条回答
  •  一生所求
    2021-01-30 12:31

    If you want to handle multiple TextInputs as asked by @MRT in the comment to the accepted answer, you can create a function that takes an initial value and returns a TextEditingController like this:

    initialValue(val) {
      return TextEditingController(text: val);
    }
    

    Then, set this function as the controller for the TextInput and supply its initial value there like this:

    controller: initialValue('Some initial value here....')
    

    You can repeat this for the other TextInputs.

提交回复
热议问题