Flutter 基础控件 顶部导航栏 TabBar
/// 顶部导航栏 demo
class TabBarDemo extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _TabBarDemoState();
}
}
/// 混入 SingleTickerProviderStateMixin 供 TabController 使用
class _TabBarDemoState extends State<TabBarDemo>
with SingleTickerProviderStateMixin {
TabController _tabController;
// 初始化方法
@override
void initState() {
super.initState();
_tabController = TabController(length: 3, vsync: this);
}
// 销毁方法
@override
void dispose() {
_tabController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("保持页面状态"),
// 顶部导航栏
bottom: TabBar(
controller: _tabController,
tabs: [
Tab(icon: Icon(Icons.home)),
Tab(icon: Icon(Icons.description)),
Tab(icon: Icon(Icons.menu))
]
),
),
body: TabBarView(
controller: _tabController,
children: [
Text("第一页"),
Text("第二页"),
Text("第三页")
]
),
);
}
}
效果