How can I dismiss the on screen keyboard?

前端 未结 18 2305
星月不相逢
星月不相逢 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:51

    Solution with FocusScope doesn't work for me. I found another:

    import 'package:flutter/services.dart';
    
    SystemChannels.textInput.invokeMethod('TextInput.hide');
    

    It solved my problem.

    0 讨论(0)
  • 2020-11-30 18:53

    Looks like different approaches for different version. I am using Flutter v1.17.1 and the below works for me.

    onTap: () {
        FocusScopeNode currentFocus = FocusScope.of(context);
        if (!currentFocus.hasPrimaryFocus && currentFocus.focusedChild != null) {
           currentFocus.focusedChild.unfocus();
        }
    }
    
    0 讨论(0)
  • 2020-11-30 18:53

    You can use unfocus() method from FocusNode class.

    import 'package:flutter/material.dart';
    
    class MyHomePage extends StatefulWidget {
      MyHomePageState createState() => new MyHomePageState();
    }
    
    class MyHomePageState extends State<MyHomePage> {
      TextEditingController _controller = new TextEditingController();
      FocusNode _focusNode = new FocusNode(); //1 - declare and initialize variable
    
      @override
      Widget build(BuildContext context) {
        return new Scaffold(
          appBar: new AppBar(),
          floatingActionButton: new FloatingActionButton(
            child: new Icon(Icons.send),
            onPressed: () {
                _focusNode.unfocus(); //3 - call this method here
            },
          ),
          body: new Container(
            alignment: FractionalOffset.center,
            padding: new EdgeInsets.all(20.0),
            child: new TextFormField(
              controller: _controller,
              focusNode: _focusNode, //2 - assign it to your TextFormField
              decoration: new InputDecoration(labelText: 'Example Text'),
            ),
          ),
        );
      }
    }
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return new MaterialApp(
          home: new MyHomePage(),
        );
      }
    }
    
    void main() {
      runApp(new MyApp());
    }
    
    0 讨论(0)
  • 2020-11-30 18:53

    This may simplify the case. Below code will work only if keyboard is open

    if(FocusScope.of(context).isFirstFocus) {
     FocusScope.of(context).requestFocus(new FocusNode());
    }
    
    0 讨论(0)
  • 2020-11-30 18:54

    To dismiss the keyboard (1.7.8+hotfix.2 and above) just call the method below:

    FocusScope.of(context).unfocus();
    

    Once the FocusScope.of(context).unfocus() method already check if there is focus before dismiss the keyboard it's not needed to check it. But in case you need it just call another context method: FocusScope.of(context).hasPrimaryFocus

    0 讨论(0)
  • 2020-11-30 18:55

    Following code helped me to hide keyboard

       void initState() {
       SystemChannels.textInput.invokeMethod('TextInput.hide');
       super.initState();
       }
    
    0 讨论(0)
提交回复
热议问题