我执行以下操作来更改输入字段的值。关于文档,这应该可以,但是不能。目的是在点击位置输入后打开Google位置搜索,并将结果值写回文本字段。以下代码是显示我的问题的简单版本:
TextFormField(
key: locationKey,
decoration:
InputDecoration(labelText: 'Location'),
// onTap: openLocationSearch,
onTap: () {
locationKey.currentState.didChange('test');
},
),
答案 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');
},
),