Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter: Detect keyboard open/close

Tags:

flutter

dart

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 :-)

like image 906
Jakob Kristensen Avatar asked Feb 12 '18 15:02

Jakob Kristensen


People also ask

How do I close open keyboard in flutter?

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.

How do I know if my keyboard is open?

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.

How do you close or dismiss the keyboard in a flutter mobile app?

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.


2 Answers

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 
like image 184
Hemanth Raj Avatar answered Oct 08 '22 22:10

Hemanth Raj


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);     },   ); } 
like image 34
ad ee Avatar answered Oct 08 '22 23:10

ad ee