本文介绍了如何关闭屏幕键盘?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 TextFormField
收集用户输入,并且当用户按下 FloatingActionButton
表示完成时,我要关闭屏幕键盘。
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 screen keyboard.
如何使键盘自动消失?
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: 'Example Text'),
),
),
);
}
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
home: new MyHomePage(),
);
}
}
void main() {
runApp(new MyApp());
}
推荐答案
注意:此答案已过时。 。
您可以通过取消 TextFormField
的焦点并将其分配给未使用的 FocusNode
:
You can dismiss the keyboard by taking away the focus of the TextFormField
and giving it to an unused FocusNode
:
FocusScope.of(context).requestFocus(FocusNode());
这篇关于如何关闭屏幕键盘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!