我目前正在尝试让多选项卡表单正常工作。我正在使用flutter_form_builder这个包。
问题是,FormBuilder 要求所有后代表单字段至少加载一次才能运行验证并提供字段的正确值。当用户访问每个选项卡时,一切都正常,但我不能假设这种情况总是会发生。
在我看来,最简单的解决方案是以某种方式让 TabBarView 在启动时呈现所有选项卡页面,以便每个选项卡小部件和表单字段都存在。
我有一个主页,我在其中定义表单生成器和其中的选项卡视图。重要的部分如下所示:
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: 'title',
bottom: TabBar(
controller: tabController,
tabs: <Widget>[
Tab(icon: Icon(Icons.title)),
Tab(icon: Icon(Icons.image)),
Tab(icon: Icon(Icons.category)),
],
),
),
body: FormBuilder(
key: formBuilderKey,
autovalidate: true,
child: TabBarView(
controller: tabController,
children: <Widget>[
MainTab(),
CoverTab(),
GroupsTab()
],
)
)
);
}
Run Code Online (Sandbox Code Playgroud)
一个标签页看起来或多或少像这样:
class GroupsTab extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return GroupsTabState();
}
}
class GroupsTabState extends State<GroupsTab> with AutomaticKeepAliveClientMixin {
@override
Widget build(BuildContext context) …Run Code Online (Sandbox Code Playgroud)