简体   繁体   English

如何在 web TextField 的 Flutter 上触发 onSubmitted

[英]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 上提交。

  • First第一的
   final entredTitle = titleController.text;
   final entredAmount = double.parse(amountController.text);
   if (entredTitle.isEmpty || entredAmount <= 0 ){
     return ;
   }
   widget.addTx(entredTitle,entredAmount);
   Navigator.of(context).pop();
 } 
  • Second第二
 TextField(
                      decoration:  InputDecoration(labelText: 'Amount'),
                      controller: amountController,
                      onSubmitted: (_) => submitData(),
                    ),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Flutter,如何在iOS上触发TextField的onSubmitted()? - Using Flutter, how can I trigger onSubmitted() of TextField on iOS? Flutter:如何在不丢失焦点的情况下关闭 TextField 的 onSubmitted() 键盘 - Flutter: How to close keyboard on onSubmitted() for TextField without lost focus 为什么导航器在颤动中不能在 TextField 的 onSubmitted 中工作? - Why navigator not working in onSubmitted of TextField in flutter? Flutter:onEdittingComplete 和 onSubmitted 之间的 TextField 差异 - Flutter: TextField difference between onEdittingComplete and onSubmitted Flutter textField 小部件中的 onSubmitted 和 onEditingComplete 有什么区别? - What is the difference between onSubmitted and onEditingComplete in Flutter textField widget? 如何将flutter textFormField onChange和onSubmitted作为参数传递? - How to pass flutter textFormField onChange and onSubmitted as a parameter? 如何在提交时将文本字段更改为另一个小部件? - How can I change a Textfield to another widget onSubmitted? 如何获得颤振的文本字段触发器? - How to get flutter's textfield trigger? 如何使用 Flutter 中的 onSubmitted 导航到另一个小部件 - How can I navigate to another widget by using onSubmitted in Flutter 为什么我们在 TextField() 中使用“onSubmitted:”语法? - In TextField( ) why we use " onSubmitted : " syntax?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM