Android上的Flutter嵌套导航返回按钮处理

rbl8hiat  于 2023-03-31  发布在  Flutter
关注(0)|答案(1)|浏览(222)
背景

我在InitialPage上有一个导航器小部件,我在上面推了两个路由(NestedFirstRouteNestedSecondRoute)。当我按下Android上的物理后退按钮时,导航器中的两个路由都弹出(这是预期的)。

用例

所以我想处理这种情况下,当按下返回按钮时,只有顶部路由(NestedSecondRoute)必须弹出。

我尝试的解决方案

为了解决这个问题,我在WillPopScope中封装了Navigator小部件来处理后退按钮按下事件,并将键分配给嵌套的路由,以便在willPop范围中弹出路由时使用它们。
我在这一行得到一个异常
public void run(){

Exception has occurred.
_CastError (Null check operator used on a null value)

这里是最小的和完整的代码示例

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  MyApp({Key? key}) : super(key: key);
  final _navigatorKey = GlobalKey<NavigatorState>();
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      onGenerateRoute: (settings) {
        switch (settings.name) {
          case NestedFirstPage.route:
            return MaterialPageRoute(
              builder: (context) {
                return WillPopScope(
                  onWillPop: () async {
                    if (NestedFirstPage.firstPageKey.currentState!.canPop()) {
                      NestedFirstPage.firstPageKey.currentState!.pop();
                      return false;
                    } else if (NestedSecondPage.secondPageKey.currentState!
                        .canPop()) {
                      NestedSecondPage.secondPageKey.currentState!.pop();
                      return false;
                    }
                    return true;
                  },
                  child: Navigator(
                    key: _navigatorKey,
                    onGenerateRoute: (settings) {
                      switch (settings.name) {
                        case Navigator.defaultRouteName:
                          return MaterialPageRoute(
                            builder: (context) => const NestedFirstPage(),
                            settings: settings,
                          );
                        case NestedSecondPage.route:
                          return MaterialPageRoute(
                            builder: (context) => const NestedSecondPage(),
                            settings: settings,
                          );
                      }
                    },
                  ),
                );
              },
              settings: settings,
            );
        }
      },
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const InitialPage(title: 'Initial Page'),
    );
  }
}

class InitialPage extends StatelessWidget {
  const InitialPage({Key? key, required this.title}) : super(key: key);
  final String title;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            OutlinedButton(
              onPressed: () {
                Navigator.pushNamed(context, NestedFirstPage.route);
              },
              child: const Text('Move to Nested First Page'),
            ),
          ],
        ),
      ),
    );
  }
}

class NestedFirstPage extends StatelessWidget {
  const NestedFirstPage({Key? key}) : super(key: key);

  static final GlobalKey<NavigatorState> firstPageKey =
      GlobalKey<NavigatorState>();
  static const String route = '/nested/first';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: firstPageKey,
      appBar: AppBar(title: const Text('Nested First Page')),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            const Text('First page'),
            OutlinedButton(
              child: const Text('Move to Nested Second Page'),
              onPressed: () {
                Navigator.pushNamed(context, NestedSecondPage.route);
              },
            ),
          ],
        ),
      ),
    );
  }
}

class NestedSecondPage extends StatelessWidget {
  const NestedSecondPage({Key? key}) : super(key: key);
  static final GlobalKey<NavigatorState> secondPageKey =
      GlobalKey<NavigatorState>();
  static const String route = '/nested/second';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: secondPageKey,
      appBar: AppBar(title: const Text('Nested Second Page')),
      body: const Center(
        child: Text('Second Page'),
      ),
    );
  }
}
vx6bjr1n

vx6bjr1n1#

下面是上述代码的一个稍微修改的版本,它允许推送嵌套的路由,并且可以通过Android的后退按钮弹出

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  MyApp({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'Flutter Demo',
        routes: {
          '/nested/first': (context) => const NestedFirstPage(),
          '/nested/first/second': (context) => const NestedSecondPage()
        },
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: const RouteManager());
  }
}

class InitialPage extends StatelessWidget {
  const InitialPage({Key? key, required this.title}) : super(key: key);
  final String title;
  static const String route = '/';
  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async {
        if (navigatorKey.currentState != null &&
            navigatorKey.currentState!.canPop()) {
          navigatorKey.currentState!.pop();
          return true;
        }
        return false;
      },
      child: Scaffold(
        appBar: AppBar(
          title: Text(title),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              OutlinedButton(
                onPressed: () {
                  navigate(context, NestedFirstPage.route);
                },
                child: const Text('Move to Nested First Page'),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

Future<void> navigate(BuildContext context, String route,
        {bool isDialog = false, bool isRootNavigator = true}) =>
    Navigator.of(context, rootNavigator: isRootNavigator).pushNamed(route);

final navigatorKey = GlobalKey<NavigatorState>();

class RouteManager extends StatelessWidget {
  const RouteManager({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Navigator(
        key: navigatorKey,
        initialRoute: '/',
        onGenerateRoute: (RouteSettings settings) {
          WidgetBuilder builder;
          switch (settings.name) {
            case '/nested/first':
              builder = (BuildContext _) => const NestedFirstPage();
              break;
            case '/nested/first/second':
              builder = (BuildContext _) => const NestedSecondPage();
              break;
            default:
              builder =
                  (BuildContext _) => const InitialPage(title: 'Initial Page');
          }
          return MaterialPageRoute(builder: builder, settings: settings);
        });
  }
}

class NestedFirstPage extends StatelessWidget {
  static final GlobalKey<NavigatorState> firstPageKey =
      GlobalKey<NavigatorState>();
  static const String route = '/nested/first';

  const NestedFirstPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: firstPageKey,
      appBar: AppBar(title: const Text('Nested First Page')),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            const Text('First page'),
            OutlinedButton(
              child: const Text('Move to Nested Second Page'),
              onPressed: () {
                navigate(context, NestedSecondPage.route);
              },
            ),
          ],
        ),
      ),
    );
  }
}

class NestedSecondPage extends StatelessWidget {
  const NestedSecondPage({Key? key}) : super(key: key);
  static final GlobalKey<NavigatorState> secondPageKey =
      GlobalKey<NavigatorState>();
  static const String route = '/nested/first/second';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: secondPageKey,
      appBar: AppBar(title: const Text('Nested Second Page')),
      body: const Center(
        child: Text('Second Page'),
      ),
    );
  }
}

这里有一个real world example与嵌套底部导航栏。

相关问题