Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to listen to keyboard on screen Flutter?

Tags:

flutter

I am building a mobile app, I want to remove a widget when the keyboard appears on the screen, i.e when the input text field is on focus.

I have tried to use RawKeyboardListener but that doesn't seem to work, my code is as below:

new Container(
  child: new RawKeyboardListener(
    focusNode: new FocusNode(),
    onKey: (input) => debugPrint("*****KEY PRESSED"),
    child: new TextField(
      controller: new TextEditingController(),
    ),
  ),
);
like image 249
grepLines Avatar asked Dec 14 '17 23:12

grepLines


People also ask

How do I know if my keyboard is visible Flutter?

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.


1 Answers

You can use this simple check:

MediaQuery.of(context).viewInsets.bottom == 0

The keyboard is closed when this returns true, otherwise it's open. Be aware to take the context of the whole screen (Scaffold for example) and not only from one widget.

This is how you integrate that check to your code:

Visibility(
  child: Icon(Icons.add),
  visible: MediaQuery.of(context).viewInsets.bottom == 0,
)
like image 74
Benjamin Menrad Avatar answered Sep 20 '22 14:09

Benjamin Menrad