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

前端 未结 10 962
南笙
南笙 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:32

    class _YourClassState extends State {
      TextEditingController _controller = TextEditingController();
    
      @override
      void initState() {
        super.initState();
        _controller.text = 'Your message';
      }
    
      @override
      Widget build(BuildContext context) {
        return Container(
          color: Colors.white,
          child: TextFormField(
            controller: _controller,
            decoration: InputDecoration(labelText: 'Send message...'),
          ),
        );
      }
    }
    

提交回复
热议问题