我用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) 在一个选项卡中,我有一个 TextFormField,而在另一个选项卡中只有一个文本列表。当我选择 Text 字段时,键盘已打开,然后我跳转到第二个 Tab 并且键盘仍然显示。我什至可以写作,当我回到 Tab 1 时,我明白了我为什么要打字。
您知道如何对第二个 Tab 执行操作以将焦点从文本字段中移出吗?
DefaultTabController(
length: 2,
child: Scaffold(
appBar: AppBar(
title: Text('Manage Products'),
bottom: TabBar(tabs: <Widget>[
Tab(icon: Icon(Icons.create), text: 'Create Product'),
Tab(icon: Icon(Icons.list), text: 'My Products'),
]),
),
body: TabBarView(
children: <Widget>[
ProductEditPage(addProduct: addProduct),
ProductListPage(products, updateProduct),
],
)),
);
Run Code Online (Sandbox Code Playgroud)
解决代码
应用@nick.tdr 建议后,示例代码如下:
class _Test extends State<Test> with TickerProviderStateMixin {
TabController _tabController;
@override
void initState() {
super.initState();
_tabController = TabController(vsync: this, length: 2);
_tabController.addListener(() {
if (_tabController.indexIsChanging) {
FocusScope.of(context).requestFocus(new FocusNode()); …Run Code Online (Sandbox Code Playgroud) 嘿,我该如何关闭这里的键盘?我希望键盘在该区域外点击时关闭。
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.fromLTRB(15, 0, 15, 0),
child: TextFormField(
keyboardType: TextInputType.number,
maxLength: 10,
controller: widget._phoneController,
onTap: () => FocusScope.of(context).unfocus(),
inputFormatters: [
//input type
FilteringTextInputFormatter.allow(
RegExp(r'[0-9]'),
),
],
//how the text box is decorated
decoration: buildInputDecoration(
Icons.phone, 'Enter your 10 digit phone number'),
),
);
}
Run Code Online (Sandbox Code Playgroud) 我不知道这是否是正常的颤动行为,但是只有当您点击键盘“确定”按钮失去焦点时才会失去焦点,这很烦人。如果不这样做,光标将在文本字段中保持活动状态。
避免这种情况的最佳做法是什么?(在 gif 中它不是很受欢迎,但失去焦点的唯一方法是点击键盘上的“确定”按钮,否则无论您是否点击屏幕的其他区域,光标都将始终处于活动状态)
Widget _searchBar() {
return Container(
child: TextField(
textAlignVertical: TextAlignVertical.center,
style: TextStyle(
textBaseline: TextBaseline.alphabetic,
color: _styles["gris_oscuro1"]["color"],
),
onChanged: (value) {},
decoration: InputDecoration(
filled: true,
fillColor: _styles["gris_claro"]["color"],
alignLabelWithHint: true,
hintText: "Buscar por código",
hintStyle: TextStyle(color: _styles["gris_oscuro2"]["color"]),
prefixIcon:
Icon(Icons.search, color: _styles["gris_oscuro2"]["color"]),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(40)),
borderSide:
BorderSide(color: _styles["gris_claro"]["color"], width: 1.0),
),
),
),
);
);
Scaffold(
key: _scaffoldKey,
backgroundColor: _styles["fondo"]["bg"],
drawer: MenuWidget(),
body: SafeArea(
child: _searchBar(),
)
Run Code Online (Sandbox Code Playgroud)