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.
我正在使用TextFormField收集用户输入,当用户按下FloatingActionButton表示它们已完成时,我想取消屏幕上的键盘。
How do I make the keyboard go away automatically?
如何让键盘自动消失?
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());
}
更多回答
sir can you please go through the below question relating to flutter . below link is my question link in stack overflow ----- stackoverflow.com/questions/69640226/… -------
先生,请您回答下面关于颤振的问题好吗?下面的链接是堆栈溢出中的我的问题链接-Stackoverflow.com/Questions/69640226/…
try using a TextEditingController.
at the begining,
尝试使用TextEditingController。一开始,
final myController = TextEditingController();
@override
void dispose() {
// Clean up the controller when the widget is disposed.
myController.dispose();
super.dispose();
}
and in the on press event,
在新闻发布会上,
onPressed: () {
myController.clear();}
this will dismiss the keybord.
这将使Keybord失效。
First Connect the TextField with focusNode
首先将Textfield与FocusNode连接起来
TextField(
focusNode: focusNode),
now for show/open the keyboard call
现在开始显示/打开键盘调用
focusNode.requestFocus();
and for hide/dismiss keyboard call
并用于隐藏/取消键盘调用
focusNode.unfocus();
and if you want to know keyboard is visible or not at that time check the belew condition
如果您想知道键盘是否可见,请检查Belew条件
final keyBoardVisible = MediaQuery.of(context).viewInsets.bottom != 0;
最终的keyBoardVisible=MediaQuery.of(上下文).viewInsets.Bottom!=0;
here keyBoardVisible value is true if keyboard is visible and false for dismissed keyboard
此处,如果键盘可见,则keyBoardVisive值为True,如果已取消键盘,则值为False
I think there is a easy way.
我认为有一种简单的方法。
set keyboardType: TextInputType.none,
设置键盘类型:TextInputType.one,
TextField(
autofocus: true,
keyboardType: TextInputType.none,
文本字段(AutoFocus:True,KeyboardType:TextInputType.one,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: '',
),
controller: _controller,
)
If you use TextField(maxLines: null)
and just want to show Done
button ON the screen keyboard to hide it, the code below works.
如果您使用Textfield(MaxLines:NULL),并且只想在屏幕键盘上显示完成按钮来隐藏它,下面的代码可以工作。
TextField(
keyboardType: TextInputType.text,
maxLines: null,
)
Side note: why in the first place doesn't the keyboard show Done
button? The reason is found in the implementation of TextField
:
附注:为什么键盘上一开始没有显示完成按钮?原因可以在Textfield的实现中找到:
keyboardType = keyboardType ?? (maxLines == 1 ? TextInputType.text : TextInputType.multiline),
更多回答
It only clears the controller's text. It doesn't unfocus the TextField.
它只清除控制器的文本。它不会取消文本字段的焦点。
我是一名优秀的程序员,十分优秀!