我用a收集用户输入,TextFormField当用户按下FloatingActionButton指示它们完成时,我想关闭屏幕键盘.
如何让键盘自动消失?
import 'package:flutter/material.dart';
class MyHomePage extends StatefulWidget {
MyHomePageState createState() => new MyHomePageState();
}
class MyHomePageState extends State<MyHomePage> {
TextEditingController _controller = new TextEditingController();
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(),
floatingActionButton: new FloatingActionButton(
child: new Icon(Icons.send),
onPressed: () {
setState(() {
// send message
// dismiss on screen keyboard here
_controller.clear();
});
},
),
body: new Container(
alignment: FractionalOffset.center,
padding: new EdgeInsets.all(20.0),
child: new TextFormField(
controller: _controller,
decoration: new InputDecoration(labelText: …Run Code Online (Sandbox Code Playgroud) 目前我知道使用此代码隐藏软键盘的onTap方法,通过任何小部件的方法.
FocusScope.of(context).requestFocus(new FocusNode());
Run Code Online (Sandbox Code Playgroud)
但我想通过单击TextField外部或屏幕上的任何位置来隐藏软键盘.有没有什么方法可以做到这一点?