I have a small flutter app for the web and am showing a TextField.
I now need a callback like onSubmitted whenever the user either leaves the TextField (focus loss) or presses the Enter key. 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
TextField(
controller: _passwordController,
decoration: InputDecoration(
labelText: 'Password',
),
obscureText: true,
onSubmitted: (value) {
_loginPresenter.login(
_usernameController.text, _passwordController.text);
},
),
Note the definition of onSubmitted
. When the user presses the Enter key on flutter web, the loginPresenter.login
method will be called.
It seems to be an issue: [web]: TextField onSubmitted is not triggered when pressing 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(),
),
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With