Created
September 1, 2021 13:52
-
-
Save stargazing-dino/ea933ac45e569050e49c21af944bd0e9 to your computer and use it in GitHub Desktop.
Some unconnected router setup
This file contains 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
class App extends HookConsumerWidget { | |
const App({Key? key}) : super(key: key); | |
@override | |
Widget build(BuildContext context, WidgetRef ref) { | |
final router = useMemoized(() => Router()); | |
final locale = ref.watch(localeProvider); | |
return MaterialApp.router( | |
debugShowCheckedModeBanner: !_kIsTakingScreenshots && kDebugMode, | |
themeMode: ref.watch(themeModeProvider), | |
locale: locale, | |
onGenerateTitle: (context) { | |
final localizations = AppLocalizations.of(context); | |
return localizations?.title ?? ''; | |
}, | |
localizationsDelegates: const [ | |
...AppLocalizations.localizationsDelegates, | |
LocaleNamesLocalizationsDelegate(), | |
], | |
supportedLocales: AppLocalizations.supportedLocales, | |
routerDelegate: AutoRouterDelegate( | |
router, | |
navigatorObservers: () => [AutoRouteObserver()], | |
), | |
routeInformationParser: router.defaultRouteParser(), | |
builder: (context, child) { | |
return UpgradeAlert(child: Unfocus(child: child)); | |
}, | |
); | |
} | |
} |
This file contains 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
class AppRouter extends HookWidget { | |
const AppRouter({Key? key, required this.user}) : super(key: key); | |
final User user; | |
@override | |
Widget build(BuildContext context) { | |
final notifier = useMemoized(() => UserNotifier(user), [user]); | |
return Material( | |
// FIXME: Why doesn't this have a material at this point? | |
// it also looks funky | |
type: MaterialType.transparency, | |
child: AutoRouter( | |
builder: (context, child) { | |
return HookConsumer( | |
builder: (context, ref, _) { | |
return ref.watch(maybeProfileDocProvider(user)).when( | |
data: (_profileDoc) { | |
if (_profileDoc.data() == null) { | |
return const NoProfile(); | |
} | |
return child; | |
}, | |
loading: defaultLoading, | |
error: defaultError, | |
); | |
}, | |
); | |
}, | |
), | |
); | |
} | |
} |
This file contains 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
final maybeUserProvider = StreamProvider.autoDispose<User?>((ref) { | |
return FirebaseAuth.instance.authStateChanges(); | |
}); |
This file contains 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
class AuthRouter extends HookConsumerWidget { | |
const AuthRouter({Key? key}) : super(key: key); | |
@override | |
Widget build(BuildContext context, WidgetRef ref) { | |
final maybeUserValue = ref.watch(maybeUserProvider); | |
return AutoRouter.declarative( | |
routes: (BuildContext context) { | |
return [ | |
maybeUserValue.when( | |
data: (user) { | |
if (user != null) { | |
return AppRouterRoute(user: user); | |
} else { | |
return const AuthPageRoute(); | |
} | |
}, | |
error: (error, stackTrace) => SplashPageRoute( | |
error: error, | |
stackTrace: stackTrace, | |
), | |
loading: () => SplashPageRoute(), | |
), | |
]; | |
}, | |
); | |
} | |
} |
This file contains 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
@AdaptiveAutoRouter( | |
// This is undocumented but it's the only way to get the router to not rebuild | |
// ignore: unnecessary_const | |
routes: const <AutoRoute>[ | |
AutoRoute<void>( | |
page: AuthRouter, | |
maintainState: true, | |
path: '/', | |
children: [ | |
AutoRoute<void>( | |
path: 'splash', | |
page: SplashPage, | |
), | |
AutoRoute<void>( | |
path: 'auth', | |
page: AuthPage, | |
), | |
AutoRoute<void>( | |
path: 'app', | |
initial: false, | |
page: AppRouter, | |
children: [ | |
AutoRoute<void>( | |
path: '', | |
page: HomePage, | |
initial: true, | |
children: [ | |
AutoRoute<void>( | |
path: 'practice', | |
page: PracticeTab, | |
), | |
AutoRoute<void>( | |
path: 'learn', | |
page: LearnTab, | |
), | |
AutoRoute<void>( | |
path: 'profile', | |
page: ProfileTab, | |
), | |
], | |
), | |
// TODO: Evaluate if this works as expected | |
AutoRoute<void>( | |
path: ':postId', | |
page: PostPage, | |
), | |
AutoRoute<void>( | |
path: ':groupId', | |
page: GroupPostPage, | |
children: [ | |
AutoRoute<void>( | |
path: ':postId', | |
page: PostPage, | |
), | |
], | |
), | |
AutoRoute<void>( | |
path: ':collectionId', | |
page: CollectionPage, | |
), | |
AutoRoute<void>( | |
path: ':collectionId/:groupId/:postId', | |
page: CollectionGroupPostPage, | |
), | |
AutoRoute<Post>( | |
path: 'add_post', | |
page: AddDialog, | |
fullscreenDialog: true, | |
), | |
AutoRoute<void>(path: 'encounter', page: EnounterPage), | |
AutoRoute<void>(path: 'about', page: AboutPage), | |
AutoRoute<void>(path: 'terms_of_use', page: TermsOfUsePage), | |
AutoRoute<void>(path: 'privacy_policy', page: PrivacyPolicyPage), | |
AutoRoute<void>(path: 'settings', page: SettingsPage), | |
AutoRoute<void>(path: 'purchase', page: PurchasePage), | |
], | |
), | |
], | |
), | |
AutoRoute<void>(path: '*', page: UnknownRoutePage), | |
], | |
) | |
class $Router {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment