How to listen to keyboard on screen Flutter?

后端 未结 5 989
栀梦
栀梦 2021-01-02 08:03

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

5条回答
  •  情深已故
    2021-01-02 08:52

    I used the package keyboard_visibility

    Then I wrapped my TextField with a KeyboardListener implemented as follows:

    class KeyboardListener extends StatefulWidget {
      final Widget child;
      final void Function(bool) onChange;
      KeyboardListener({@required this.child, @required this.onChange});
      @override
      _KeyboardListenerState createState() => _KeyboardListenerState();
    }
    
    class _KeyboardListenerState extends State {
      int _sId;
      KeyboardVisibilityNotification _kvn;
    
      @override
      void initState() {
        super.initState();
        _kvn = KeyboardVisibilityNotification();
        _sId = _kvn.addNewListener(
          onChange: widget.onChange,
        );
      }
    
      @override
      Widget build(BuildContext context) {
        return widget.child;
      }
    
      @override
      void dispose() {
        _kvn.removeListener(_sId);
        super.dispose();
      }
    }
    

提交回复
热议问题