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

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

    It is true what maheshmnj said that from version v1.7.8+hotfix.2, you can hide keyboard using unfocus() instead of requestfocus().

    FocusScope.of(context).unfocus()
    

    But in my case I was still presented with a lot of layout errors, as the screen I navigated to was not capable of handling the layout.

    ════════ Exception Caught By rendering library ═════════════════════════════════
    The following JsonUnsupportedObjectError was thrown during paint():
    Converting object to an encodable object failed: Infinity
    When the exception was thrown, this was the stack
    #0      _JsonStringifier.writeObject  (dart:convert/json.dart:647:7)
    #1      _JsonStringifier.writeMap  (dart:convert/json.dart:728:7)
    #2      _JsonStringifier.writeJsonValue  (dart:convert/json.dart:683:21)
    #3      _JsonStringifier.writeObject  (dart:convert/json.dart:638:9)
    #4      _JsonStringifier.writeList  (dart:convert/json.dart:698:9)
    

    This was handled by inserting "resizeToAvoidBottomInset: false" in the receiving screens Scaffold()

    @override
      Widget build(BuildContext context) {
        return Scaffold(
          resizeToAvoidBottomInset: false,   // HERE
          appBar: AppBar(
            centerTitle: true,
            title: Text("Receiving Screen "),
          ),
          body: Container(...)
          ),
        );
      }
    

提交回复
热议问题