Flutter:如何检测键盘按下Enter键? [英] Flutter: How to detect keyboard press Enter?
问题描述
TextEditingController.addListener()
为文本更改时注册一个回调。这似乎也可以通过按Enter键,打开/关闭键盘(改变焦点)来触发。
TextEditingController.addListener()
registers a callback for when the text changes. This seems to also be triggered by pressing the Enter button, opening/closing the keyboard (changing focus).
我如何注册被按下的输入,因此例如,我可以调用API吗?
How do i register that it was the enter that was pressed so i can, for example, invoke an API?
我目前的实现方式是通过监听 FocusNode
检测焦点变化。当按Enter键时,焦点将从 FocusNode
中移出,并且键盘关闭。这是正确的方法吗?
The current way i achieve this is by listening on the FocusNode
to detect focus change. Focus is removed from the FocusNode
when pressing enter, and the keyboard closes. Is this the right way to do it?
推荐答案
有一个 onSubmitted
属性,该属性接受 ValueChanged
回调。用户在编辑后按完操作,就会触发 onSubmitted
。
There is a onSubmitted
property which accepts a ValueChanged
callback. onSubmitted
is triggered once the user press done after editing.
希望有帮助!
这篇关于Flutter:如何检测键盘按下Enter键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!