1

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

在 _CupertinoTabViewState 中找不到路由 RouteSettings("/anotherPage", null) 的生成器。

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 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));
    }
  }
}
4

1 回答 1

3

我为此挣扎了一段时间。CupertinoTabView 有一个“路线”属性。将您的应用路线放在这里

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 中声明的路线

于 2020-07-27T18:35:48.930 回答