0

^ ^ ^^ ^ 不!这个问题没有答案!^ ^ ^ ^ ^ ^

我遇到了问题Navigator.popUntil。我编写了一个小型演示应用程序来展示正在发生的事情。在我将此作为错误发布之前,我使用popUntil错了吗?

调用 popUntil 显示

在此处输入图像描述

看起来有些东西正在锁定导航器(设置_debugLocked)而不是释放它。

main.dart 下面:(可以粘贴到 Flutter 演示应用中)

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Routing Test Page'),
      onGenerateRoute: (RouteSettings settings) {
        switch (settings.name) {
          case '/':
            return MaterialPageRoute(
              builder: (_) => MyHomePage(title: 'Home Page',),
              settings: settings,
            );
          case '/home':
            return MaterialPageRoute(
              builder: (_) => MyHomePage(title: 'Home Page',),
              settings: settings,
            );
          case '/middlepage':
            return MaterialPageRoute(
              builder: (_) => MiddlePage(),
              settings: settings,
            );
          case '/bottompage':
            return MaterialPageRoute(
              builder: (_) => BottomBage(),
              settings: settings,
            );
          default:
            return null;
        }
      },
    );
  }
}

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

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'Home Page',
            ),
            OutlineButton(
              child: Text('push MiddlePage()'),
              onPressed: () => Navigator.push(
                context,
                MaterialPageRoute(builder: (context) => (MiddlePage())),
              ),
            ),
            OutlineButton(
              child: Text('pushNamed ''/middlepage'''),
              onPressed: () => Navigator.pushNamed(context, '/middlepage'),
            ),
          ],
        ),
      ),
    );
  }
}

class MiddlePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Middle Page'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'Middle Page',style: TextStyle(fontWeight: FontWeight.bold)
            ),
            OutlineButton(
              child: Text('push BottomPage()'),
              onPressed: () => Navigator.push(
                context,
                MaterialPageRoute(builder: (context) => (BottomBage())),
              ),
            ),
            OutlineButton(
              child: Text('pushNamed ''/bottompage'''),
              onPressed: () => Navigator.pushNamed(context, '/bottompage'),
            ),
            OutlineButton(
                child: Text('pop'), onPressed: () => Navigator.pop(context)),
            OutlineButton(
                child: Text('popUntil ''/home'''),
                onPressed: () =>
                    Navigator.popUntil(context, ModalRoute.withName('/home'))),
          ],
        ),
      ),
    );
  }
}

class BottomBage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Bottom page'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'Bottom Page', style: TextStyle(fontWeight: FontWeight.bold),
            ),
            OutlineButton(
              child: Text('push ''home'''),
              onPressed: () =>
                  Navigator.pushNamed(context, '/home'),
            ),
            OutlineButton(
                child: Text('pop'),
                onPressed: () =>
                    Navigator.pop(context)),

            OutlineButton(
              child: Text('popUntil ''/home'''),
              onPressed: () =>
                  Navigator.popUntil(context, ModalRoute.withName('/home')),
            ),
            OutlineButton(
              child: Text('popUntil ''/home'' (with Are You Sure box)) '),
              onPressed: () async {
                try {
                  if (await _areYouSureDialog(context)) {
                    Navigator.popUntil(context, ModalRoute.withName('/home'));
                  }
                } catch (e) {
                  debugPrint("exception: $e");
                }
              },
            ),
          ],
        ),
      ),
    );
  }
}

Future<bool> _areYouSureDialog(BuildContext context) async {
  return await showDialog<bool>(
        context: context,
        builder: (BuildContext context) {
          return AlertDialog(
            title: const Text('Pop back?'),
            content: const Text('Are you sure?'),
            actions: <Widget>[
              FlatButton(
                child: const Text('YES'),
                onPressed: () {
                  Navigator.of(context).pop(true);
                },
              ),
              FlatButton(
                child: const Text('NO'),
                onPressed: () {
                  Navigator.of(context).pop(false);
                },
              ),
            ],
          );
        },
      ) ??
      false;
}
4

1 回答 1

7

我重新创建了您的案例。每当我们想从任何地方弹出导航到主屏幕(根)时,有几种方法可以做到这一点,如下所示:

一、使用.isFirst方法:

Navigator.of(context).popUntil((route) => route.isFirst);
  1. 使用defaultRouteName

Navigator.popUntil(context, ModalRoute.withName(Navigator.defaultRouteName));

通过提供第context一个,route将确保导航将始终弹出到默认值。

您可以尝试任何一种方法。我在最后进行了测试,效果很好。

希望这能回答你的问题。

于 2019-10-01T11:37:25.817 回答