您的位置:首页 > 其它

flutter 全局 提示_Flutter —如何全局关闭键盘

2020-07-18 12:29 344 查看

flutter 全局 提示

When working with Flutter, we regularly try to mimic platform-specific behaviors. For example, the keyboard on iOS usually hides automatically once the user taps any place in the screen out of a TextField.

与Flutter合作时,我们会定期尝试模仿特定于平台的行为。 例如,一旦用户从TextField中点击屏幕上的任何位置,iOS上的键盘通常会自动隐藏。

One way to replicate this is by controlling the Focus manually in each screen that contains a TextField; once it loses the Focus, we dismiss the keyboard. It is a tedious solution, prone error, and generates a lot of code.

一种复制方法是在每个包含TextField的屏幕中手动控制Focus。 一旦失去焦点,我们将关闭键盘。 这是一个乏味的解决方案,容易出错,并生成大量代码。

A better way is to set this behavior globally in our Flutter app:

更好的方法是在Flutter应用中全局设置此行为:

The builder method from MaterialApp can be used for inserting widgets above the Navigator but below the other widgets. We’ve simply added a “global” GestureDetector that will handle the keyboard dismiss for us. The result can be seen below:

MaterialAppbuilder方法可用于在Navigator上方但在其他小部件下方插入小部件。 我们仅添加了一个“全局” GestureDetector ,它将为我们处理键盘关闭。 结果如下所示:

TextFields using the same "global" GestureDetector to dismiss the keyboard 使用相同的“全局” GestureDetector来关闭键盘的TextField

☕️ Twitter

Twitter️ 推特

☕️ LinkedIn

LinkedIn️LinkedIn

翻译自: https://itnext.io/flutter-how-to-dismiss-keyboard-globally-a543452cfefa

flutter 全局 提示

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: