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

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 ,它将为我们处理键盘关闭。 结果如下所示:

Image for post
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 全局 提示

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值