所以我找到了一个解决方案,我需要使用TabController和TabView。
当我设置我的TabController时,我设置了一个监听器来监听其索引的任何事件。
class _State extends State<AdminDashboard> with SingleTickerProviderStateMixin{
int active = 0;
@override
void initState() {
super.initState();
tabController = TabController(length: 5, vsync: this, initialIndex: 0)
..addListener(() {
setState(() {
active = tabController.index;
});
});
}
然后我修改了我的菜单,使其在点击时动画到正确的页面,并且如果当前所在的页面为真,则被选中。
Widget adminMenu() {
return ListView(
shrinkWrap: true,
children: <Widget>[
ListTile(
leading: Icon(Icons.home),
title: Text('Home'),
selected: tabController.index == 0 ? true : false,
onTap: () {
tabController.animateTo(0);
},
),
ListTile(
leading: Icon(Icons.add),
title: Text('Add New Centre'),
selected: tabController.index == 1 ? true : false,
onTap: () {
tabController.animateTo(1);
},
),
ListTile(
leading: Icon(Icons.list),
title: Text('List Centres'),
selected: tabController.index == 2 ? true : false,
onTap: () {
tabController.animateTo(2);
},
),
ListTile(
leading: Icon(Icons.people),
title: Text('Users'),
selected: tabController.index == 3 ? true : false,
onTap: () {
tabController.animateTo(3);
},
),
ListTile(
leading: Icon(Icons.exit_to_app),
title: Text('Logout'),
selected: tabController.index == 4 ? true : false,
onTap: () {
tabController.animateTo(4);
},
),
],
);
}
然后我只需要在内容区域简单设置我的TabBarView:
return Scaffold(
appBar: AppBar(
title: Text('Admin Dashboard - Overview'),
),
body: Container(
child: Row(
children: <Widget>[
DashboardSideBar(),
Expanded(
child: TabBarView(controller: tabController,
children: <Widget>[
DashboardAdmin(),
Container(child: Text('Hello World!'),),
Container(child: Text('Page 3'),),
Container(child: Text('Page 4'),),
Container(child: Text('Page 5'),),
],),
),
],
)),
);
我仍需重构并清理我的代码,但对于任何想要创建一个干净的动态Web仪表板的人来说,这就是方法 :)