在 Flutter TextFormField 中,我想在编辑完全完成后调用一个方法,我不想在用户更改文本表单字段中的文本时调用该方法(通过使用侦听器或通过 onchanged 函数),通过使用 OnEditingComplete 函数我可以实现这个要求,但问题是只有在点击键盘中的完成按钮时才会调用 oneditingcomplete,当从一个文本字段编辑到另一个文本字段后更改焦点时 onEditingComplete 函数不起作用,那么最好的方法是什么一旦用户完全完成编辑,则检测。
`TextField(
onEditingComplete: () {//this is called only when done or ok is button is tapped in keyboard
print('test');
},
Run Code Online (Sandbox Code Playgroud)
使用 FocusScopeWidget 帮助我解决这个问题https://api.flutter.dev/flutter/widgets/FocusNode-class.html,
当用户按下键盘上的完成按钮时,将调用 onsubmitted 函数,并在用户更改焦点时使用焦点范围小部件。
FocusScope(
onFocusChange: (value) {
if (!value) {
//here checkAndUpdate();
}
},
child: TextFormField(
key: Key('productSet${DateTime.now().toIso8601String()}'),
getImmediateSuggestions: true,
textFieldConfiguration: TextFieldConfiguration(
onSubmitted: (cal) {
//here checkAndUpdate();
},
Run Code Online (Sandbox Code Playgroud)
您可以使用TextFormField代替TextField:
TextFormField(
decoration: InputDecoration(labelText: 'First Name'),
onSaved: (val) => setState(() => _firstName = val),
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2720 次 |
| 最近记录: |