9
Help us understand the problem. What are the problem?

More than 3 years have passed since last update.

posted at

updated at

FlutterでTextFieldに初期値を設定する方法

TextEditingControllerで設定する模様。

class Example extends StatefulWidget {
  @override
  _ExampleState createState() => _ExampleState();
}

class _ExampleState extends State<Example> {
  String _text = '';
  TextEditingController _textEditingController;

  @override
  void initState() {
    super.initState();
    _textEditingController = new TextEditingController(text: ''); // <- こんな感じ
  }

  @override
  Widget build(BuildContext context) {
    return TextField(
      controller: _textEditingController,
      onChanged: (value) {
        setState(() {
          _text = value;
        });
      },
    );
  }

  @override
  void dispose() {
    _textEditingController.dispose();
    super.dispose();
  }
}

  
TextFormFieldでは初期値設定ができるようだけど、onTapとかないのでTextFieldと同じように使うにはGestureDetectorとか使わないといけないかもしれない。
  

TextFormField(initialValue: 'hogehoge')

参考

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

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Sign upLogin
9
Help us understand the problem. What are the problem?