How can I dismiss the on screen keyboard?

前端 未结 18 2307
星月不相逢
星月不相逢 2020-11-30 18:31

I am collecting user input with a TextFormField and when the user presses a FloatingActionButton indicating they are done, I want to dismiss the on

相关标签:
18条回答
  • 2020-11-30 18:58
    _dismissKeyboard(BuildContext context) {
       FocusScope.of(context).requestFocus(new FocusNode());
    }
    
    @override
    Widget build(BuildContext context) {
    
    return new GestureDetector(
        onTap: () {
        this._dismissKeyboard(context);
        },
        child: new Container(
        color: Colors.white,
        child: new Column(
            children: <Widget>[/*...*/],
        ),
        ),
     );
    }
    
    0 讨论(0)
  • 2020-11-30 19:00

    For Flutter 1.17.3 (stable channel as of June 2020), use

    FocusManager.instance.primaryFocus.unfocus();
    
    0 讨论(0)
  • 2020-11-30 19:01

    As of Flutter v1.7.8+hotfix.2, the way to go is:

    FocusScope.of(context).unfocus()
    

    Comment on PR about that:

    Now that #31909 (be75fb3) has landed, you should use FocusScope.of(context).unfocus() instead of FocusScope.of(context).requestFocus(FocusNode()), since FocusNodes are ChangeNotifiers, and should be disposed properly.

    -> DO NOT use ̶r̶e̶q̶u̶e̶s̶t̶F̶o̶c̶u̶s̶(̶F̶o̶c̶u̶s̶N̶o̶d̶e̶(̶)̶ anymore.

     F̶o̶c̶u̶s̶S̶c̶o̶p̶e̶.̶o̶f̶(̶c̶o̶n̶t̶e̶x̶t̶)̶.̶r̶e̶q̶u̶e̶s̶t̶F̶o̶c̶u̶s̶(̶F̶o̶c̶u̶s̶N̶o̶d̶e̶(̶)̶)̶;̶
    
    0 讨论(0)
  • 2020-11-30 19:02

    None of the above solutions don't work for me.

    Flutter suggests this - Put your widget inside new GestureDetector() on which tap will hide keyboard and onTap use FocusScope.of(context).requestFocus(new FocusNode())

    class Home extends StatelessWidget {
    @override
      Widget build(BuildContext context) {
        var widget = new MaterialApp(
            home: new Scaffold(
                body: new Container(
                    height:500.0,
                    child: new GestureDetector(
                        onTap: () {
                            FocusScope.of(context).requestFocus(new FocusNode());
                        },
                        child: new Container(
                            color: Colors.white,
                            child:  new Column(
                                mainAxisAlignment:  MainAxisAlignment.center,
                                crossAxisAlignment: CrossAxisAlignment.center,
    
                                children: [
                                    new TextField( ),
                                    new Text("Test"),                                
                                ],
                            )
                        )
                    )
                )
            ),
        );
    
        return widget;
    }}
    
    0 讨论(0)
  • 2020-11-30 19:02
    GestureDetector(
              onTap: () {
                FocusScope.of(context).unfocus();
              },
              child:Container(
        alignment: FractionalOffset.center,
        padding: new EdgeInsets.all(20.0),
        child: new TextFormField(
          controller: _controller,
          decoration: new InputDecoration(labelText: 'Example Text'),
        ),
      ), })
    

    try this on tap gesture

    0 讨论(0)
  • 2020-11-30 19:02

    For me, the Listener above App widget is the best approach I've found:

    Listener(
      onPointerUp: (_) {
        FocusScopeNode currentFocus = FocusScope.of(context);
        if (!currentFocus.hasPrimaryFocus && currentFocus.focusedChild != null) {
          currentFocus.focusedChild.unfocus();
        }
      },
      child: MaterialApp(
        title: 'Flutter Test App',
        theme: theme,
        ...
      ),
    )
    
    0 讨论(0)
提交回复
热议问题