Showing
10 changed files
with
178 additions
and
101 deletions
| @@ -5,24 +5,22 @@ import '../routes/app_pages.dart'; | @@ -5,24 +5,22 @@ import '../routes/app_pages.dart'; | ||
| 5 | 5 | ||
| 6 | class EnsureAuthMiddleware extends GetMiddleware { | 6 | class EnsureAuthMiddleware extends GetMiddleware { | 
| 7 | @override | 7 | @override | 
| 8 | - Future<RouteDecoder?> redirect(RouteDecoder route) async { | 8 | + Future<RouteDecoder?> redirectDelegate(RouteDecoder route) async { | 
| 9 | // you can do whatever you want here | 9 | // you can do whatever you want here | 
| 10 | // but it's preferable to make this method fast | 10 | // but it's preferable to make this method fast | 
| 11 | // await Future.delayed(Duration(milliseconds: 500)); | 11 | // await Future.delayed(Duration(milliseconds: 500)); | 
| 12 | 12 | ||
| 13 | if (!AuthService.to.isLoggedInValue) { | 13 | if (!AuthService.to.isLoggedInValue) { | 
| 14 | - final path = route.pageSettings?.name as String; | ||
| 15 | - final newRoute = Routes.LOGIN_THEN(path); | ||
| 16 | - | 14 | + final newRoute = Routes.LOGIN_THEN(route.pageSettings!.name); | 
| 17 | return RouteDecoder.fromRoute(newRoute); | 15 | return RouteDecoder.fromRoute(newRoute); | 
| 18 | } | 16 | } | 
| 19 | - return super.redirect(route); | 17 | + return await super.redirectDelegate(route); | 
| 20 | } | 18 | } | 
| 21 | } | 19 | } | 
| 22 | 20 | ||
| 23 | class EnsureNotAuthedMiddleware extends GetMiddleware { | 21 | class EnsureNotAuthedMiddleware extends GetMiddleware { | 
| 24 | @override | 22 | @override | 
| 25 | - Future<RouteDecoder?> redirect(RouteDecoder route) async { | 23 | + Future<RouteDecoder?> redirectDelegate(RouteDecoder route) async { | 
| 26 | if (AuthService.to.isLoggedInValue) { | 24 | if (AuthService.to.isLoggedInValue) { | 
| 27 | //NEVER navigate to auth screen, when user is already authed | 25 | //NEVER navigate to auth screen, when user is already authed | 
| 28 | return null; | 26 | return null; | 
| @@ -30,6 +28,6 @@ class EnsureNotAuthedMiddleware extends GetMiddleware { | @@ -30,6 +28,6 @@ class EnsureNotAuthedMiddleware extends GetMiddleware { | ||
| 30 | //OR redirect user to another screen | 28 | //OR redirect user to another screen | 
| 31 | //return RouteDecoder.fromRoute(Routes.PROFILE); | 29 | //return RouteDecoder.fromRoute(Routes.PROFILE); | 
| 32 | } | 30 | } | 
| 33 | - return super.redirect(route); | 31 | + return await super.redirectDelegate(route); | 
| 34 | } | 32 | } | 
| 35 | } | 33 | } | 
| @@ -8,7 +8,9 @@ class HomeView extends GetView<HomeController> { | @@ -8,7 +8,9 @@ class HomeView extends GetView<HomeController> { | ||
| 8 | @override | 8 | @override | 
| 9 | Widget build(BuildContext context) { | 9 | Widget build(BuildContext context) { | 
| 10 | return GetRouterOutlet.builder( | 10 | return GetRouterOutlet.builder( | 
| 11 | - builder: (context, delegate, currentRoute) { | 11 | + routerDelegate: Get.nestedKey(Routes.HOME), | 
| 12 | + builder: (context) { | ||
| 13 | + final delegate = context.navigation; | ||
| 12 | //This router outlet handles the appbar and the bottom navigation bar | 14 | //This router outlet handles the appbar and the bottom navigation bar | 
| 13 | final currentLocation = context.location; | 15 | final currentLocation = context.location; | 
| 14 | var currentIndex = 0; | 16 | var currentIndex = 0; | 
| @@ -22,6 +24,7 @@ class HomeView extends GetView<HomeController> { | @@ -22,6 +24,7 @@ class HomeView extends GetView<HomeController> { | ||
| 22 | body: GetRouterOutlet( | 24 | body: GetRouterOutlet( | 
| 23 | initialRoute: Routes.DASHBOARD, | 25 | initialRoute: Routes.DASHBOARD, | 
| 24 | anchorRoute: Routes.HOME, | 26 | anchorRoute: Routes.HOME, | 
| 27 | + //delegate: Get.nestedKey(Routes.HOME), | ||
| 25 | // key: Get.nestedKey(Routes.HOME), | 28 | // key: Get.nestedKey(Routes.HOME), | 
| 26 | ), | 29 | ), | 
| 27 | bottomNavigationBar: BottomNavigationBar( | 30 | bottomNavigationBar: BottomNavigationBar( | 
| @@ -8,8 +8,9 @@ import 'drawer.dart'; | @@ -8,8 +8,9 @@ import 'drawer.dart'; | ||
| 8 | class RootView extends GetView<RootController> { | 8 | class RootView extends GetView<RootController> { | 
| 9 | @override | 9 | @override | 
| 10 | Widget build(BuildContext context) { | 10 | Widget build(BuildContext context) { | 
| 11 | - return GetRouterOutlet.builder( | ||
| 12 | - builder: (context, delegate, current) { | 11 | + return RouterOutlet.builder( | 
| 12 | + delegate: Get.nestedKey(null), | ||
| 13 | + builder: (context) { | ||
| 13 | final title = context.location; | 14 | final title = context.location; | 
| 14 | return Scaffold( | 15 | return Scaffold( | 
| 15 | drawer: DrawerWidget(), | 16 | drawer: DrawerWidget(), | 
| @@ -17,12 +18,15 @@ class RootView extends GetView<RootController> { | @@ -17,12 +18,15 @@ class RootView extends GetView<RootController> { | ||
| 17 | title: Text(title), | 18 | title: Text(title), | 
| 18 | centerTitle: true, | 19 | centerTitle: true, | 
| 19 | ), | 20 | ), | 
| 21 | + //body: HomeView(), | ||
| 22 | + | ||
| 20 | body: GetRouterOutlet( | 23 | body: GetRouterOutlet( | 
| 21 | initialRoute: Routes.HOME, | 24 | initialRoute: Routes.HOME, | 
| 25 | + delegate: Get.nestedKey(null), | ||
| 22 | anchorRoute: '/', | 26 | anchorRoute: '/', | 
| 23 | - // filterPages: (afterAnchor) { | ||
| 24 | - // return afterAnchor.take(1); | ||
| 25 | - // }, | 27 | + filterPages: (afterAnchor) { | 
| 28 | + return afterAnchor.take(1); | ||
| 29 | + }, | ||
| 26 | ), | 30 | ), | 
| 27 | ); | 31 | ); | 
| 28 | }, | 32 | }, | 
| @@ -2,13 +2,12 @@ import 'package:flutter/material.dart'; | @@ -2,13 +2,12 @@ import 'package:flutter/material.dart'; | ||
| 2 | import 'package:get/get.dart'; | 2 | import 'package:get/get.dart'; | 
| 3 | 3 | ||
| 4 | import 'app/modules/splash/controllers/splash_service.dart'; | 4 | import 'app/modules/splash/controllers/splash_service.dart'; | 
| 5 | -import 'app/modules/splash/views/splash_view.dart'; | ||
| 6 | import 'app/routes/app_pages.dart'; | 5 | import 'app/routes/app_pages.dart'; | 
| 7 | import 'services/auth_service.dart'; | 6 | import 'services/auth_service.dart'; | 
| 8 | 7 | ||
| 9 | void main() { | 8 | void main() { | 
| 10 | runApp( | 9 | runApp( | 
| 11 | - GetMaterialApp.router( | 10 | + GetMaterialApp( | 
| 12 | title: "Application", | 11 | title: "Application", | 
| 13 | initialBinding: BindingsBuilder( | 12 | initialBinding: BindingsBuilder( | 
| 14 | () { | 13 | () { | 
| @@ -17,18 +16,19 @@ void main() { | @@ -17,18 +16,19 @@ void main() { | ||
| 17 | }, | 16 | }, | 
| 18 | ), | 17 | ), | 
| 19 | getPages: AppPages.routes, | 18 | getPages: AppPages.routes, | 
| 20 | - builder: (context, child) { | ||
| 21 | - return FutureBuilder<void>( | ||
| 22 | - key: ValueKey('initFuture'), | ||
| 23 | - future: Get.find<SplashService>().init(), | ||
| 24 | - builder: (context, snapshot) { | ||
| 25 | - if (snapshot.connectionState == ConnectionState.done) { | ||
| 26 | - return child ?? SizedBox.shrink(); | ||
| 27 | - } | ||
| 28 | - return SplashView(); | ||
| 29 | - }, | ||
| 30 | - ); | ||
| 31 | - }, | 19 | + initialRoute: AppPages.INITIAL, | 
| 20 | + // builder: (context, child) { | ||
| 21 | + // return FutureBuilder<void>( | ||
| 22 | + // key: ValueKey('initFuture'), | ||
| 23 | + // future: Get.find<SplashService>().init(), | ||
| 24 | + // builder: (context, snapshot) { | ||
| 25 | + // if (snapshot.connectionState == ConnectionState.done) { | ||
| 26 | + // return child ?? SizedBox.shrink(); | ||
| 27 | + // } | ||
| 28 | + // return SplashView(); | ||
| 29 | + // }, | ||
| 30 | + // ); | ||
| 31 | + // }, | ||
| 32 | // routeInformationParser: GetInformationParser( | 32 | // routeInformationParser: GetInformationParser( | 
| 33 | // // initialRoute: Routes.HOME, | 33 | // // initialRoute: Routes.HOME, | 
| 34 | // ), | 34 | // ), | 
| @@ -576,7 +576,7 @@ extension GetNavigationExt on GetInterface { | @@ -576,7 +576,7 @@ extension GetNavigationExt on GetInterface { | ||
| 576 | Future<T?>? toNamed<T>( | 576 | Future<T?>? toNamed<T>( | 
| 577 | String page, { | 577 | String page, { | 
| 578 | dynamic arguments, | 578 | dynamic arguments, | 
| 579 | - int? id, | 579 | + dynamic id, | 
| 580 | bool preventDuplicates = true, | 580 | bool preventDuplicates = true, | 
| 581 | Map<String, String>? parameters, | 581 | Map<String, String>? parameters, | 
| 582 | }) { | 582 | }) { | 
| @@ -1078,7 +1078,7 @@ extension GetNavigationExt on GetInterface { | @@ -1078,7 +1078,7 @@ extension GetNavigationExt on GetInterface { | ||
| 1078 | 1078 | ||
| 1079 | GetDelegate? nestedKey(String? key) { | 1079 | GetDelegate? nestedKey(String? key) { | 
| 1080 | if (key == null) { | 1080 | if (key == null) { | 
| 1081 | - throw 'You need to define a ID'; | 1081 | + return routerDelegate as GetDelegate; | 
| 1082 | } | 1082 | } | 
| 1083 | keys.putIfAbsent( | 1083 | keys.putIfAbsent( | 
| 1084 | key, | 1084 | key, | 
| @@ -1091,7 +1091,7 @@ extension GetNavigationExt on GetInterface { | @@ -1091,7 +1091,7 @@ extension GetNavigationExt on GetInterface { | ||
| 1091 | return keys[key]; | 1091 | return keys[key]; | 
| 1092 | } | 1092 | } | 
| 1093 | 1093 | ||
| 1094 | - GetDelegate searchDelegate(int? k) { | 1094 | + GetDelegate searchDelegate(dynamic k) { | 
| 1095 | GetDelegate _key; | 1095 | GetDelegate _key; | 
| 1096 | if (k == null) { | 1096 | if (k == null) { | 
| 1097 | _key = Get.rootController.rootDelegate; | 1097 | _key = Get.rootController.rootDelegate; | 
| @@ -107,7 +107,7 @@ class GetDelegate extends RouterDelegate<RouteDecoder> | @@ -107,7 +107,7 @@ class GetDelegate extends RouterDelegate<RouteDecoder> | ||
| 107 | } | 107 | } | 
| 108 | var iterator = config; | 108 | var iterator = config; | 
| 109 | for (var item in middlewares) { | 109 | for (var item in middlewares) { | 
| 110 | - var redirectRes = await item.redirect(iterator); | 110 | + var redirectRes = await item.redirectDelegate(iterator); | 
| 111 | if (redirectRes == null) return null; | 111 | if (redirectRes == null) return null; | 
| 112 | iterator = redirectRes; | 112 | iterator = redirectRes; | 
| 113 | } | 113 | } | 
| @@ -312,7 +312,7 @@ class GetDelegate extends RouterDelegate<RouteDecoder> | @@ -312,7 +312,7 @@ class GetDelegate extends RouterDelegate<RouteDecoder> | ||
| 312 | if (clearPages) _activePages.clear(); | 312 | if (clearPages) _activePages.clear(); | 
| 313 | 313 | ||
| 314 | final pageSettings = _buildPageSettings(notFoundRoute.name); | 314 | final pageSettings = _buildPageSettings(notFoundRoute.name); | 
| 315 | - final routeDecoder = await _getRouteDecoder(pageSettings); | 315 | + final routeDecoder = _getRouteDecoder(pageSettings); | 
| 316 | 316 | ||
| 317 | _push(routeDecoder!); | 317 | _push(routeDecoder!); | 
| 318 | } | 318 | } | 
| @@ -327,12 +327,12 @@ class GetDelegate extends RouterDelegate<RouteDecoder> | @@ -327,12 +327,12 @@ class GetDelegate extends RouterDelegate<RouteDecoder> | ||
| 327 | Future<T?> toNamed<T>( | 327 | Future<T?> toNamed<T>( | 
| 328 | String page, { | 328 | String page, { | 
| 329 | dynamic arguments, | 329 | dynamic arguments, | 
| 330 | - int? id, | 330 | + dynamic id, | 
| 331 | bool preventDuplicates = true, | 331 | bool preventDuplicates = true, | 
| 332 | Map<String, String>? parameters, | 332 | Map<String, String>? parameters, | 
| 333 | }) async { | 333 | }) async { | 
| 334 | final args = _buildPageSettings(page, arguments); | 334 | final args = _buildPageSettings(page, arguments); | 
| 335 | - final route = await _getRouteDecoder<T>(args); | 335 | + final route = _getRouteDecoder<T>(args); | 
| 336 | if (route != null) { | 336 | if (route != null) { | 
| 337 | return _push<T>(route); | 337 | return _push<T>(route); | 
| 338 | } else { | 338 | } else { | 
| @@ -380,7 +380,7 @@ class GetDelegate extends RouterDelegate<RouteDecoder> | @@ -380,7 +380,7 @@ class GetDelegate extends RouterDelegate<RouteDecoder> | ||
| 380 | 380 | ||
| 381 | Get.addPage(getPage); | 381 | Get.addPage(getPage); | 
| 382 | final args = _buildPageSettings(routeName, arguments); | 382 | final args = _buildPageSettings(routeName, arguments); | 
| 383 | - final route = await _getRouteDecoder<T>(args); | 383 | + final route = _getRouteDecoder<T>(args); | 
| 384 | final result = await _push<T>( | 384 | final result = await _push<T>( | 
| 385 | route!, | 385 | route!, | 
| 386 | rebuildStack: rebuildStack, | 386 | rebuildStack: rebuildStack, | 
| @@ -478,7 +478,7 @@ class GetDelegate extends RouterDelegate<RouteDecoder> | @@ -478,7 +478,7 @@ class GetDelegate extends RouterDelegate<RouteDecoder> | ||
| 478 | Map<String, String>? parameters, | 478 | Map<String, String>? parameters, | 
| 479 | }) async { | 479 | }) async { | 
| 480 | final args = _buildPageSettings(page, arguments); | 480 | final args = _buildPageSettings(page, arguments); | 
| 481 | - final route = await _getRouteDecoder<T>(args); | 481 | + final route = _getRouteDecoder<T>(args); | 
| 482 | if (route == null) return null; | 482 | if (route == null) return null; | 
| 483 | 483 | ||
| 484 | while (_activePages.length > 1) { | 484 | while (_activePages.length > 1) { | 
| @@ -497,7 +497,7 @@ class GetDelegate extends RouterDelegate<RouteDecoder> | @@ -497,7 +497,7 @@ class GetDelegate extends RouterDelegate<RouteDecoder> | ||
| 497 | Map<String, String>? parameters, | 497 | Map<String, String>? parameters, | 
| 498 | }) async { | 498 | }) async { | 
| 499 | final args = _buildPageSettings(page, arguments); | 499 | final args = _buildPageSettings(page, arguments); | 
| 500 | - final route = await _getRouteDecoder<T>(args); | 500 | + final route = _getRouteDecoder<T>(args); | 
| 501 | if (route == null) return null; | 501 | if (route == null) return null; | 
| 502 | 502 | ||
| 503 | final newPredicate = predicate ?? (route) => false; | 503 | final newPredicate = predicate ?? (route) => false; | 
| @@ -517,7 +517,7 @@ class GetDelegate extends RouterDelegate<RouteDecoder> | @@ -517,7 +517,7 @@ class GetDelegate extends RouterDelegate<RouteDecoder> | ||
| 517 | Map<String, String>? parameters, | 517 | Map<String, String>? parameters, | 
| 518 | }) async { | 518 | }) async { | 
| 519 | final args = _buildPageSettings(page, arguments); | 519 | final args = _buildPageSettings(page, arguments); | 
| 520 | - final route = await _getRouteDecoder<T>(args); | 520 | + final route = _getRouteDecoder<T>(args); | 
| 521 | if (route == null) return null; | 521 | if (route == null) return null; | 
| 522 | _popWithResult(); | 522 | _popWithResult(); | 
| 523 | return _push<T>(route); | 523 | return _push<T>(route); | 
| @@ -531,7 +531,7 @@ class GetDelegate extends RouterDelegate<RouteDecoder> | @@ -531,7 +531,7 @@ class GetDelegate extends RouterDelegate<RouteDecoder> | ||
| 531 | ]) async { | 531 | ]) async { | 
| 532 | final arguments = _buildPageSettings(page, data); | 532 | final arguments = _buildPageSettings(page, data); | 
| 533 | 533 | ||
| 534 | - final route = await _getRouteDecoder<T>(arguments); | 534 | + final route = _getRouteDecoder<T>(arguments); | 
| 535 | 535 | ||
| 536 | if (route == null) return null; | 536 | if (route == null) return null; | 
| 537 | 537 | ||
| @@ -586,7 +586,7 @@ class GetDelegate extends RouterDelegate<RouteDecoder> | @@ -586,7 +586,7 @@ class GetDelegate extends RouterDelegate<RouteDecoder> | ||
| 586 | Future<R?> backAndtoNamed<T, R>(String page, | 586 | Future<R?> backAndtoNamed<T, R>(String page, | 
| 587 | {T? result, Object? arguments}) async { | 587 | {T? result, Object? arguments}) async { | 
| 588 | final args = _buildPageSettings(page, arguments); | 588 | final args = _buildPageSettings(page, arguments); | 
| 589 | - final route = await _getRouteDecoder<R>(args); | 589 | + final route = _getRouteDecoder<R>(args); | 
| 590 | if (route == null) return null; | 590 | if (route == null) return null; | 
| 591 | _popWithResult<T>(result); | 591 | _popWithResult<T>(result); | 
| 592 | return _push<R>(route); | 592 | return _push<R>(route); | 
| @@ -625,7 +625,7 @@ class GetDelegate extends RouterDelegate<RouteDecoder> | @@ -625,7 +625,7 @@ class GetDelegate extends RouterDelegate<RouteDecoder> | ||
| 625 | final index = _activePages.length > 1 ? _activePages.length - 1 : 0; | 625 | final index = _activePages.length > 1 ? _activePages.length - 1 : 0; | 
| 626 | Get.addPage(page); | 626 | Get.addPage(page); | 
| 627 | 627 | ||
| 628 | - final activePage = await _getRouteDecoder(arguments); | 628 | + final activePage = _getRouteDecoder(arguments); | 
| 629 | 629 | ||
| 630 | // final activePage = _configureRouterDecoder<T>(route!, arguments); | 630 | // final activePage = _configureRouterDecoder<T>(route!, arguments); | 
| 631 | 631 | ||
| @@ -670,7 +670,7 @@ class GetDelegate extends RouterDelegate<RouteDecoder> | @@ -670,7 +670,7 @@ class GetDelegate extends RouterDelegate<RouteDecoder> | ||
| 670 | } | 670 | } | 
| 671 | 671 | ||
| 672 | @protected | 672 | @protected | 
| 673 | - Future<RouteDecoder?> _getRouteDecoder<T>(PageSettings arguments) async { | 673 | + RouteDecoder? _getRouteDecoder<T>(PageSettings arguments) { | 
| 674 | var page = arguments.uri.path; | 674 | var page = arguments.uri.path; | 
| 675 | final parameters = arguments.params; | 675 | final parameters = arguments.params; | 
| 676 | if (parameters.isNotEmpty) { | 676 | if (parameters.isNotEmpty) { | 
| @@ -682,10 +682,7 @@ class GetDelegate extends RouterDelegate<RouteDecoder> | @@ -682,10 +682,7 @@ class GetDelegate extends RouterDelegate<RouteDecoder> | ||
| 682 | final route = decoder.route; | 682 | final route = decoder.route; | 
| 683 | if (route == null) return null; | 683 | if (route == null) return null; | 
| 684 | 684 | ||
| 685 | - final configure = _configureRouterDecoder(decoder, arguments); | ||
| 686 | - | ||
| 687 | - final router = await runMiddleware(configure); | ||
| 688 | - return router; | 685 | + return _configureRouterDecoder(decoder, arguments); | 
| 689 | } | 686 | } | 
| 690 | 687 | ||
| 691 | @protected | 688 | @protected | 
| @@ -709,10 +706,14 @@ class GetDelegate extends RouterDelegate<RouteDecoder> | @@ -709,10 +706,14 @@ class GetDelegate extends RouterDelegate<RouteDecoder> | ||
| 709 | return decoder; | 706 | return decoder; | 
| 710 | } | 707 | } | 
| 711 | 708 | ||
| 712 | - Future<T?> _push<T>(RouteDecoder res, | 709 | + Future<T?> _push<T>(RouteDecoder decoder, | 
| 713 | {bool rebuildStack = true, | 710 | {bool rebuildStack = true, | 
| 714 | PreventDuplicateHandlingMode preventDuplicateHandlingMode = | 711 | PreventDuplicateHandlingMode preventDuplicateHandlingMode = | 
| 715 | PreventDuplicateHandlingMode.ReorderRoutes}) async { | 712 | PreventDuplicateHandlingMode.ReorderRoutes}) async { | 
| 713 | + var mid = await runMiddleware(decoder); | ||
| 714 | + final res = mid ?? decoder; | ||
| 715 | + // if (res == null) res = decoder; | ||
| 716 | + | ||
| 716 | final onStackPage = _activePages | 717 | final onStackPage = _activePages | 
| 717 | .firstWhereOrNull((element) => element.route?.key == res.route?.key); | 718 | .firstWhereOrNull((element) => element.route?.key == res.route?.key); | 
| 718 | 719 | ||
| @@ -744,7 +745,7 @@ class GetDelegate extends RouterDelegate<RouteDecoder> | @@ -744,7 +745,7 @@ class GetDelegate extends RouterDelegate<RouteDecoder> | ||
| 744 | refresh(); | 745 | refresh(); | 
| 745 | } | 746 | } | 
| 746 | 747 | ||
| 747 | - return res.route?.completer?.future as Future<T?>?; | 748 | + return decoder.route?.completer?.future as Future<T?>?; | 
| 748 | } | 749 | } | 
| 749 | 750 | ||
| 750 | @override | 751 | @override | 
| 1 | +import 'package:flutter/foundation.dart'; | ||
| 2 | + | ||
| 1 | import '../../../route_manager.dart'; | 3 | import '../../../route_manager.dart'; | 
| 2 | 4 | ||
| 5 | +@immutable | ||
| 3 | class RouteDecoder { | 6 | class RouteDecoder { | 
| 4 | const RouteDecoder( | 7 | const RouteDecoder( | 
| 5 | this.currentTreeBranch, | 8 | this.currentTreeBranch, | 
| @@ -59,6 +62,18 @@ class RouteDecoder { | @@ -59,6 +62,18 @@ class RouteDecoder { | ||
| 59 | currentTreeBranch[index] = _route.copy(arguments: arguments); | 62 | currentTreeBranch[index] = _route.copy(arguments: arguments); | 
| 60 | } | 63 | } | 
| 61 | } | 64 | } | 
| 65 | + | ||
| 66 | + @override | ||
| 67 | + bool operator ==(Object other) { | ||
| 68 | + if (identical(this, other)) return true; | ||
| 69 | + | ||
| 70 | + return other is RouteDecoder && | ||
| 71 | + listEquals(other.currentTreeBranch, currentTreeBranch) && | ||
| 72 | + other.pageSettings == pageSettings; | ||
| 73 | + } | ||
| 74 | + | ||
| 75 | + @override | ||
| 76 | + int get hashCode => currentTreeBranch.hashCode ^ pageSettings.hashCode; | ||
| 62 | } | 77 | } | 
| 63 | 78 | ||
| 64 | class ParseRouteTree { | 79 | class ParseRouteTree { | 
| @@ -20,6 +20,21 @@ abstract class _RouteMiddleware { | @@ -20,6 +20,21 @@ abstract class _RouteMiddleware { | ||
| 20 | /// {@end-tool} | 20 | /// {@end-tool} | 
| 21 | int? priority; | 21 | int? priority; | 
| 22 | 22 | ||
| 23 | + /// This function will be called when the page of | ||
| 24 | + /// the called route is being searched for. | ||
| 25 | + /// It take RouteSettings as a result an redirect to the new settings or | ||
| 26 | + /// give it null and there will be no redirecting. | ||
| 27 | + /// {@tool snippet} | ||
| 28 | + /// ```dart | ||
| 29 | + /// GetPage redirect(String route) { | ||
| 30 | + /// final authService = Get.find<AuthService>(); | ||
| 31 | + /// return authService.authed.value ? null : RouteSettings(name: '/login'); | ||
| 32 | + /// } | ||
| 33 | + /// ``` | ||
| 34 | + /// {@end-tool} | ||
| 35 | + RouteSettings? redirect(String route); | ||
| 36 | + | ||
| 37 | + /// Similar to [redirect], | ||
| 23 | /// This function will be called when the router delegate changes the | 38 | /// This function will be called when the router delegate changes the | 
| 24 | /// current route. | 39 | /// current route. | 
| 25 | /// | 40 | /// | 
| @@ -30,13 +45,13 @@ abstract class _RouteMiddleware { | @@ -30,13 +45,13 @@ abstract class _RouteMiddleware { | ||
| 30 | /// and no new routes are pushed. | 45 | /// and no new routes are pushed. | 
| 31 | /// {@tool snippet} | 46 | /// {@tool snippet} | 
| 32 | /// ```dart | 47 | /// ```dart | 
| 33 | - /// RouteDecoder? redirect(RouteDecoder route) { | 48 | + /// GetNavConfig? redirect(GetNavConfig route) { | 
| 34 | /// final authService = Get.find<AuthService>(); | 49 | /// final authService = Get.find<AuthService>(); | 
| 35 | - /// return authService.authed.value ? null : RouteDecoder.fromRoute('/login'); | 50 | + /// return authService.authed.value ? null : RouteSettings(name: '/login'); | 
| 36 | /// } | 51 | /// } | 
| 37 | /// ``` | 52 | /// ``` | 
| 38 | /// {@end-tool} | 53 | /// {@end-tool} | 
| 39 | - Future<RouteDecoder?> redirect(RouteDecoder route); | 54 | + Future<RouteDecoder?> redirectDelegate(RouteDecoder route); | 
| 40 | 55 | ||
| 41 | /// This function will be called when this Page is called | 56 | /// This function will be called when this Page is called | 
| 42 | /// you can use it to change something about the page or give it new page | 57 | /// you can use it to change something about the page or give it new page | 
| @@ -86,8 +101,8 @@ class GetMiddleware implements _RouteMiddleware { | @@ -86,8 +101,8 @@ class GetMiddleware implements _RouteMiddleware { | ||
| 86 | 101 | ||
| 87 | GetMiddleware({this.priority}); | 102 | GetMiddleware({this.priority}); | 
| 88 | 103 | ||
| 89 | - // @override | ||
| 90 | - // RouteSettings? redirect(String? route) => null; | 104 | + @override | 
| 105 | + RouteSettings? redirect(String? route) => null; | ||
| 91 | 106 | ||
| 92 | @override | 107 | @override | 
| 93 | GetPage? onPageCalled(GetPage? page) => page; | 108 | GetPage? onPageCalled(GetPage? page) => page; | 
| @@ -105,7 +120,7 @@ class GetMiddleware implements _RouteMiddleware { | @@ -105,7 +120,7 @@ class GetMiddleware implements _RouteMiddleware { | ||
| 105 | void onPageDispose() {} | 120 | void onPageDispose() {} | 
| 106 | 121 | ||
| 107 | @override | 122 | @override | 
| 108 | - Future<RouteDecoder?> redirect(RouteDecoder route) => | 123 | + Future<RouteDecoder?> redirectDelegate(RouteDecoder route) => | 
| 109 | SynchronousFuture(route); | 124 | SynchronousFuture(route); | 
| 110 | } | 125 | } | 
| 111 | 126 | ||
| @@ -129,17 +144,17 @@ class MiddlewareRunner { | @@ -129,17 +144,17 @@ class MiddlewareRunner { | ||
| 129 | return page; | 144 | return page; | 
| 130 | } | 145 | } | 
| 131 | 146 | ||
| 132 | - // RouteSettings? runRedirect(String? route) { | ||
| 133 | - // RouteSettings? to; | ||
| 134 | - // for (final element in _getMiddlewares()) { | ||
| 135 | - // to = element.redirect(route); | ||
| 136 | - // if (to != null) { | ||
| 137 | - // break; | ||
| 138 | - // } | ||
| 139 | - // } | ||
| 140 | - // Get.log('Redirect to $to'); | ||
| 141 | - // return to; | ||
| 142 | - // } | 147 | + RouteSettings? runRedirect(String? route) { | 
| 148 | + RouteSettings? to; | ||
| 149 | + for (final element in _getMiddlewares()) { | ||
| 150 | + to = element.redirect(route); | ||
| 151 | + if (to != null) { | ||
| 152 | + break; | ||
| 153 | + } | ||
| 154 | + } | ||
| 155 | + Get.log('Redirect to $to'); | ||
| 156 | + return to; | ||
| 157 | + } | ||
| 143 | 158 | ||
| 144 | List<R>? runOnBindingsStart<R>(List<R>? bindings) { | 159 | List<R>? runOnBindingsStart<R>(List<R>? bindings) { | 
| 145 | _getMiddlewares().forEach((element) { | 160 | _getMiddlewares().forEach((element) { | 
| @@ -259,11 +274,11 @@ class PageRedirect { | @@ -259,11 +274,11 @@ class PageRedirect { | ||
| 259 | if (match.route!.middlewares == null || match.route!.middlewares!.isEmpty) { | 274 | if (match.route!.middlewares == null || match.route!.middlewares!.isEmpty) { | 
| 260 | return false; | 275 | return false; | 
| 261 | } | 276 | } | 
| 262 | - // final newSettings = runner.runRedirect(settings!.name); | ||
| 263 | - // if (newSettings == null) { | ||
| 264 | - // return false; | ||
| 265 | - // } | ||
| 266 | - // settings = newSettings; | 277 | + final newSettings = runner.runRedirect(settings!.name); | 
| 278 | + if (newSettings == null) { | ||
| 279 | + return false; | ||
| 280 | + } | ||
| 281 | + settings = newSettings; | ||
| 267 | return true; | 282 | return true; | 
| 268 | } | 283 | } | 
| 269 | 284 | 
| @@ -5,11 +5,7 @@ import '../../../get.dart'; | @@ -5,11 +5,7 @@ import '../../../get.dart'; | ||
| 5 | class RouterOutlet<TDelegate extends RouterDelegate<T>, T extends Object> | 5 | class RouterOutlet<TDelegate extends RouterDelegate<T>, T extends Object> | 
| 6 | extends StatefulWidget { | 6 | extends StatefulWidget { | 
| 7 | final TDelegate routerDelegate; | 7 | final TDelegate routerDelegate; | 
| 8 | - final Widget Function( | ||
| 9 | - BuildContext context, | ||
| 10 | - TDelegate delegate, | ||
| 11 | - T? currentRoute, | ||
| 12 | - ) builder; | 8 | + final Widget Function(BuildContext context) builder; | 
| 13 | 9 | ||
| 14 | //keys | 10 | //keys | 
| 15 | RouterOutlet.builder({ | 11 | RouterOutlet.builder({ | 
| @@ -28,7 +24,9 @@ class RouterOutlet<TDelegate extends RouterDelegate<T>, T extends Object> | @@ -28,7 +24,9 @@ class RouterOutlet<TDelegate extends RouterDelegate<T>, T extends Object> | ||
| 28 | ) | 24 | ) | 
| 29 | pageBuilder, | 25 | pageBuilder, | 
| 30 | }) : this.builder( | 26 | }) : this.builder( | 
| 31 | - builder: (context, rDelegate, currentConfig) { | 27 | + builder: (context) { | 
| 28 | + final currentConfig = context.delegate.currentConfiguration as T?; | ||
| 29 | + final rDelegate = context.delegate as TDelegate; | ||
| 32 | var picked = | 30 | var picked = | 
| 33 | currentConfig == null ? null : pickPages(currentConfig); | 31 | currentConfig == null ? null : pickPages(currentConfig); | 
| 34 | if (picked?.length == 0) { | 32 | if (picked?.length == 0) { | 
| @@ -45,38 +43,83 @@ class RouterOutlet<TDelegate extends RouterDelegate<T>, T extends Object> | @@ -45,38 +43,83 @@ class RouterOutlet<TDelegate extends RouterDelegate<T>, T extends Object> | ||
| 45 | 43 | ||
| 46 | class _RouterOutletState<TDelegate extends RouterDelegate<T>, T extends Object> | 44 | class _RouterOutletState<TDelegate extends RouterDelegate<T>, T extends Object> | 
| 47 | extends State<RouterOutlet<TDelegate, T>> { | 45 | extends State<RouterOutlet<TDelegate, T>> { | 
| 48 | - TDelegate get delegate => widget.routerDelegate; | 46 | + RouterDelegate? delegate; | 
| 47 | + late ChildBackButtonDispatcher _backButtonDispatcher; | ||
| 48 | + | ||
| 49 | + void _listener() { | ||
| 50 | + setState(() {}); | ||
| 51 | + } | ||
| 52 | + | ||
| 53 | + VoidCallback? disposer; | ||
| 54 | + | ||
| 49 | @override | 55 | @override | 
| 50 | - void initState() { | ||
| 51 | - super.initState(); | ||
| 52 | - _getCurrentRoute(); | ||
| 53 | - delegate.addListener(onRouterDelegateChanged); | 56 | + void didChangeDependencies() { | 
| 57 | + super.didChangeDependencies(); | ||
| 58 | + disposer?.call(); | ||
| 59 | + final router = Router.of(context); | ||
| 60 | + delegate ??= router.routerDelegate; | ||
| 61 | + delegate?.addListener(_listener); | ||
| 62 | + disposer = () => delegate?.removeListener(_listener); | ||
| 63 | + | ||
| 64 | + _backButtonDispatcher = | ||
| 65 | + router.backButtonDispatcher!.createChildBackButtonDispatcher(); | ||
| 54 | } | 66 | } | 
| 55 | 67 | ||
| 56 | @override | 68 | @override | 
| 57 | void dispose() { | 69 | void dispose() { | 
| 58 | - delegate.removeListener(onRouterDelegateChanged); | ||
| 59 | super.dispose(); | 70 | super.dispose(); | 
| 60 | - } | ||
| 61 | - | ||
| 62 | - T? currentRoute; | ||
| 63 | - void _getCurrentRoute() { | ||
| 64 | - currentRoute = delegate.currentConfiguration; | ||
| 65 | - } | ||
| 66 | - | ||
| 67 | - void onRouterDelegateChanged() { | ||
| 68 | - setState(_getCurrentRoute); | 71 | + Get.routerDelegate?.removeListener(_listener); | 
| 72 | + //_backButtonDispatcher.forget(_backButtonDispatcher) | ||
| 69 | } | 73 | } | 
| 70 | 74 | ||
| 71 | @override | 75 | @override | 
| 72 | Widget build(BuildContext context) { | 76 | Widget build(BuildContext context) { | 
| 73 | - return widget.builder(context, delegate, currentRoute); | 77 | + _backButtonDispatcher.takePriority(); | 
| 78 | + return widget.builder(context); | ||
| 74 | } | 79 | } | 
| 75 | } | 80 | } | 
| 76 | 81 | ||
| 77 | -class GetRouterOutlet extends RouterOutlet<GetDelegate, RouteDecoder> { | 82 | +// class _RouterOutletState<TDelegate extends RouterDelegate<T>, T extends Object> | 
| 83 | +// extends State<RouterOutlet<TDelegate, T>> { | ||
| 84 | +// TDelegate get delegate => context.delegate as TDelegate; | ||
| 85 | +// @override | ||
| 86 | +// void initState() { | ||
| 87 | +// super.initState(); | ||
| 88 | +// } | ||
| 89 | + | ||
| 90 | +// VoidCallback? disposer; | ||
| 91 | + | ||
| 92 | +// @override | ||
| 93 | +// void didChangeDependencies() { | ||
| 94 | +// disposer?.call(); | ||
| 95 | +// delegate.addListener(onRouterDelegateChanged); | ||
| 96 | +// disposer = () => delegate.removeListener(onRouterDelegateChanged); | ||
| 97 | +// _getCurrentRoute(); | ||
| 98 | +// super.didChangeDependencies(); | ||
| 99 | +// } | ||
| 100 | + | ||
| 101 | +// @override | ||
| 102 | +// void dispose() { | ||
| 103 | +// disposer?.call(); | ||
| 104 | +// super.dispose(); | ||
| 105 | +// } | ||
| 106 | + | ||
| 107 | +// T? currentRoute; | ||
| 108 | +// void _getCurrentRoute() { | ||
| 109 | +// currentRoute = delegate.currentConfiguration; | ||
| 110 | +// } | ||
| 111 | + | ||
| 112 | +// void onRouterDelegateChanged() { | ||
| 113 | +// setState(_getCurrentRoute); | ||
| 114 | +// } | ||
| 115 | + | ||
| 116 | +// @override | ||
| 117 | +// Widget build(BuildContext context) { | ||
| 118 | +// return widget.builder(context); | ||
| 119 | +// } | ||
| 120 | +// } | ||
| 78 | 121 | ||
| 79 | - | 122 | +class GetRouterOutlet extends RouterOutlet<GetDelegate, RouteDecoder> { | 
| 80 | GetRouterOutlet({ | 123 | GetRouterOutlet({ | 
| 81 | String? anchorRoute, | 124 | String? anchorRoute, | 
| 82 | required String initialRoute, | 125 | required String initialRoute, | 
| @@ -101,7 +144,7 @@ class GetRouterOutlet extends RouterOutlet<GetDelegate, RouteDecoder> { | @@ -101,7 +144,7 @@ class GetRouterOutlet extends RouterOutlet<GetDelegate, RouteDecoder> { | ||
| 101 | } | 144 | } | 
| 102 | return ret; | 145 | return ret; | 
| 103 | }, | 146 | }, | 
| 104 | - emptyPage: (delegate) => | 147 | + emptyPage: (delegate) => | 
| 105 | Get.routeTree.matchRoute(initialRoute).route ?? | 148 | Get.routeTree.matchRoute(initialRoute).route ?? | 
| 106 | delegate.notFoundRoute, | 149 | delegate.notFoundRoute, | 
| 107 | key: Get.nestedKey(anchorRoute)?.navigatorKey, | 150 | key: Get.nestedKey(anchorRoute)?.navigatorKey, | 
| @@ -145,8 +188,6 @@ class GetRouterOutlet extends RouterOutlet<GetDelegate, RouteDecoder> { | @@ -145,8 +188,6 @@ class GetRouterOutlet extends RouterOutlet<GetDelegate, RouteDecoder> { | ||
| 145 | GetRouterOutlet.builder({ | 188 | GetRouterOutlet.builder({ | 
| 146 | required Widget Function( | 189 | required Widget Function( | 
| 147 | BuildContext context, | 190 | BuildContext context, | 
| 148 | - GetDelegate delegate, | ||
| 149 | - RouteDecoder? currentRoute, | ||
| 150 | ) | 191 | ) | 
| 151 | builder, | 192 | builder, | 
| 152 | GetDelegate? routerDelegate, | 193 | GetDelegate? routerDelegate, | 
| @@ -11,7 +11,7 @@ class RedirectMiddleware extends GetMiddleware { | @@ -11,7 +11,7 @@ class RedirectMiddleware extends GetMiddleware { | ||
| 11 | // } | 11 | // } | 
| 12 | 12 | ||
| 13 | @override | 13 | @override | 
| 14 | - Future<RouteDecoder?> redirect(RouteDecoder route) async { | 14 | + Future<RouteDecoder?> redirectDelegate(RouteDecoder route) async { | 
| 15 | return RouteDecoder.fromRoute('/second'); | 15 | return RouteDecoder.fromRoute('/second'); | 
| 16 | } | 16 | } | 
| 17 | } | 17 | } | 
| @@ -23,8 +23,8 @@ class RedirectMiddlewareNull extends GetMiddleware { | @@ -23,8 +23,8 @@ class RedirectMiddlewareNull extends GetMiddleware { | ||
| 23 | // } | 23 | // } | 
| 24 | 24 | ||
| 25 | @override | 25 | @override | 
| 26 | - Future<RouteDecoder?> redirect(RouteDecoder route) async { | ||
| 27 | - return route; | 26 | + Future<RouteDecoder?> redirectDelegate(RouteDecoder route) async { | 
| 27 | + return null; | ||
| 28 | } | 28 | } | 
| 29 | } | 29 | } | 
| 30 | 30 | ||
| @@ -66,7 +66,7 @@ void main() { | @@ -66,7 +66,7 @@ void main() { | ||
| 66 | ), | 66 | ), | 
| 67 | ); | 67 | ); | 
| 68 | 68 | ||
| 69 | - await tester.pump(); | 69 | + // await tester.pump(); | 
| 70 | 70 | ||
| 71 | Get.toNamed('/first'); | 71 | Get.toNamed('/first'); | 
| 72 | 72 | 
- 
Please register or login to post a comment