在一个选项卡中,我有一个 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());
}
});
}
@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()
],
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
您可以将手势检测器添加到脚手架并删除焦点。但这对选项卡不起作用。对于选项卡,您需要执行以下操作:
controller.addListener((){
if(controller.indexIsChanging)
{
FocusScope.of(context).detach();
}
});
Run Code Online (Sandbox Code Playgroud)
其中控制器是您的选项卡控制器。希望有帮助
| 归档时间: |
|
| 查看次数: |
1411 次 |
| 最近记录: |