Last active
October 13, 2022 09:12
-
-
Save haashem/3611cf1e66fa3b1c2e19f83310a987f6 to your computer and use it in GitHub Desktop.
AppRouter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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