How to hide soft input keyboard on flutter after clicking outside TextField/anywhere on screen?

后端 未结 16 2027
甜味超标
甜味超标 2020-12-08 01:54

Currently, I know the method of hiding the soft keyboard using this code, by onTap methods of any widget.

FocusScope.of(context).requestFocus(new         


        
16条回答
  •  甜味超标
    2020-12-08 02:24

    Updated

    Starting May 2019, FocusNode now has unfocus method:

    Cancels any outstanding requests for focus.

    This method is safe to call regardless of whether this node has ever requested focus.

    Use unfocus if you have declared a FocusNode for your text fields:

    final focusNode = FocusNode();
    
    // ...
    
    focusNode.unfocus();
    

    My original answer suggested detach method - use it only if you need to get rid of your FocusNode completely. If you plan to keep it around - use unfocus instead.

    If you have not declared a FocusNode specifically - use unfocus for the FocusScope of your current context:

    FocusScope.of(context).unfocus();
    

    See revision history for the original answer.

提交回复
热议问题