How can I dismiss the on screen keyboard?

前端 未结 18 2358
星月不相逢
星月不相逢 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: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 {
      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());
    }
    

提交回复
热议问题