有什么办法可以捕获文本字段中的按键吗?就我而言,当用户在文本字段内按回车键时,这些值将被存储。为此,我需要像 Kotlin+Android 中那样使用 Keypress-event。我这周才开始尝试 flutter,因为它很有趣而且是跨平台的。
RawKeyboardListener(
child: TextFormField(
keyboardType: TextInputType.text,
decoration: new InputDecoration(labelText: "Phone"),
validator: (val) => val.length == 0 ? 'Enter your phone' : null,
onSaved: (val) => this.phone = val,
),
focusNode: FocusNode(),
onKey: (RawKeyEvent event) {
print(event.data.logicalKey.keyId);
if (event.runtimeType == RawKeyDownEvent ) {
print("asdadda");
}
},
),
但不知道为什么我按了键却不起作用。
As Alok
建议,我调查了onSubmitted
方法。我使用TextfromField,所以我选择onFieldSubmitted
以我为例的方法。我还添加了 RawKeyBoardListener,用于从移动扫描仪设备按下物理 Enter 键。代码是 -
RawKeyboardListener(//for physical keyboard press
child: TextFormField(
keyboardType: TextInputType.text,
decoration: new InputDecoration(labelText: "Phone"),
validator: (val) => val.length == 0 ? 'Enter your phone' : null,
onSaved: (val) => this.phone = val,
onFieldSubmitted: (_) async {
print("asdadda");
submitContact();
},
),
focusNode: FocusNode(),
onKey: (RawKeyEvent event) {
print(event.data.logicalKey.keyId);
if (event.runtimeType == RawKeyDownEvent &&
(event.logicalKey.keyId == 4295426088))//Enter Key ID from keyboard
{
print("asdadda");
submitContact();
}
},
),
欢迎编辑^_^
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)