简体   繁体   English

Flutter:onEdittingComplete 和 onSubmitted 之间的 TextField 差异

[英]Flutter: TextField difference between onEdittingComplete and onSubmitted

I am trying to figure out the difference between onEdittingComplete and onSubmitted , I don't know when the latter should be used since the former can be used to switch focus or to submit the content of the form.我想弄清楚onEdittingCompleteonSubmitted之间的区别,我不知道什么时候应该使用后者,因为前者可用于切换焦点或提交表单内容。

I tried looking into the documentation but there is not much said about the onSubmitted property.我尝试查看文档,但对onSubmitted属性没有太多说明。

onSubmitted提交时

As the name suggestions, it's called when user finishes editing, eg press "done" or "send" on the keyboard.顾名思义,它在用户完成编辑时调用,例如按键盘上的“完成”或“发送”。 The callback conveniently passes the value to you, so you can do your business logic with it.回调方便地将值传递给您,因此您可以使用它来执行您的业务逻辑。 At the same time, since Flutter assumes the user is "done", it will hide the on-screen keyboard.同时,由于 Flutter 假设用户已经“完成”,它会隐藏屏幕键盘。

onEditingComplete编辑完成

This is more of an "FYI" that tells you that the user has finished editing.这更像是一个“仅供参考”,告诉您用户已完成编辑。 It is fired before onSubmitted .onSubmitted之前被触发。 It does not pass you the value (while you can technically get the value using a controller, that's not the intention here), because you can still handle value-related business logic in onSubmitted .它不会将值传递给您(虽然您可以使用控制器从技术上获取值,但这不是这里的意图),因为您仍然可以在onSubmitted处理与值相关的业务逻辑。 Both events will fire anyway.无论如何,这两个事件都会触发。

The real purpose behind onEditingComplete is that, in the default implementation, Flutter hides the on-screen keyboard when the keyboard action is considered a "completion" action, such as "done", "go", "send", or "search", but does not hide the keyboard if the action is "non-completion", such as "next" or "previous". onEditingComplete背后的真正目的是,在默认实现中,当键盘操作被认为是“完成”操作时,例如“完成”、“去”、“发送”或“搜索”,Flutter 会隐藏屏幕键盘,但如果操作是“未完成”,例如“下一个”或“上一个”,则不会隐藏键盘。 (The keyboard action can be modified via textInputAction property of the TextField widget.) (可以通过TextField小部件的textInputAction属性修改键盘操作。)

If you don't like this behaviour, you should modify it.如果你不喜欢这种行为,你应该修改它。 For example, "send" is considered a "completion action" here, thus in an Instant Messaging (chatting) app, each time user sends a short message, the keyboard will be collapsed, that's not good.例如,“发送”在这里被认为是“完成动作”,因此在即时消息(聊天)应用程序中,用户每次发送短消息时,键盘都会折叠,这是不好的。 But if we override the onEditingComplete callback to an empty function, it will stop the default behaviour and not hide the keyboard.但是如果我们将onEditingComplete回调覆盖为一个空函数,它将停止默认行为并且不会隐藏键盘。 For example:例如:

TextField(
  controller: _controller,
  onSubmitted: (text) {
    sendMessage(text);
    _controller.clear();
  },
  onEditingComplete: () {}, // do not hide keyboard
  textInputAction: TextInputAction.send,
)

Demo:演示:

演示 gif

onSubmitted:提交时:

final ValueChanged<String> onSubmitted

It returns the TextField entered value in onSubmitted callback, most of the time its used for next/previous field button of keyboard when using TextInputAction.next and TextInputAction.previous for textInputAction performed.它在onSubmitted回调中返回TextField输入的值,大多数情况下,当使用TextInputAction.nextTextInputAction.previous执行textInputAction时,它用于键盘的下一个/上一个字段按钮。

onEditingComplete:在编辑完成:

final VoidCallback onEditingComplete

It's similar to onSubmitted but does not return value inside the callback, instead, it updates the text controller and then we can fetch a value from the controller where ever required.它与onSubmitted类似,但不会在回调中返回值,而是更新文本controller ,然后我们可以在需要时从controller中获取值。

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

相关问题 Flutter textField 小部件中的 onSubmitted 和 onEditingComplete 有什么区别? - What is the difference between onSubmitted and onEditingComplete in Flutter textField widget? 为什么导航器在颤动中不能在 TextField 的 onSubmitted 中工作? - Why navigator not working in onSubmitted of TextField in flutter? 如何在 web TextField 的 Flutter 上触发 onSubmitted - How to trigger onSubmitted on a Flutter for web TextField Flutter:如何在不丢失焦点的情况下关闭 TextField 的 onSubmitted() 键盘 - Flutter: How to close keyboard on onSubmitted() for TextField without lost focus 使用Flutter,如何在iOS上触发TextField的onSubmitted()? - Using Flutter, how can I trigger onSubmitted() of TextField on iOS? 为什么我们在 TextField() 中使用“onSubmitted:”语法? - In TextField( ) why we use " onSubmitted : " syntax? 如何将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? TextFormField 和 TextField 有什么区别? - What is the difference between TextFormField and TextField? 如何使用 Flutter 中的 onSubmitted 导航到另一个小部件 - How can I navigate to another widget by using onSubmitted in Flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM