I have a BottomNavigationBar
at the upper-most level of my application. I want to detect keyboard open and close basically anywhere in the app/subtree, so I can show and hide the BottomNavigationBar
whenever the keyboard is visible.
This is a general issue and may not be directly related to the BottomNavigationBar
. In other words, abstract from the BottomNavigationBar
:-)
Android devices have a solution; press the physical back button (provided on some mobile phones) or the soft key back button, and it closes the keyboard.
Android provides no direct way to determine if the keyboard is open, so we have to get a little creative. The View class has a handy method called getWindowVisibleDisplayFrame from which we can retrieve a rectangle which contains the portion of the view visible to the user.
TextField is a very common widget in Flutter. When you click on the TextField it opens up the on-screen keyboard. To hide/dismiss the keyboard you have to press the back button in Android and the done button (inside the onscreen keyboard) in iOS.
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.
You can check for the viewInsets
with MediaQuery
like:
MediaQuery.of(context).viewInsets.bottom
I just created a Flutter plugin to notify about keyboard open and close events. It works both on Android and iOS.
keyboard_visibility
import 'package:keyboard_visibility/keyboard_visibility.dart'; @override void initState() { super.initState(); KeyboardVisibilityNotification().addNewListener( onChange: (bool visible) { print(visible); }, ); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With