Skip to content

Instantly share code, notes, and snippets.

@haashem
Last active October 13, 2022 09:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haashem/3611cf1e66fa3b1c2e19f83310a987f6 to your computer and use it in GitHub Desktop.
Save haashem/3611cf1e66fa3b1c2e19f83310a987f6 to your computer and use it in GitHub Desktop.
AppRouter
extension MenuItemTypeFromString on MenuItemType {
static MenuItemType fromString(String value) {
if (value == Routes.signInAndSecurity.value) {
return MenuItemType.security;
} else if (value == Routes.personalInformation.value) {
return MenuItemType.information;
} else if (value == Routes.paymentMethods.value) {
return MenuItemType.payment;
} else if (value == Routes.familySharing.value) {
return MenuItemType.family;
} else if (value == Routes.devices.value) {
return MenuItemType.devices;
} else if (value == Routes.privacy.value) {
return MenuItemType.privacy;
} else {
return MenuItemType.security;
}
}
}
class AppRouter {
AppRouter._();
static final router = GoRouter(
initialLocation: '/',
routes: [
GoRoute(
path: '/',
redirect: (context, state) => state.namedLocation(Routes.home.value,
params: {'section': Routes.signInAndSecurity.name}),
),
GoRoute(
name: Routes.home.value,
path: '/account/manage/section/:section',
builder: (context, state) {
final section = state.params['section']!;
return HomePage(
key: state.pageKey,
tab: MenuItemTypeFromString.fromString(section));
},
),
// forwarding routes to remove the need to put the 'tab' param in the code
GoRoute(
name: Routes.signInAndSecurity.value,
path: '/security',
redirect: (context, state) => state.namedLocation(Routes.home.value,
params: {'section': Routes.signInAndSecurity.name}),
),
GoRoute(
name: Routes.personalInformation.value,
path: '/information',
redirect: (context, state) => state.namedLocation(Routes.home.value,
params: {'section': Routes.personalInformation.name}),
),
],
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment