[英]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.我想弄清楚
onEdittingComplete
和onSubmitted
之间的区别,我不知道什么时候应该使用后者,因为前者可用于切换焦点或提交表单内容。
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:演示:
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.next
和TextInputAction.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.