How to specify the original value in the text box?

I would like to specify an initial value in a text box and redraw it with an empty value to clear the text. What is the best approach to use with Flutter APIs?

+50
source share
7 answers

(From the mailing list. I did not come up with this answer.)

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'),
        ),
      ],
    );
  }
}
+52
source

You can use TextFormFieldinstead TextFieldand use the property initialValue. eg

TextFormField(initialValue: "I am smart")
+24
source

TextEditingController, ,

TextEditingController _controller = new TextEditingController();


_controller.text = 'your initial text';

final your_text_name = TextFormField(
      autofocus: false,
      controller: _controller,
      decoration: InputDecoration(
        hintText: 'Hint Value',
      ),
    );

- TextEditingController, initialValue,

final last_name = TextFormField(
      autofocus: false,
      initialValue: 'your initial text',
      decoration: InputDecoration(
        hintText: 'Last Name',
      ),
    );

TextEditingController

+14

TextInput @MRT , , TextEditingController :

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

TextInput TextInput :

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

TextInput s.

+5

,

  final usernameController = TextEditingController(text: 'bhanuka');

,

   child: new TextField(
        controller: usernameController,
    ...
)
0

, :

TextField(
  controller: TextEditingController()..text = 'Your initial value',
  onChanged: (text) => {},
)
0

If you did not find the answer for this and for those who came here to find the answer: Check out the InputDecorationhintText field :

new TextField(
  decoration: new InputDecoration(
    hintText:"My Text String."
  ),
...
-12
source

Source: https://habr.com/ru/post/1674024/


All Articles