在 Flutter 的平板电脑上制作固定的应用范围菜单而不是 Drawer [英] Making fixed app-wide menu instead of Drawer on tablets in Flutter

查看:9
本文介绍了在 Flutter 的平板电脑上制作固定的应用范围菜单而不是 Drawer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序有很多路线,几乎每条路线都使用带有相同抽屉菜单的 Scaffold 在应用程序内部导航(我自己的 CustomDrawer 小部件).至于大屏幕的设备,我希望在布局中始终在左侧显示菜单,而不是使用 Drawer(它在 Gmail 应用程序中的工作方式是这样的.我附上了一张图片).换句话说,我需要使用固定菜单制作响应式布局.

My application has a lot of routes and almost every route uses Scaffold with the same Drawer menu to navigate inside the app (my own CustomDrawer widget). As for devices with big screen, I want to always show the menu on the left side in layout, instead of using Drawer (it works like this in Gmail app. I attached a pic). In other words, I need to make a responsive layout with fixed menu.

我尝试过的:

  • 我知道你可以使用 LayoutBuilder 来学习约束大小;
  • 在每条路线中进行相同的布局会起作用,但这是不好的解决方案,因为每条路线都会为自己构建菜单(滚动位置会不同,许多菜单会有很多状态等).我需要一个适用于许多不同路线的应用范围菜单,但无法在顶级路线上进行布局;
  • 通过改变主要内容的状态将所有路由压缩成一条路由将需要大量重构,而且听起来一点都不好.

在 React 中,应用布局看起来像这样:

In React the app layout would look something like this:

    <App>
      <Menu />
      <main className="content">
        <Switch>
          <Route path="/about" component={About} />
          <Route path="/contact" component={Contact} />
          <Route path="/" component={Home} />
        </Switch>
      </main>
    </App>

但我不知道如何在 Flutter 中制作这样的东西.

But I have no idea how to make something like this in Flutter.

tl;dr:为了让 UI 响应大屏幕,我想显示固定菜单而不是抽屉.整个应用程序的一个菜单,而不是每条路线.

tl;dr: To make UI responsive for big screens, I want to show fixed menu instead of Drawer. One menu for whole app, not for every route.

推荐答案

诀窍是使用嵌套导航器.如果视口的宽度很大,我将菜单与内容放在一行中,否则将菜单作为 drawer 参数传递给 Scaffold.

The trick was to use a nested navigator. If viewport's width is big, I put a menu in a row with content, otherwise pass a menu to Scaffold as drawer parameter.

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  final String title;

  MyHomePage({Key key, this.title}) : super(key: key);

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final routes = List.generate(20, (i) => 'test $i');

  final navigatorKey = GlobalKey<NavigatorState>();

  bool isMenuFixed(BuildContext context) {
    return MediaQuery.of(context).size.width > 500;
  }

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);

    final menu = Container(
      color: theme.canvasColor,
      child: SafeArea(
        right: false,
        child: Drawer(
          elevation: 0,
          child: ListView(
            children: <Widget>[
              for (final s in routes)
                ListTile(
                  title: Text(s),
                  onTap: () {
                    // Using navigator key, because the widget is above nested navigator  
                    navigatorKey.currentState.pushNamedAndRemoveUntil(s, (r) => false);

                    // navigatorKey.currentState.pushNamed(s);
                  },
                ),
            ],
          ),
        )
      )
    );

    return Row(
      children: <Widget>[
        if (isMenuFixed(context))
          menu,
        Expanded(
          child: Navigator(
            key: navigatorKey,
            initialRoute: '/',
            onGenerateRoute: (settings) {
              return MaterialPageRoute(
                builder: (context) {
                  return Scaffold(
                    appBar: AppBar(
                      title: Text(settings.name),
                    ),
                    body: SafeArea(
                      child: Text(settings.name),
                    ),
                    drawer: isMenuFixed(context) ? null : menu,
                  );
                },
                settings: settings
              );
            },
          ),
        ),
      ],
    );
  }
}

这篇关于在 Flutter 的平板电脑上制作固定的应用范围菜单而不是 Drawer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆