Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trigger onSubmitted on a Flutter for web TextField

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,
),
like image 727
Fabian Avatar asked Oct 10 '19 10:10

Fabian


2 Answers

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.

like image 126
dazza5000 Avatar answered Oct 25 '22 00:10

dazza5000


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(),
    ),
like image 35
Pablo Barrera Avatar answered Oct 25 '22 00:10

Pablo Barrera