Flutter:检测键盘打开/关闭 [英] Flutter: Detect keyboard open/close

查看:77
本文介绍了Flutter:检测键盘打开/关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序的最上层有一个 BottomNavigationBar.我想基本上在应用程序/子树中的任何地方检测键盘打开/关闭,这样我就可以在键盘可见时显示/隐藏 BottomNavigationBar.

I have a BottomNavigationBar at the upper-most level of my app. I want to detect keyboard open/close basically anywhere in the app/subtree, so i can show/hide the BottomNavigationBar whenever the keyboard is visible.

这是一个普遍问题,可能与 BottomNavigationBar 没有直接关系.换句话说,抽象自 BottomNavigationBar :-)

This is a general issue and may not be directly related to the BottomNavigationBar. In other words, abstract from the BottomNavigationBar :-)

推荐答案

要检查键盘可见性,只需检查 viewInsets 属性位于小部件树中的任何位置.当 viewInsets.bottom 等于 0 时,键盘隐藏.

To check for keyboard visibility, just check for the viewInsets property anywhere in the widget tree. The keyboard is hidden when viewInsets.bottom is equal to zero.

您可以使用 MediaQuery 来检查 viewInsets,例如:

You can check for the viewInsets with MediaQuery like:

MediaQuery.of(context).viewInsets.bottom

希望有所帮助!

这篇关于Flutter:检测键盘打开/关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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