尝试在 CupertinoApp 中添加带有底部导航栏的路由器

Trying to add router with bottom navigation bar in CupertinoApp

我正在尝试在 CupertinoApp 中添加带有底部导航栏的路由器,但是 Navigator.pushNamed(context,anotherPage) 出现错误

Could not find a generator for route RouteSettings("/anotherPage", null) in the _CupertinoTabViewState.

Navigator.push(context, CupertinoPageRoute(builder: (context)=>AnotherPage())); 正在工作 示例代码:

return CupertinoApp(
  localizationsDelegates: <LocalizationsDelegate<dynamic>>[
    DefaultMaterialLocalizations.delegate,
    DefaultWidgetsLocalizations.delegate,
    DefaultCupertinoLocalizations.delegate,
  ],
  theme: CupertinoThemeData(brightness: Brightness.light),  
 onGenerateRoute: Router.generateRoute,
  initialRoute: splashScreen,
); }} 

//路由器class

    class Router {
  static Route<dynamic> generateRoute(RouteSettings settings) {
    switch (settings.name) {
      case homeRoute:
        return CupertinoPageRoute(builder: (_) => CupertinoHomePage()); 
      case productDetails:
        final ProductDetails args = settings.arguments;
        return CupertinoPageRoute(
            builder: (_) =>
                ProductDetails(args.productsPojo, args.userId));
      case anotherPage:
        return MaterialPageRoute(builder: (_) => AnotherPage());   
        case splashScreen:
        return MaterialPageRoute(builder: (_) => SplashScreen());    
      default:
        return MaterialPageRoute(builder: (_) => UndefinedView(name: settings.name));
    }
  }
}

我为此苦苦挣扎了一段时间。 CupertinoTabView 有一个 'routes' 属性。将您的应用程序路由放在这里

return CupertinoTabView(
      routes: appRoutes,
      builder: (BuildContext context) {
        return CupertinoPageScaffold(
          navigationBar: CupertinoNavigationBar(
            middle: Text(
              titles[currentRoute]
            ),
            trailing: FlatButton(
              child: Icon(Icons.search, color: Colors.white,),
              onPressed: openSearch,
            ),
          ),
          child: Material(
            child: Center(
              child: routes[currentRoute],
            ),
          ),
        );
      },
    );

应用程序路由:

final appRoutes = {
  '/exampleRoute': (context) => ExampleRoute(),
  '/exampleRoute2': (context) => ExampleRoute2(),
}

您基本上必须复制您已经在 main.dart

中声明的路由