1

By following this thread I am not able to combine routes and BottomNavigationBar (How to use a named routes in a BottomNavigationBar in flutter?).

Any suggestion on how to make this work? I am trying to add a BottomNavigationBar for some of the defined routes. For the rest of the routes no BottomNavigationBar should be present.

void main() {
  runApp(Og1AlarmApp());
}

class Og1AlarmApp extends StatelessWidget {
  final Map<String, WidgetBuilder> _routes = {
    AppRoute.of(AppScreen.alarmList): (c) => AlarmListScreen(),
    AppRoute.of(AppScreen.alarmDetails): (c) => AlarmDetailsScreen(),
    AppRoute.of(AppScreen.alarm): (c) => AlarmScreen(),
    AppRoute.of(AppScreen.dashboard): (c) => DashboardScreen(),
  };

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'Og1 Alarm',
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        routes: _routes,
        initialRoute: AppRoute.of(AppScreen.alarmList),
        home: Scaffold(
          body: Center(
            child: _routes[AppRoute.of(AppScreen.alarmList)],
          ),
          bottomNavigationBar: BottomNavigationBar(
            items: <BottomNavigationBarItem>[
              BottomNavigationBarItem(icon: Icon(Icons.alarm_rounded)),
              BottomNavigationBarItem(icon: Icon(Icons.apps_rounded)),
              BottomNavigationBarItem(icon: Icon(Icons.analytics_rounded)),
            ],
            currentIndex: 0,
            selectedItemColor: Palette.gainsboro,
            unselectedItemColor: Palette.lightGray,
            onTap: (index) {
              switch (index) {
                case 0:
                  Navigator.pushNamed(
                      context, AppRoute.of(AppScreen.alarmList));
                  break;
                case 1:
                  Navigator.pushNamed(
                      context, AppRoute.of(AppScreen.dashboard));
                  break;
                case 2:
                  Navigator.pushNamed(
                      context, AppRoute.of(AppScreen.sleepMonitor));
                  break;
              }
            },
          ),
        ));
  }
}

Firstly in the above thread, routes are accessed somehow from the MaterialApp, which throws an error in my case. Secondly accessing the _routes variable as _routes[AppRoute.of(AppScreen.alarmList)] or_routes['/alarmList'] throws (The argument type 'Widget Function(BuildContext)?' can't be assigned to the parameter type 'Widget?')

Thanks for your review

1 Answers1

0

I was able to find a solution following the instructions here: https://www.vojtech.net/posts/flutter-bottom-navigation/

  • The provided link seems to be dead. Can you please answer your question with code examples and explanations? – Daniel May 31 '23 at 03:35