在 Flutter 的平板电脑上制作固定的应用程序范围菜单而不是抽屉

Making fixed app-wide menu instead of Drawer on tablets in Flutter

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

我尝试过的:

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

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

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

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

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

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