How to listen to keyboard on screen Flutter?

后端 未结 5 942
栀梦
栀梦 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:45

    The keyboard will automatically appear when the text field is focused. So you can add a listner to the focusnode to listen the focus change and hide respective widget.

    Example:

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

提交回复
热议问题