Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter Keyboard listen on hide and show

Tags:

How can I listen whether the keyboard is showing up or hiding?

I tried this example How to listen to keyboard on screen Flutter?

 void _listener(){     if(_myNode.hasFocus){       // keyboard appeared      }else{       // keyboard dismissed     } }  FocusNode _myNode = new FocusNode()..addListener(_listner);  TextField _myTextField = new TextField(         focusNode: _mynNode,         ...         ...     ); 

But unfortunately it doesn't work. Any ideas how it could be possible to listen to the keyboard change?

It seems like it works when I press "Done" on the keyboard. But if i press back on my phone it won't go to "keyboard dismissed" because the focus still exists.. Any help?

like image 470
GreenTigerEye Avatar asked Jan 03 '18 21:01

GreenTigerEye


People also ask

How do you detect keyboard hide in 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. You can use the keyboard_visibility package to do this effectively, I've used it and it works like charm.

How do you handle keyboard events in flutter?

For text entry, consider using a EditableText, which integrates with on-screen keyboards and input method editors (IMEs). Creates a widget that receives raw keyboard events. For text entry, consider using a EditableText, which integrates with on-screen keyboards and input method editors (IMEs).


1 Answers

KeyboardVisibilityBuilder

Listening for keyboard show/hide events can be achieved with WidgetsBindingObserver mixin. I prepared KeyboardVisibilityBuilder widget that handles the behavior for you. The usage is quite similar to AnimatedBuilder:

return KeyboardVisibilityBuilder(   builder: (context, child, isKeyboardVisible) {     if (isKeyboardVisible) {       // build layout for visible keyboard     } else {       // build layout for invisible keyboard     }   },   child: child, // this widget goes to the builder's child property. Made for better performance. ); 

KeyboardVisibilityBuilder implementation:

/// Calls `builder` on keyboard close/open. /// https://stackoverflow.com/a/63241409/1321917 class KeyboardVisibilityBuilder extends StatefulWidget {   final Widget child;   final Widget Function(     BuildContext context,     Widget child,     bool isKeyboardVisible,   ) builder;    const KeyboardVisibilityBuilder({     Key key,     this.child,     @required this.builder,   }) : super(key: key);    @override   _KeyboardVisibilityBuilderState createState() => _KeyboardVisibilityBuilderState(); }  class _KeyboardVisibilityBuilderState extends State<KeyboardVisibilityBuilder>     with WidgetsBindingObserver {   var _isKeyboardVisible = false;    @override   void initState() {     super.initState();     WidgetsBinding.instance.addObserver(this);   }    @override   void dispose() {     WidgetsBinding.instance.removeObserver(this);     super.dispose();   }    @override   void didChangeMetrics() {     final bottomInset = WidgetsBinding.instance.window.viewInsets.bottom;     final newValue = bottomInset > 0.0;     if (newValue != _isKeyboardVisible) {       setState(() {         _isKeyboardVisible = newValue;       });     }   }    @override   Widget build(BuildContext context) => widget.builder(         context,         widget.child,         _isKeyboardVisible,       ); } 
like image 119
Andrey Gordeev Avatar answered Sep 25 '22 16:09

Andrey Gordeev