I have two tabs each with some data to be filled by the user. In Tab-1 (Personal Data) I have a form with some widgets where the user has to enter his personal details.
After filling all the details in the Tab-1 (form-1) only the Tab-2 (Medical History) should be navigated and visible. How to achieve this in Flutter?
We can achieve this by maintaining the count of tab bar items to be displayed.
Basically we have to create two List<Widget>
which will depend on tab bar count to be displayed.
Example:
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
int _tabBarCount = 1;
List<Widget> getTabBarList() { // Tab Bar items displayed on the App Bar
switch (_tabBarCount) {
case 1:
return [Tab(icon: Icon(Icons.search))];
case 2:
return [
Tab(icon: Icon(Icons.search)),
Tab(icon: Icon(Icons.file_download))
];
default:
return [];
}
}
List<Widget> getTabScreen() { // Screens to be displayed on Tab Bar
switch (_tabBarCount) {
case 1:
return [
RaisedButton(onPressed: () {
setState(() {
_tabBarCount = 2; // Click event, here tab bar count should increse, so that multiple tab bar can be visible.
});
}, child: Text('Save'),)
];
case 2:
return [
Text('First Screen'),
Text('Second Screen')
];
default:
return [];
}
}
Widget build(BuildContext context) {
return MaterialApp(
home: DefaultTabController(
length: _tabBarCount,
child: Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
bottom: TabBar(
tabs: getTabBarList(),
),
title: Text('Tabs Demo'),
),
body: TabBarView(
children: getTabScreen(),
),
),
),
);
}
}