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:
MaterialApp的builder方法可用于在Navigator上方但在其他小部件下方插入小部件。 我们仅添加了一个“全局” GestureDetector ,它将为我们处理键盘关闭。 结果如下所示:
![Image for post](https://miro.medium.com/freeze/max/9999/1*lAt8OA8lmy-au9NQg1YUyA.gif)
翻译自: https://itnext.io/flutter-how-to-dismiss-keyboard-globally-a543452cfefa
flutter 全局 提示