【问题标题】:Flutter: feels like my Navigator stack is empty, although if I use Navigator.pop it works well, black screen comes if I use Navigator.popUntil?Flutter:感觉我的 Navigator 堆栈是空的,虽然如果我使用 Navigator.pop 效果很好,如果我使用 Navigator.popUntil 会出现黑屏?
【发布时间】:2021-08-22 01:08:03
【问题描述】:

编辑:我最终使它工作,更改了代码。

这是我启动应用程序的方式:

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'Test Demo',
        home: LoginPage(),
         onGenerateRoute: (settings) {
          return CustomPageRouteBuilder.getPageRouteBuilder(Routing.openRoute(settings), settings.name);
        },
    );
  }
}

class CustomPageRouteBuilder {
  static PageRouteBuilder getPageRouteBuilder(Widget widget, [String? name]) {
    return PageRouteBuilder(
        settings: RouteSettings(name: name),
        transitionDuration: Duration(
          milliseconds: 100,
        ),
        transitionsBuilder: (BuildContext context, Animation<double> animation, Animation<double> secAnimation, Widget child) {
          animation = CurvedAnimation(
            parent: animation,
            curve: Curves.elasticIn,
          );
          return FadeTransition(
            opacity: animation,
            child: child,
          );
        },
        pageBuilder: (BuildContext context, Animation<double> animation, Animation<double> secAnimation) {
          return Scaffold(
            body: Stack(children: [
              BiometricAuthentication(widget),
              Align(
                alignment: Alignment.bottomLeft,
                child: Opacity(
                  opacity: 0.5,
                  child: Text("Version: ${Constants.buildVersion}",
                      style: TextStyle(
                        color: Constants.FONT_COLOR,
                        fontSize: 12,
                      )),
                ),
              )
            ]),
          );
        });
  }
}

然后我让我的 Routing 类处理路由:

enum Routes {
  LOGIN,
  FIRST_PAGE,
  SECOND_PAGE,
  THIRD_PAGE,
}

extension GetRoute on Routes {
  String route() {
    return '/' + this.toString().split('.').last.toLowerCase().replaceAll('_', '-');
  }
}

class Routing {
  static openRoute(RouteSettings settings) {
    return getRoute(settings);
  }

  static getRoute(RouteSettings settings) {
     String? name = settings.name;
     if (name == Routes.LOGIN.route()) {
      return LoginPage();
    }

    if (name == Routes.FIRST_PAGE.route()) {
      return FirstPage();
    }

   if (name == Routes.SECOND_PAGE.route()) {
      return SecondPage();
   }

   if (name == Routes.THIRD_PAGE.route()) {
      return ThirdPage();
   }
  }
}

我在我的ThirdPage 上使用以下代码:登录 --> FirstPage --> SecondPage --> ThirdPage:

await Navigator.pushNamed(context, Routes.FIRST_PAGE.route()); // Login
await Navigator.pushNamed(context, Routes.SECOND_PAGE.route()); // FirstPage
await Navigator.pushNamed(context, Routes.THIRD_PAGE.route()); // SecondPage

所以我的导航器堆栈中应该有 4 页。

现在我尝试 popUntil Routes.FIRST_PAGE.route(),所以 LoginFirstPage 应该在导航器中:

Navigator.popUntil(context, ModalRoute.withName(Routes.FIRST_PAGE.route())); // ThirdPage

在这里,我希望在堆栈中有 Login -> FirstPage,但一切都变黑了。

如果我只使用Navigator.pop(context),它会从ThirdPage 返回到SecondPage。但在我的实际应用程序中,如果使用按钮,我必须返回 5 个屏幕,所以我不想在每个页面中都处理它......

有什么想法吗?提前致谢。

【问题讨论】:

    标签: flutter dart


    【解决方案1】:
        switch (settings.name) {
          case RouteNames.home:
            return MaterialPageRoute(
              settings: RouteSettings(name: '/HomePage'),
              builder: (_) => HomePage(),
            );
         };
    

    这就是我设置页面并将它们返回给onGenerateRoute 的方式。我怀疑你没有设置路由设置名称settings: RouteSettings(name: '/HomePage')。这将导致导航器不知道您的路由名称存在于堆栈中。

    【讨论】:

    • 有了这个问题,我使用PageRouteBuilder getPageRouteBuilder(Widget widget) {} 在整个过渡过程中提供一些动画。所以当我在我的MyApp 中更改为这个时,它会抛出:Unhandled Exception: type 'MaterialPageRoute&lt;dynamic&gt;' is not a subtype of type 'Widget'。我将其添加到问题中。
    • 经过一些研究,我成功了。你帮了很多忙,谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-22
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-29
    相关资源
    最近更新 更多