16
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Last updated at Posted at 2019-05-01

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?

16
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?