[英]How to trigger onSubmitted on a Flutter for web TextField
I have a small flutter app for the web and am showing a TextField.我有一个用于 web 的小型 flutter 应用程序,并且正在显示一个 TextField。
I now need a callback like onSubmitted whenever the user either leaves the TextField (focus loss) or presses the Enter key.现在,每当用户离开 TextField(失去焦点)或按下 Enter 键时,我都需要一个类似 onSubmitted 的回调。 Right now I can't make any callback happen at all.
现在我根本无法进行任何回调。
TextField(
decoration: InputDecoration(
labelText: 'Name',
),
controller: TextEditingController(text: event.name),
onEditingComplete: () { print("editing complete"); },
onSubmitted: (String value) { print("submitted\n"); },
maxLines: 1,
),
This works for me on Flutter Web这适用于 Flutter Web
TextField(
controller: _passwordController,
decoration: InputDecoration(
labelText: 'Password',
),
obscureText: true,
onSubmitted: (value) {
_loginPresenter.login(
_usernameController.text, _passwordController.text);
},
),
Note the definition of onSubmitted
.注意
onSubmitted
的定义。 When the user presses the Enter key on flutter web, the loginPresenter.login
method will be called.当用户在 flutter web 上按 Enter 键时,将调用
loginPresenter.login
方法。
It seems to be an issue: [web]: TextField onSubmitted is not triggered when pressing enter这似乎是一个问题: [web]: TextField onSubmitted is not triggered when press enter
This is a workaround mentioned in the link:这是链接中提到的解决方法:
body: RawKeyboardListener(
focusNode: focusNode,
onKey: (event) {
if (event is RawKeyUpEvent && event.data is RawKeyEventDataAndroid) {
var data = event.data as RawKeyEventDataAndroid;
if (data.keyCode == 13) {
debugPrint('onSubmitted');
}
}
},
child: TextField(),
),
Also removing the maxLines: 1
seems to get the onsubmitted to work.[Faced the same issue on mobile]还删除了
maxLines: 1
似乎让 onsubmitted 工作。[在移动设备上面临同样的问题]
You can try this create a methode where you make a condition on your TextEditingController and based on that you can submite on your TextField.您可以尝试创建一个方法,在其中对 TextEditingController 设置条件,并在此基础上在 TextField 上提交。
final entredTitle = titleController.text;
final entredAmount = double.parse(amountController.text);
if (entredTitle.isEmpty || entredAmount <= 0 ){
return ;
}
widget.addTx(entredTitle,entredAmount);
Navigator.of(context).pop();
}
TextField(
decoration: InputDecoration(labelText: 'Amount'),
controller: amountController,
onSubmitted: (_) => submitData(),
),
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.