【发布时间】:2019-10-23 06:41:33
【问题描述】:
在一个选项卡中,我有一个 TextFormField,而在另一个选项卡中只有一个文本列表。当我选择键盘打开的文本字段时,我跳到第二个选项卡,键盘仍然显示。 我什至可以写,当我回到 Tab 1 时,我明白我为什么要打字了。
您知道如何对第二个选项卡执行操作以将焦点从文本字段中移开吗?
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),
],
)),
);
求解代码
应用@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());
}
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('2 Tabs'),
bottom: TabBar(controller: _tabController, tabs: <Widget>[
Tab(text: 'Tab with Text Field'),
Tab(text: 'Empty Tab'),
]),
),
body: TabBarView(
controller: _tabController,
children: <Widget>[
Container(
child: TextFormField(
decoration: InputDecoration(labelText: 'Title'),
),
),
Container()
],
),
);
}
}
【问题讨论】:
-
感谢@AaronSaunders,但 detach 对我不起作用。 VSC 确实标记为错误代码:“没有为类 'FocusScopeNode' 定义方法 'detach'”。我不得不使用
FocusScope.of(context).requestFocus(new FocusNode());
标签: flutter