Skip to content

Instantly share code, notes, and snippets.

@ewaldhorn
Created May 8, 2019 18:56
Show Gist options
  • Save ewaldhorn/fbf3c9268573afaef87a1901fffa7725 to your computer and use it in GitHub Desktop.
Save ewaldhorn/fbf3c9268573afaef87a1901fffa7725 to your computer and use it in GitHub Desktop.
Flutter Dynamic TabView
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
State<StatefulWidget> createState() => MyAppState();
}
class MyAppState extends State<MyApp> {
bool isExpanded = false;
@override
Widget build(BuildContext context) {
return MaterialApp(
home: DefaultTabController(
length: _calcNumberOfTabs(),
child: Scaffold(
appBar: AppBar(
actions: <Widget>[
GestureDetector(
onTap: _toggleTabs, child: Icon(Icons.add_circle_outline))
],
bottom: TabBar(
tabs: _buildTabs(),
),
title: Text('Flutter Tabs Example'),
),
body: TabBarView(
children: _buildChildren(),
),
),
),
);
}
_toggleTabs() {
print("Toggle called");
setState(() {
isExpanded = !isExpanded;
});
}
_buildTabs() {
List<Widget> tabs = List();
tabs.add(Tab(icon: Icon(Icons.directions_car)));
tabs.add(Tab(icon: Icon(Icons.directions_transit)));
if (isExpanded) {
tabs.add(Tab(icon: Icon(Icons.directions_bike)));
}
return tabs;
}
_buildChildren() {
List<Widget> children = List();
children.add(FirstScreen());
children.add(SecondScreen());
if (isExpanded) {
children.add(OptionalScreen());
}
return children;
}
_calcNumberOfTabs() {
return (isExpanded ? 3 : 2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment