Job*_*yNZ 3 flutter flutter-layout flutter-navigation flutter-listview flutter-web
我正在创建一个管理仪表板,我目前有两个连续的视图小部件:
这是页面的代码:
import 'package:flutter/material.dart';
import 'package:webenrol/widgets/dashboard_admin.dart';
import 'package:webenrol/widgets/drawer.dart';
//TODO: add flappy_search_bar package and add to appBar
class AdminDashboard extends StatelessWidget {
//TODO: Add title
static String id = '/admin_dashboard';
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Admin Dashboard - Overview'),),
body: Container(child: Row(
children: <Widget>[
//Sidebar
DashboardSideBar(),
//Main Dashboard Content
DashboardAdmin(),
],
)),
);
}
}
Run Code Online (Sandbox Code Playgroud)
我将为侧边栏中的链接创建其他内容小部件,我最希望的是,将内容小部件更新为在菜单上单击的内容,并且还将 ListTile 选择为活动状态,而无需重新加载页面。
这可能吗,我的 WebApp 是否为此正确布局,还是我需要更改它?
所以我找到了一个解决方案,我需要使用 TabController 和 TabView。
当我设置 TabController 时,我设置了一个侦听器来侦听其索引上的任何事件。
class _State extends State<AdminDashboard> with SingleTickerProviderStateMixin{
int active = 0;
//TODO: Add title
@override
void initState() {
super.initState();
tabController = TabController(length: 5, vsync: this, initialIndex: 0)
..addListener(() {
setState(() {
active = tabController.index;
});
});
}
Run Code Online (Sandbox Code Playgroud)
然后我修改了我的菜单以动画到正确的页面 onTap 并且如果我所在的页面是真实的,也会被选中。
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);
},
),
],
);
}
Run Code Online (Sandbox Code Playgroud)
然后我必须在内容区域中简单地设置我的 TabBarView:
return Scaffold(
appBar: AppBar(
//TODO: Make title dynamic to page using tabController.index turnkey operator
title: Text('Admin Dashboard - Overview'),
),
body: Container(
child: Row(
children: <Widget>[
//Responsive Sidebar
DashboardSideBar(),
//Main Dashboard Content
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'),),
],),
),
],
)),
);
Run Code Online (Sandbox Code Playgroud)
我仍然需要重构我的代码并清理它,但是对于任何想要创建一个干净的动态 Web 仪表板的人来说,这就是方法:)
| 归档时间: |
|
| 查看次数: |
3460 次 |
| 最近记录: |