routerProvider top-level property

Provider<GoRouter> routerProvider
final

Provides a router configuration that builds the routing structure of the Homeowner app.

The router configuration uses go_router under the hood, but it is recommended to use the routing tools in shared/values/routes.dart for better type-safe routing.

Implementation

final routerProvider = Provider<GoRouter>(
  (ref) {
    final rootNavigatorKey = ref.watch(navigatorKeyProvider);

    return GoRouter(
      navigatorKey: rootNavigatorKey,
      initialLocation: '/dashboard',
      redirect: (context, state) {
        final currentUser = InheritedUser.of(context);

        if (currentUser == null) {
          return AppRoute.onboarding.path;
        } else {
          final hasAcceptedCredentialTerms =
              ref.watch(hasAcceptedCredentialTermsProvider);
          if (!hasAcceptedCredentialTerms) {
            return AppRoute.credentialTermsScreen.path;
          }
          if (state.fullPath == AppRoute.onboarding.path) {
            return AppRoute.dashboard.path;
          }
        }

        return null;
      },
      routes: [
        StatefulShellRoute(
          builder: (context, state, shell) => shell,
          navigatorContainerBuilder: (context, shell, children) {
            return HomeownerScreen(
              shell: shell,
              children: children,
            );
          },
          // Branches must be ordered based on the order of AppBranch
          branches: [
            StatefulShellBranch(
              routes: [
                GoRoute(
                  path: '/dashboard',
                  builder: (context, state) => const DashboardPage(),
                  routes: [
                    GoRoute(
                      path: 'add_generator',
                      parentNavigatorKey: rootNavigatorKey,
                      builder: (context, state) =>
                          const GeneratorAdditionScreen(),
                    ),
                    GoRoute(
                      path: ':id/events',
                      builder: (context, state) => EventLogPage(
                        id: int.parse(state.pathParameters['id']!),
                      ),
                    ),
                    GoRoute(
                      path: ':id/maintenanceHistory',
                      builder: (context, state) => MaintenanceHistoryPage(
                        id: int.parse(state.pathParameters['id']!),
                      ),
                    ),
                  ],
                ),
              ],
            ),
            StatefulShellBranch(
              routes: [
                GoRoute(
                  path: '/notifications',
                  builder: (context, state) => const NotificationsPage(),
                ),
              ],
            ),
            StatefulShellBranch(
              routes: [
                GoRoute(
                  path: '/profile',
                  builder: (context, state) => const ProfilePage(),
                  routes: [
                    GoRoute(
                      path: 'settings',
                      parentNavigatorKey: rootNavigatorKey,
                      builder: (context, state) => Scaffold(
                        appBar: AppBar(title: Text(context.l10n.settings)),
                        body: Center(child: Text(context.l10n.settings)),
                      ),
                    ),
                    GoRoute(
                      path: 'contactDetails',
                      builder: (context, state) => const ContactDetailsPage(),
                    ),
                    GoRoute(
                      path: 'termsandconditions',
                      builder: (context, state) =>
                          const TermsAndConditionsPage(),
                    ),
                    GoRoute(
                      path: 'locationsUsers',
                      builder: (context, state) => const LocationUsersPage(),
                    ),
                  ],
                ),
              ],
            ),
          ],
        ),
        // New route for generator addition
        GoRoute(
          path: '/onboarding',
          builder: (context, state) => const OnboardingScreen(),
        ),
        GoRoute(
          path: '/credentialTermsScreen',
          builder: (context, state) => const CredentialTermsScreen(),
        ),
      ],
    );
  },
);