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


当前回答

如果你使用TextEditingController只需要在你的类中使用这一行。

TextEditingController txtController = TextEditingController(text: 'Initial value')

TextField(
  controller: txtController,
);

如果你不使用TextEditingController就去initialValue

TextFormField(
      initialValue: 'your initial text',
  );

完整代码

class _TestScreen extends State<Test> {
  TextEditingController _controller;

  @override
  void initState() {
    super.initState();
    _controller = new TextEditingController(text: 'Initial value');
  }

  @override
  Widget build(BuildContext context) {
    return new Column(
      children: <Widget>[
       TextField(       // First method
          initialValue: "Initial text"
        ),
       TextField(       // Second method
          controller: _controller, // Controller has the initial value
        ),
        
      ],
    );
  }
}

其他回答

这可以通过使用TextEditingController来实现。

要有一个初始值,你可以加上

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

or

如果你正在使用TextFormField,你有一个initialValue属性。它基本上自动地将这个initialValue提供给小部件。

TextFormField(
  initialValue: 'initial value'
)

清除文本可以使用 _controller.clear()方法。

你可以做以上所有的事情,但如果你想让API在加载时显示你的数据,它会像配置文件页面一样显示。下面是代码:

TextEditingController _nameController = TextEditingController(); // initialize the controller
 // when API gets the data, do this:
 _nameController.text = response.data.fullName; or _nameController.text = "Apoorv Pandey"

我希望这能澄清一切。编码快乐!

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

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

你可以使用TextFormField代替TextField,并使用initialValue属性。例如

TextFormField(initialValue: "I am smart")

至于上述答案都是正确的,但有一件事是缺失的,我想添加到它是如何自定义TextFormField的默认文本的风格。

TextEditingController textController = TextEditingController(text: '4');

 TextFormField(
    controller: textController,
     // This style property will customize the default controller text
    style: TextStyle(
    color: Colors.white,
    fontSize: 18,
    fontWeight: FontWeight.bold,
   ),
    ),

对于这种情况,我更喜欢使用TextEditingController而不是initialValue,因为如果你想改变它以后或修改它,那么textController将不断监听你的TextFormField输入的变化。 这是我个人的观点,如果我错了请告诉我。