Flutter:如何检测键盘按下Enter键? [英] Flutter: How to detect keyboard press Enter?

查看:2376
本文介绍了Flutter:如何检测键盘按下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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆