更改颤动中的文本输入值

时间:2020-02-13 15:05:19

标签: flutter

我执行以下操作来更改输入字段的值。关于文档,这应该可以,但是不能。目的是在点击位置输入后打开Goog​​le位置搜索,并将结果值写回文本字段。以下代码是显示我的问题的简单版本:

            TextFormField(
             key: locationKey,
                decoration:
                InputDecoration(labelText: 'Location'),
             // onTap: openLocationSearch,
              onTap: () {
                locationKey.currentState.didChange('test');
             },
          ),

2 个答案:

答案 0 :(得分:1)

这将满足您的需求:

class TapTextFormIssue extends StatefulWidget {
  @override
  _TapTextFormIssueState createState() => _TapTextFormIssueState();
}

class _TapTextFormIssueState extends State<TapTextFormIssue> {
  TextEditingController _textEditingController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Center(
      child: TextFormField(
        controller: _textEditingController,
        onTap: () {
          setState(() {
            _textEditingController.text = 'test';
          });
        },
      ),
    );
  }
}

答案 1 :(得分:1)

像这样向您的TextEditingController添加TextFormField

TextEditingController myTEC = TextEditingController('Location');
TextFormField(
  key: locationKey,
  controller: myTEC,
  onTap: () {
    locationKey.currentState.didChange('test');
  },
),