Flutter TabBar
- TabBar
class FirstTabBarDemo extends StatefulWidget {
@override
_FirstTabBarDemoState createState() => _FirstTabBarDemoState();
}
class _FirstTabBarDemoState extends State<FirstTabBarDemo> with SingleTickerProviderStateMixin{
TabController _tabController;
@override
void initState() {
super.initState();
// 长度要和展示部分的Tab数一致
_tabController = TabController(vsync: this, length: 7);
}
@override
void dispose() {
_tabController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return SizedBox(
height: 500,
child: Scaffold(
appBar: AppBar(title: Text('选项卡'), leading: Icon(Icons.arrow_back_ios), backgroundColor: Colors.green,
bottom: TabBar(
// 是否可以滚动
isScrollable: true,
controller: _tabController,
tabs: [
Tab(text: '科技', icon: Icon(Icons.camera),),
Tab(text: '房产', icon: Icon(Icons.camera),),
Tab(text: '体育', icon: Icon(Icons.camera),),
Tab(text: '教育', icon: Icon(Icons.camera),),
Tab(text: '娱乐', icon: Icon(Icons.camera),),
Tab(text: '电影', icon: Icon(Icons.camera),),
Tab(text: '生活', icon: Icon(Icons.camera),),
],
),),
body: TabBarView(
controller: _tabController,
children: <Widget> [
Text('选项卡1'),
Text('选项卡2'),
Text('选项卡3'),
Text('选项卡4'),
Text('选项卡5'),
Text('选项卡6'),
Text('选项卡7'),
]
),
),
);
}
}
- 图片