【问题标题】:Flutter Navigator popUntil method doesn't stop at the Named Route I want it to, and pops all the screenFlutter Navigator popUntil 方法不会在我想要的命名路由处停止,并弹出所有屏幕
【发布时间】:2021-12-05 00:48:15
【问题描述】:

我有这段代码想要弹出所有屏幕,直到它到达名称为“/”的基本屏幕。

Navigator.of(context).popUntil(ModalRoute.withName("/"));

在调用 popUntil 方法之前,我使用以下方法进行导航:

Navigator.of(context).pushNamed("/Loading");
Navigator.of(context).pushReplacementNamed("/Menu");

但我得到的结果是所有屏幕都被弹出,直到它进入黑屏。我应该改变什么让它停在“/”?

这是它的设置方式:

main.dart

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'My App',
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        debugShowCheckedModeBanner: false,
        onGenerateRoute: AppRouter().onGenerateRoute,
        initialRoute: '/',
      ),
    );
  }
}

class AppRouter {
  Route? onGenerateRoute(RouteSettings routeSettings) {
    switch (routeSettings.name) {
      case '/':
        return MaterialPageRoute(builder: (_) => const LoadingScreen());
      case '/Menu':
        return MaterialPageRoute(builder: (_) => const MenuScreen());
      case '/Loading':
        return MaterialPageRoute(builder: (_) => const LoadingScreen());
    }
  }
}


【问题讨论】:

    标签: flutter dart flutter-navigation flutter-routes


    【解决方案1】:

    ModalRoute.withName 谓词在路由绑定到特定路由名称时使用。因为您使用的是onGenerateRoute(这通常是最后的手段)而不是MaterialApp 中的routes 表,所以没有与/ 路由名称关联的路由。

    【讨论】:

      猜你喜欢
      • 2020-12-11
      • 1970-01-01
      • 2019-11-05
      • 1970-01-01
      • 2021-04-09
      • 1970-01-01
      • 2021-04-05
      • 1970-01-01
      • 2021-08-22
      相关资源
      最近更新 更多