-
-
Save slightfoot/0fc0a9f3302b1e468a1ed4ae67660554 to your computer and use it in GitHub Desktop.
For @mtellect on Flutter Gitter chat.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:meta/meta.dart'; | |
const List<TabItem> TabItems = const <TabItem>[ | |
const TabItem(title: 'Home', icon: Icons.home), | |
const TabItem(title: 'Gallery', icon: Icons.image), | |
const TabItem(title: 'Settings', icon: Icons.settings) | |
]; | |
class MyApp extends StatefulWidget { | |
@override | |
MyAppState createState() => new MyAppState(); | |
} | |
class MyAppState extends State<MyApp> with SingleTickerProviderStateMixin { | |
TabController tabController; | |
int _titleIndex = 0; | |
@override | |
void initState() { | |
super.initState(); | |
tabController = new TabController(length: TabItems.length, vsync: this); | |
tabController.animation.addListener(() { | |
final titleIndex = tabController.animation.value.round(); | |
if (_titleIndex != titleIndex) { | |
setState(() { | |
_titleIndex = titleIndex; | |
}); | |
} | |
}); | |
} | |
@override | |
void dispose() { | |
tabController.dispose(); | |
super.dispose(); | |
} | |
void onIconPressed(String icon) { | |
print('Icon $icon pressed.'); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return new Scaffold( | |
appBar: new AppBar( | |
title: new Text(TabItems[_titleIndex].title), | |
backgroundColor: Colors.blue, | |
bottom: new TabBar( | |
controller: tabController, | |
indicatorColor: Colors.white, | |
tabs: TabItems.map((item) => new Tab(icon: new Icon(item.icon))).toList(), | |
), | |
actions: <Widget>[ | |
new IconButton(icon: new Icon(Icons.search), onPressed: () => onIconPressed('search')), | |
new IconButton(icon: new Icon(Icons.more_vert), onPressed: () => onIconPressed('more')), | |
], | |
), | |
body: new TabBarView( | |
controller: tabController, | |
children: <Widget>[ | |
new Container(alignment: Alignment.center, child: Text('Home')), | |
new Container(alignment: Alignment.center, child: Text('Gallery')), | |
new Container(alignment: Alignment.center, child: Text('Settings')), | |
], | |
), | |
); | |
} | |
} | |
class TabItem { | |
const TabItem({@required this.title, @required this.icon}); | |
final String title; | |
final IconData icon; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment