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

后端 未结 16 1966
甜味超标
甜味超标 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:12

    If you want to do this "the right way", use Listener instead of GestureDetector.

    GestureDetector will only work for "single taps", which isn't representative of all possible gestures that can be executed.

    Listener(
      onPointerDown: (_) {
        FocusScopeNode currentFocus = FocusScope.of(context);
        if (!currentFocus.hasPrimaryFocus) {
          currentFocus.focusedChild.unfocus();
        }
      },
      child: MaterialApp(...),
    );
    

提交回复
热议问题