【问题标题】:Flutter Web Dashboard Content - Best Practice?Flutter Web Dashboard Content - 最佳实践?
【发布时间】:2020-06-20 11:37:08
【问题描述】:

我正在创建一个管理仪表板,我目前连续有两个视图小部件:

  • 边栏 - 300 像素(不是抽屉,因为我希望它永久显示) - 有一个列表。
  • 内容小部件 - 展开。

Admin Dashboard View

这是页面的代码:

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(),
        ],
      )),
    );
  }
}

我将为侧边栏中的链接创建其他内容小部件,我希望将内容小部件更新为在菜单上单击的内容,并将 ListTile 选为活动而不需要页面重新加载。

这可能吗,我的 WebApp 是否为此正确布局,还是我需要更改它?

【问题讨论】:

    标签: flutter flutter-layout flutter-web flutter-navigation flutter-listview


    【解决方案1】:

    所以我找到了一个解决方案,我需要使用 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;
          });
        });
      }
    

    然后我修改了我的菜单以动画到正确的页面 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);
            },
          ),
        ],
      );
    }
    

    然后我必须在内容区域中简单地设置我的 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'),),
                ],),
              ),
            ],
          )),
        );
    

    我仍然需要重构我的代码并清理它,但是对于任何想要创建干净的动态 Web 仪表板的人来说,这就是方法:)

    【讨论】:

    • 我们可以有路由器和标签控制器吗?为不同的菜单项设置不同的路由以进行深度链接。请帮我。我是新来的。我正在尝试使用 Flutter Web 构建一个 SPA Web 应用程序。我的网络应用程序应该有一个固定的菜单,并且每个菜单项都应该有一个深层链接。单击菜单项时,不应刷新菜单小部件,但必须使用内容视图选择当前菜单项才能根据所选菜单项加载内容。请给我一个好的参考。
    • 是的,您可以,按照您的说法,您不会有路由器,更多链接到您要查看的选项卡。使用您当前的代码创建一个问题,我今天会帮助您
    • 如何使用默认路由器或 fluro_router 添加深度链接?谢谢,救了我的培根
    • 您是否将其与 PageView 相关联?
    • 谢谢。非常有帮助。 :)
    猜你喜欢
    • 1970-01-01
    • 2020-01-09
    • 2020-05-11
    • 2021-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-08
    • 2021-06-24
    相关资源
    最近更新 更多