routerTemplate top-level constant
String
const routerTemplate
routerTemplate
this will be use in order to create router.dart for export and import
Implementation
const String routerTemplate = """import 'package:projectName/core.dart';\n
/// ### `MyRouter`
///
/// `Description`: the main class for route management
///
class MyRouter {
late AuthenticationController authenticationController;
MyRouter(this.authenticationController);
late final router = GoRouter(
refreshListenable: authenticationController,
debugLogDiagnostics: true,
urlPathStrategy: UrlPathStrategy.path,
initialLocation: rootRoutePath,
routes: [
// TODO: HomeScreen.router,
GoRoute(
name: rootRouteName,
path: rootRoutePath,
pageBuilder: (_, state) => NoTransitionPage(
key: state.pageKey,
child: const HomeScreen(key: HomeScreen.pageKey)),
routes: [
/// nested routes
],
/// redirect: redirect,
),
// TODO: LoginScreen.router,
GoRoute(
name: loginRouteName,
path: loginRoutePath,
pageBuilder: (_, state) => NoTransitionPage(
key: state.pageKey,
child: const LoginScreen(key: LoginScreen.pageKey)),
routes: [
/// nested routes
],
/// redirect: redirect,
),
// TODO: ResetScreen.router,
GoRoute(
name: resetRouteName,
path: resetRoutePath,
pageBuilder: (_, state) => NoTransitionPage(
key: state.pageKey,
child: const ResetScreen(key: ResetScreen.pageKey)),
routes: [
/// nested routes
],
/// redirect: redirect,
),
// TODO: RegisterScreen.router,
GoRoute(
name: registerRouteName,
path: registerRoutePath,
pageBuilder: (_, state) => NoTransitionPage(
key: state.pageKey,
child: const RegisterScreen(key: RegisterScreen.pageKey)),
routes: [
/// nested routes
],
/// redirect: redirect,
),
// TODO: DashboardScreen.router,
GoRoute(
name: adminRouteName,
path: adminRoutePath,
pageBuilder: (_, state) => NoTransitionPage(
key: state.pageKey,
child: const Center(child: CircularProgressIndicator())),
routes: [
GoRoute(
name: dashboardRouteName,
path: dashboardRoutePath,
pageBuilder: (_, state) => const NoTransitionPage(
child: DashboardScreen(key: DashboardScreen.pageKey)),
routes: [
/// nested routes
],
/// redirect: redirect,
),
GoRoute(
name: profileRouteName,
path: profileRoutePath,
pageBuilder: (_, state) => const NoTransitionPage(
child: ProfileScreen(key: ProfileScreen.pageKey)),
routes: [
/// nested routes
],
/// redirect: redirect,
),
],
redirect: (state) {
final dashboardLocation = state.namedLocation(dashboardRouteName);
return dashboardLocation;
},
),
],
/// handle error
errorBuilder: (context, state) => Scaffold(
body: Center(
child: Text(state.error.toString()),
),
),
// TODO: handle redirect or middlewar
redirect: (state) {
/// if user is in login screen
final loginLocation = state.namedLocation(loginRouteName);
final loggingIn = state.subloc == loginLocation;
/// if user is in register screen
final registerAccountLocation = state.namedLocation(registerRouteName);
final registeringAccount = state.subloc == registerAccountLocation;
final isLoggedIn = authenticationController.isLoggedIn;
final rootLocation = state.namedLocation(rootRouteName);
/// if not logged-in nor in loggin page nor in register page then go to login
if (!isLoggedIn && !loggingIn && !registeringAccount) {
return loginLocation;
}
/// if login and but sticted in login or register page, then go to api dashboard
if (isLoggedIn && (loggingIn || registeringAccount)) {
return rootLocation;
}
return null;
},
);
}
""";