使用路由器抽屉抽屉使用并检查抽屉中当前活动路线的高亮度?

时间:2018-03-07 14:27:08

标签: flutter

需要示例使用Flutter抽屉使用当前路线的路线在菜单中突出显示,并从任何其他路线返回主页路线,app仅从主页退出

1 个答案:

答案 0 :(得分:0)

这是我想到的:

import 'package:myapp/blocs/provider.dart';
import 'package:myapp/routes.dart';
import 'package:flutter/material.dart';

class DrawerItemList extends StatelessWidget {
  String getCurrentRouteName(context) {
    String currentRouteName;

    Navigator.popUntil(context, (route) {
      currentRouteName = route.settings.name;
      return true;
    });

    return currentRouteName;
  }

  @override
  Widget build(BuildContext context) {
    final bloc = Provider.of(context);
    String currentRoute = getCurrentRouteName(context);

    return Column(
      children: <Widget>[
        AppBar(
          automaticallyImplyLeading: false,
          title: Text('MyAppTitle'),
          actions: <Widget>[
            IconButton(
              icon: Icon(Icons.exit_to_app),
              tooltip: 'Log Out',
              onPressed: () {
                bloc.logOut();
                Navigator.pushReplacementNamed(
                  context,
                  AppRoutes.home,
                );
              },
            ),
          ],
        ),
        ListTile(
          leading: Icon(Icons.home),
          title: Text('Home'),
          selected: currentRoute == AppRoutes.home,
          onTap: () {
            if (currentRoute == AppRoutes.home) return;

            Navigator.pushReplacementNamed(
              context,
              AppRoutes.home,
            );
          },
        ),
        ListTile(
          leading: Icon(Icons.list),
          title: Text('Records'),
          selected: currentRoute == AppRoutes.records,
          onTap: () {
            if (currentRoute == AppRoutes.records) return;

            Navigator.pushReplacementNamed(
              context,
              AppRoutes.records,
            );
          },
        ),
        ListTile(
          title: Text('Planning'),
          leading: Icon(Icons.graphic_eq),
          selected: currentRoute == AppRoutes.planning,
          onTap: () {
            if (currentRoute == AppRoutes.planning) return;

            Navigator.pushReplacementNamed(
              context,
              AppRoutes.planning,
            );
          },
        ),
      ],
    );
  }
}