我想为文本字段提供一个初始值,并重新绘制一个空值以清除文本。用Flutter的api实现这一点的最佳方法是什么?


当前回答

由于没有一个答案提到它,TextEditingController应该在使用后被丢弃。如:

class MyWidget extends StatefulWidget {
  const MyWidget({Key? key}) : super(key: key);

  @override
  MyWidgetState createState() => MyWidgetState();
}

class MyWidgetState extends State<MyWidget> {
  final myController = TextEditingController(text: "Initial value");

  @override
  Widget build(BuildContext context) {
    return TextField(
      controller: myController,
    );
  }

  @override
  void dispose() {
    // dispose it here
    myController.dispose();
    super.dispose();
  }
}

其他回答

当你使用TextEditingController

如果使用TextEditingController,请将其文本字段设置为所需的值

TextEditingController txtController = TextEditingController()..text = 'Your initial text value'; 
TextField( controller: txtController ..... ) 

当你不使用TextEditingController

如果你没有使用texteditingcontroller,直接从TextField小部件使用initialValue字段:

TextFormField( initialValue: "Your initial text value" )
class _YourClassState extends State<YourClass> {
  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...'),
      ),
    );
  }
}

(来自邮件列表。这个答案不是我想出来的。)

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

由于没有一个答案提到它,TextEditingController应该在使用后被丢弃。如:

class MyWidget extends StatefulWidget {
  const MyWidget({Key? key}) : super(key: key);

  @override
  MyWidgetState createState() => MyWidgetState();
}

class MyWidgetState extends State<MyWidget> {
  final myController = TextEditingController(text: "Initial value");

  @override
  Widget build(BuildContext context) {
    return TextField(
      controller: myController,
    );
  }

  @override
  void dispose() {
    // dispose it here
    myController.dispose();
    super.dispose();
  }
}

简单高效的方法

分配控制器到你的TextFormField或TextField和在initState你可以初始化它像这样的初始值

_controller = TextEditingController(text: 'Your initial value');