nav.dart 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. import 'dart:async';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter_spinkit/flutter_spinkit.dart';
  4. import 'package:go_router/go_router.dart';
  5. import 'package:page_transition/page_transition.dart';
  6. import 'package:provider/provider.dart';
  7. import '/backend/schema/structs/index.dart';
  8. import '/auth/custom_auth/custom_auth_user_provider.dart';
  9. import '/main.dart';
  10. import '/flutter_flow/flutter_flow_theme.dart';
  11. import '/flutter_flow/lat_lng.dart';
  12. import '/flutter_flow/place.dart';
  13. import '/flutter_flow/flutter_flow_util.dart';
  14. import 'serialization_util.dart';
  15. import '/index.dart';
  16. export 'package:go_router/go_router.dart';
  17. export 'serialization_util.dart';
  18. const kTransitionInfoKey = '__transition_info__';
  19. GlobalKey<NavigatorState> appNavigatorKey = GlobalKey<NavigatorState>();
  20. const debugRouteLinkMap = {
  21. '/login':
  22. 'https://app.flutterflow.io/project/uitgaanskrant-1qhvtd?tab=uiBuilder&page=login',
  23. '/horecagelegenheidCurrent':
  24. 'https://app.flutterflow.io/project/uitgaanskrant-1qhvtd?tab=uiBuilder&page=horecagelegenheidCurrent',
  25. '/home':
  26. 'https://app.flutterflow.io/project/uitgaanskrant-1qhvtd?tab=uiBuilder&page=home',
  27. '/selectprovinciegemeente':
  28. 'https://app.flutterflow.io/project/uitgaanskrant-1qhvtd?tab=uiBuilder&page=selectprovinciegemeente',
  29. '/pUitgaanPage':
  30. 'https://app.flutterflow.io/project/uitgaanskrant-1qhvtd?tab=uiBuilder&page=PUitgaanPage',
  31. '/event':
  32. 'https://app.flutterflow.io/project/uitgaanskrant-1qhvtd?tab=uiBuilder&page=Event',
  33. '/eventCurrent':
  34. 'https://app.flutterflow.io/project/uitgaanskrant-1qhvtd?tab=uiBuilder&page=EventCurrent',
  35. '/horecagelegenhedenOverzichtProvinciePage':
  36. 'https://app.flutterflow.io/project/uitgaanskrant-1qhvtd?tab=uiBuilder&page=HorecagelegenhedenOverzichtProvinciePage',
  37. '/wachtwoordVergeten':
  38. 'https://app.flutterflow.io/project/uitgaanskrant-1qhvtd?tab=uiBuilder&page=wachtwoordVergeten',
  39. '/favorieten':
  40. 'https://app.flutterflow.io/project/uitgaanskrant-1qhvtd?tab=uiBuilder&page=favorieten',
  41. '/kanwegTestUpload':
  42. 'https://app.flutterflow.io/project/uitgaanskrant-1qhvtd?tab=uiBuilder&page=kanwegTestUpload',
  43. '/mijnProfiel':
  44. 'https://app.flutterflow.io/project/uitgaanskrant-1qhvtd?tab=uiBuilder&page=mijnProfiel',
  45. '/stadsactiviteitAanmaken':
  46. 'https://app.flutterflow.io/project/uitgaanskrant-1qhvtd?tab=uiBuilder&page=stadsactiviteitAanmaken',
  47. '/uitgaansevenementAanmaken':
  48. 'https://app.flutterflow.io/project/uitgaanskrant-1qhvtd?tab=uiBuilder&page=uitgaansevenementAanmaken',
  49. '/horecagelegenhedenOverzichtCurrent':
  50. 'https://app.flutterflow.io/project/uitgaanskrant-1qhvtd?tab=uiBuilder&page=horecagelegenhedenOverzichtCurrent',
  51. '/thuisBezorgen':
  52. 'https://app.flutterflow.io/project/uitgaanskrant-1qhvtd?tab=uiBuilder&page=thuisBezorgen'
  53. };
  54. class AppStateNotifier extends ChangeNotifier {
  55. AppStateNotifier._();
  56. static AppStateNotifier? _instance;
  57. static AppStateNotifier get instance => _instance ??= AppStateNotifier._();
  58. UitgaanskrantAuthUser? initialUser;
  59. UitgaanskrantAuthUser? user;
  60. bool showSplashImage = true;
  61. String? _redirectLocation;
  62. /// Determines whether the app will refresh and build again when a sign
  63. /// in or sign out happens. This is useful when the app is launched or
  64. /// on an unexpected logout. However, this must be turned off when we
  65. /// intend to sign in/out and then navigate or perform any actions after.
  66. /// Otherwise, this will trigger a refresh and interrupt the action(s).
  67. bool notifyOnAuthChange = true;
  68. bool get loading => user == null || showSplashImage;
  69. bool get loggedIn => user?.loggedIn ?? false;
  70. bool get initiallyLoggedIn => initialUser?.loggedIn ?? false;
  71. bool get shouldRedirect => loggedIn && _redirectLocation != null;
  72. String getRedirectLocation() => _redirectLocation!;
  73. bool hasRedirect() => _redirectLocation != null;
  74. void setRedirectLocationIfUnset(String loc) => _redirectLocation ??= loc;
  75. void clearRedirectLocation() => _redirectLocation = null;
  76. /// Mark as not needing to notify on a sign in / out when we intend
  77. /// to perform subsequent actions (such as navigation) afterwards.
  78. void updateNotifyOnAuthChange(bool notify) => notifyOnAuthChange = notify;
  79. void update(UitgaanskrantAuthUser newUser) {
  80. final shouldUpdate =
  81. user?.uid == null || newUser.uid == null || user?.uid != newUser.uid;
  82. initialUser ??= newUser;
  83. user = newUser;
  84. // Refresh the app on auth change unless explicitly marked otherwise.
  85. // No need to update unless the user has changed.
  86. if (notifyOnAuthChange && shouldUpdate) {
  87. notifyListeners();
  88. }
  89. // Once again mark the notifier as needing to update on auth change
  90. // (in order to catch sign in / out events).
  91. updateNotifyOnAuthChange(true);
  92. }
  93. void stopShowingSplashImage() {
  94. showSplashImage = false;
  95. notifyListeners();
  96. }
  97. }
  98. GoRouter createRouter(AppStateNotifier appStateNotifier) => GoRouter(
  99. initialLocation: '/',
  100. debugLogDiagnostics: true,
  101. refreshListenable: appStateNotifier,
  102. navigatorKey: appNavigatorKey,
  103. errorBuilder: (context, state) =>
  104. appStateNotifier.loggedIn ? HomeWidget() : HomeWidget(),
  105. routes: [
  106. FFRoute(
  107. name: '_initialize',
  108. path: '/',
  109. builder: (context, _) =>
  110. appStateNotifier.loggedIn ? HomeWidget() : HomeWidget(),
  111. ),
  112. FFRoute(
  113. name: LoginWidget.routeName,
  114. path: LoginWidget.routePath,
  115. builder: (context, params) => LoginWidget(),
  116. ),
  117. FFRoute(
  118. name: HorecagelegenheidCurrentWidget.routeName,
  119. path: HorecagelegenheidCurrentWidget.routePath,
  120. builder: (context, params) => HorecagelegenheidCurrentWidget(
  121. nid: params.getParam(
  122. 'nid',
  123. ParamType.String,
  124. ),
  125. ),
  126. ),
  127. FFRoute(
  128. name: HomeWidget.routeName,
  129. path: HomeWidget.routePath,
  130. builder: (context, params) => HomeWidget(),
  131. ),
  132. FFRoute(
  133. name: SelectprovinciegemeenteWidget.routeName,
  134. path: SelectprovinciegemeenteWidget.routePath,
  135. builder: (context, params) => SelectprovinciegemeenteWidget(),
  136. ),
  137. FFRoute(
  138. name: PUitgaanPageWidget.routeName,
  139. path: PUitgaanPageWidget.routePath,
  140. builder: (context, params) => PUitgaanPageWidget(
  141. plaats: params.getParam(
  142. 'plaats',
  143. ParamType.String,
  144. ),
  145. services: params.getParam(
  146. 'services',
  147. ParamType.String,
  148. ),
  149. ),
  150. ),
  151. FFRoute(
  152. name: EventWidget.routeName,
  153. path: EventWidget.routePath,
  154. builder: (context, params) => EventWidget(
  155. nid: params.getParam(
  156. 'nid',
  157. ParamType.String,
  158. ),
  159. ),
  160. ),
  161. FFRoute(
  162. name: EventCurrentWidget.routeName,
  163. path: EventCurrentWidget.routePath,
  164. builder: (context, params) => EventCurrentWidget(
  165. nid: params.getParam(
  166. 'nid',
  167. ParamType.String,
  168. ),
  169. horecaid: params.getParam(
  170. 'horecaid',
  171. ParamType.String,
  172. ),
  173. ),
  174. ),
  175. FFRoute(
  176. name: HorecagelegenhedenOverzichtProvinciePageWidget.routeName,
  177. path: HorecagelegenhedenOverzichtProvinciePageWidget.routePath,
  178. builder: (context, params) =>
  179. HorecagelegenhedenOverzichtProvinciePageWidget(
  180. plaats: params.getParam(
  181. 'plaats',
  182. ParamType.String,
  183. ),
  184. ),
  185. ),
  186. FFRoute(
  187. name: WachtwoordVergetenWidget.routeName,
  188. path: WachtwoordVergetenWidget.routePath,
  189. builder: (context, params) => WachtwoordVergetenWidget(),
  190. ),
  191. FFRoute(
  192. name: FavorietenWidget.routeName,
  193. path: FavorietenWidget.routePath,
  194. builder: (context, params) => FavorietenWidget(),
  195. ),
  196. FFRoute(
  197. name: KanwegTestUploadWidget.routeName,
  198. path: KanwegTestUploadWidget.routePath,
  199. builder: (context, params) => KanwegTestUploadWidget(),
  200. ),
  201. FFRoute(
  202. name: MijnProfielWidget.routeName,
  203. path: MijnProfielWidget.routePath,
  204. builder: (context, params) => MijnProfielWidget(),
  205. ),
  206. FFRoute(
  207. name: StadsactiviteitAanmakenWidget.routeName,
  208. path: StadsactiviteitAanmakenWidget.routePath,
  209. builder: (context, params) => StadsactiviteitAanmakenWidget(),
  210. ),
  211. FFRoute(
  212. name: UitgaansevenementAanmakenWidget.routeName,
  213. path: UitgaansevenementAanmakenWidget.routePath,
  214. builder: (context, params) => UitgaansevenementAanmakenWidget(
  215. horecagelegenheidNid: params.getParam(
  216. 'horecagelegenheidNid',
  217. ParamType.String,
  218. ),
  219. ),
  220. ),
  221. FFRoute(
  222. name: HorecagelegenhedenOverzichtCurrentWidget.routeName,
  223. path: HorecagelegenhedenOverzichtCurrentWidget.routePath,
  224. builder: (context, params) =>
  225. HorecagelegenhedenOverzichtCurrentWidget(
  226. plaats: params.getParam(
  227. 'plaats',
  228. ParamType.String,
  229. ),
  230. ),
  231. ),
  232. FFRoute(
  233. name: ThuisBezorgenWidget.routeName,
  234. path: ThuisBezorgenWidget.routePath,
  235. builder: (context, params) => ThuisBezorgenWidget(
  236. plaats: params.getParam(
  237. 'plaats',
  238. ParamType.String,
  239. ),
  240. ),
  241. )
  242. ].map((r) => r.toRoute(appStateNotifier)).toList(),
  243. observers: ffNavigatorObservers,
  244. );
  245. extension NavParamExtensions on Map<String, String?> {
  246. Map<String, String> get withoutNulls => Map.fromEntries(
  247. entries
  248. .where((e) => e.value != null)
  249. .map((e) => MapEntry(e.key, e.value!)),
  250. );
  251. }
  252. extension NavigationExtensions on BuildContext {
  253. void goNamedAuth(
  254. String name,
  255. bool mounted, {
  256. Map<String, String> pathParameters = const <String, String>{},
  257. Map<String, String> queryParameters = const <String, String>{},
  258. Object? extra,
  259. bool ignoreRedirect = false,
  260. }) =>
  261. !mounted || GoRouter.of(this).shouldRedirect(ignoreRedirect)
  262. ? null
  263. : goNamed(
  264. name,
  265. pathParameters: pathParameters,
  266. queryParameters: queryParameters,
  267. extra: extra,
  268. );
  269. void pushNamedAuth(
  270. String name,
  271. bool mounted, {
  272. Map<String, String> pathParameters = const <String, String>{},
  273. Map<String, String> queryParameters = const <String, String>{},
  274. Object? extra,
  275. bool ignoreRedirect = false,
  276. }) =>
  277. !mounted || GoRouter.of(this).shouldRedirect(ignoreRedirect)
  278. ? null
  279. : pushNamed(
  280. name,
  281. pathParameters: pathParameters,
  282. queryParameters: queryParameters,
  283. extra: extra,
  284. );
  285. void safePop() {
  286. // If there is only one route on the stack, navigate to the initial
  287. // page instead of popping.
  288. if (canPop()) {
  289. pop();
  290. } else {
  291. go('/');
  292. }
  293. }
  294. }
  295. extension GoRouterExtensions on GoRouter {
  296. AppStateNotifier get appState => AppStateNotifier.instance;
  297. void prepareAuthEvent([bool ignoreRedirect = false]) =>
  298. appState.hasRedirect() && !ignoreRedirect
  299. ? null
  300. : appState.updateNotifyOnAuthChange(false);
  301. bool shouldRedirect(bool ignoreRedirect) =>
  302. !ignoreRedirect && appState.hasRedirect();
  303. void clearRedirectLocation() => appState.clearRedirectLocation();
  304. void setRedirectLocationIfUnset(String location) =>
  305. appState.updateNotifyOnAuthChange(false);
  306. }
  307. extension _GoRouterStateExtensions on GoRouterState {
  308. Map<String, dynamic> get extraMap =>
  309. extra != null ? extra as Map<String, dynamic> : {};
  310. Map<String, dynamic> get allParams => <String, dynamic>{}
  311. ..addAll(pathParameters)
  312. ..addAll(uri.queryParameters)
  313. ..addAll(extraMap);
  314. TransitionInfo get transitionInfo => extraMap.containsKey(kTransitionInfoKey)
  315. ? extraMap[kTransitionInfoKey] as TransitionInfo
  316. : TransitionInfo.appDefault();
  317. }
  318. class FFParameters {
  319. FFParameters(this.state, [this.asyncParams = const {}]);
  320. final GoRouterState state;
  321. final Map<String, Future<dynamic> Function(String)> asyncParams;
  322. Map<String, dynamic> futureParamValues = {};
  323. // Parameters are empty if the params map is empty or if the only parameter
  324. // present is the special extra parameter reserved for the transition info.
  325. bool get isEmpty =>
  326. state.allParams.isEmpty ||
  327. (state.allParams.length == 1 &&
  328. state.extraMap.containsKey(kTransitionInfoKey));
  329. bool isAsyncParam(MapEntry<String, dynamic> param) =>
  330. asyncParams.containsKey(param.key) && param.value is String;
  331. bool get hasFutures => state.allParams.entries.any(isAsyncParam);
  332. Future<bool> completeFutures() => Future.wait(
  333. state.allParams.entries.where(isAsyncParam).map(
  334. (param) async {
  335. final doc = await asyncParams[param.key]!(param.value)
  336. .onError((_, __) => null);
  337. if (doc != null) {
  338. futureParamValues[param.key] = doc;
  339. return true;
  340. }
  341. return false;
  342. },
  343. ),
  344. ).onError((_, __) => [false]).then((v) => v.every((e) => e));
  345. dynamic getParam<T>(
  346. String paramName,
  347. ParamType type, {
  348. bool isList = false,
  349. StructBuilder<T>? structBuilder,
  350. }) {
  351. if (futureParamValues.containsKey(paramName)) {
  352. return futureParamValues[paramName];
  353. }
  354. if (!state.allParams.containsKey(paramName)) {
  355. return null;
  356. }
  357. final param = state.allParams[paramName];
  358. // Got parameter from `extras`, so just directly return it.
  359. if (param is! String) {
  360. return param;
  361. }
  362. // Return serialized value.
  363. return deserializeParam<T>(
  364. param,
  365. type,
  366. isList,
  367. structBuilder: structBuilder,
  368. );
  369. }
  370. }
  371. class FFRoute {
  372. const FFRoute({
  373. required this.name,
  374. required this.path,
  375. required this.builder,
  376. this.requireAuth = false,
  377. this.asyncParams = const {},
  378. this.routes = const [],
  379. });
  380. final String name;
  381. final String path;
  382. final bool requireAuth;
  383. final Map<String, Future<dynamic> Function(String)> asyncParams;
  384. final Widget Function(BuildContext, FFParameters) builder;
  385. final List<GoRoute> routes;
  386. GoRoute toRoute(AppStateNotifier appStateNotifier) => GoRoute(
  387. name: name,
  388. path: path,
  389. redirect: (context, state) {
  390. if (appStateNotifier.shouldRedirect) {
  391. final redirectLocation = appStateNotifier.getRedirectLocation();
  392. appStateNotifier.clearRedirectLocation();
  393. return redirectLocation;
  394. }
  395. if (requireAuth && !appStateNotifier.loggedIn) {
  396. appStateNotifier.setRedirectLocationIfUnset(state.uri.toString());
  397. return '/home';
  398. }
  399. return null;
  400. },
  401. pageBuilder: (context, state) {
  402. fixStatusBarOniOS16AndBelow(context);
  403. final ffParams = FFParameters(state, asyncParams);
  404. final page = ffParams.hasFutures
  405. ? FutureBuilder(
  406. future: ffParams.completeFutures(),
  407. builder: (context, _) => builder(context, ffParams),
  408. )
  409. : builder(context, ffParams);
  410. final child = appStateNotifier.loading
  411. ? Center(
  412. child: SizedBox(
  413. width: 80.0,
  414. height: 80.0,
  415. child: SpinKitFadingCircle(
  416. color: FlutterFlowTheme.of(context).primary,
  417. size: 80.0,
  418. ),
  419. ),
  420. )
  421. : page;
  422. final transitionInfo = state.transitionInfo;
  423. return transitionInfo.hasTransition
  424. ? CustomTransitionPage(
  425. key: state.pageKey,
  426. name: state.name,
  427. child: child,
  428. transitionDuration: transitionInfo.duration,
  429. transitionsBuilder:
  430. (context, animation, secondaryAnimation, child) =>
  431. PageTransition(
  432. type: transitionInfo.transitionType,
  433. duration: transitionInfo.duration,
  434. reverseDuration: transitionInfo.duration,
  435. alignment: transitionInfo.alignment,
  436. child: child,
  437. ).buildTransitions(
  438. context,
  439. animation,
  440. secondaryAnimation,
  441. child,
  442. ),
  443. )
  444. : MaterialPage(
  445. key: state.pageKey, name: state.name, child: child);
  446. },
  447. routes: routes,
  448. );
  449. }
  450. class TransitionInfo {
  451. const TransitionInfo({
  452. required this.hasTransition,
  453. this.transitionType = PageTransitionType.fade,
  454. this.duration = const Duration(milliseconds: 300),
  455. this.alignment,
  456. });
  457. final bool hasTransition;
  458. final PageTransitionType transitionType;
  459. final Duration duration;
  460. final Alignment? alignment;
  461. static TransitionInfo appDefault() => TransitionInfo(hasTransition: false);
  462. }
  463. class RootPageContext {
  464. const RootPageContext(this.isRootPage, [this.errorRoute]);
  465. final bool isRootPage;
  466. final String? errorRoute;
  467. static bool isInactiveRootPage(BuildContext context) {
  468. final rootPageContext = context.read<RootPageContext?>();
  469. final isRootPage = rootPageContext?.isRootPage ?? false;
  470. final location = GoRouterState.of(context).uri.toString();
  471. return isRootPage &&
  472. location != '/' &&
  473. location != rootPageContext?.errorRoute;
  474. }
  475. static Widget wrap(Widget child, {String? errorRoute}) => Provider.value(
  476. value: RootPageContext(true, errorRoute),
  477. child: child,
  478. );
  479. }
  480. extension GoRouterLocationExtension on GoRouter {
  481. String getCurrentLocation() {
  482. final RouteMatch lastMatch = routerDelegate.currentConfiguration.last;
  483. final RouteMatchList matchList = lastMatch is ImperativeRouteMatch
  484. ? lastMatch.matches
  485. : routerDelegate.currentConfiguration;
  486. return matchList.uri.toString();
  487. }
  488. }