Trying to add router with bottom navigation bar in CupertinoApp

Issue

I am trying to add router with bottom navigation bar in CupertinoApp, but Navigator.pushNamed(context,anotherPage) is giving error

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

but Navigator.push(context, CupertinoPageRoute(builder: (context)=>AnotherPage())); is working
Sample code:

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

//router 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));
    }
  }
}

Solution

I struggled with this for some time.
CupertinoTabView has a ‘routes’ property. Put your app routes here

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],
            ),
          ),
        );
      },
    );

appRoutes:

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

You’ll basically have to copy the routes you already declared in main.dart

Answered By – applejeewce

Answer Checked By – Willingham (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.