Jonny Borges
Committed by GitHub

Merge pull request #1515 from Bdaya-Dev/router-outlet

Fixed route creation and handling
... ... @@ -23,6 +23,8 @@ class HomeView extends GetView<HomeController> {
body: GetRouterOutlet(
emptyPage: (delegate) => DashboardView(),
pickPages: (currentNavStack) {
print('Home RouterOutlet: $currentNavStack');
// will take any route after home
final res =
currentNavStack.currentTreeBranch.pickAfterRoute(Routes.HOME);
... ...
... ... @@ -35,8 +35,6 @@ class GetInformationParser extends RouteInformationParser<GetNavConfig> {
@override
RouteInformation restoreRouteInformation(GetNavConfig config) {
print('restore $config');
return RouteInformation(
location: config.location,
state: config.state,
... ...
... ... @@ -31,8 +31,5 @@ class GetNavConfig extends RouteInformation {
@override
String toString() => '''
======GetNavConfig=====
currentTreeBranch: $currentTreeBranch
currentPage: $currentPage
======GetNavConfig=====''';
======GetNavConfig=====\ncurrentTreeBranch: $currentTreeBranch\ncurrentPage: $currentPage\n======GetNavConfig=====''';
}
... ...
... ... @@ -46,7 +46,6 @@ class GetDelegate extends RouterDelegate<GetNavConfig>
final List<GetNavConfig> history = <GetNavConfig>[];
final PopMode backButtonPopMode;
final PreventDuplicateHandlingMode preventDuplicateHandlingMode;
final pageRoutes = <String, GetPageRoute>{};
GetPage? notFoundRoute;
... ... @@ -82,7 +81,6 @@ class GetDelegate extends RouterDelegate<GetNavConfig>
void _removeHistoryEntry(GetNavConfig entry) {
history.remove(entry);
pageRoutes.remove(entry.location);
final lastCompleter = _resultCompleter.remove(entry);
lastCompleter?.complete(entry);
}
... ... @@ -92,7 +90,7 @@ class GetDelegate extends RouterDelegate<GetNavConfig>
if (history.any((element) => element.location == config.location)) {
switch (preventDuplicateHandlingMode) {
case PreventDuplicateHandlingMode.PopUntilOriginalRoute:
until(config.location!, popMode: PopMode.History);
until(config.location!, popMode: PopMode.Page);
return;
case PreventDuplicateHandlingMode.DoNothing:
default:
... ... @@ -101,8 +99,11 @@ class GetDelegate extends RouterDelegate<GetNavConfig>
}
}
history.add(config);
pageRoutes[config.location!] =
PageRedirect(config.currentPage!, _notFound()).page();
}
GetPageRoute getPageRoute(RouteSettings? settings) {
return PageRedirect(settings ?? RouteSettings(name: '/404'), _notFound())
.page();
}
GetNavConfig? _popHistory() {
... ... @@ -112,7 +113,6 @@ class GetDelegate extends RouterDelegate<GetNavConfig>
GetNavConfig _doPopHistory() {
final res = history.removeLast();
pageRoutes.remove(res.location);
return res;
}
... ... @@ -190,6 +190,16 @@ class GetDelegate extends RouterDelegate<GetNavConfig>
return SynchronousFuture(_canPopPage());
}
bool _canPop(PopMode mode) {
switch (mode) {
case PopMode.History:
return _canPopHistory();
case PopMode.Page:
default:
return _canPopPage();
}
}
/// gets the visual pages from the current history entry
///
/// visual pages must have the [RouterOutletContainerMiddleWare] middleware
... ... @@ -225,7 +235,6 @@ class GetDelegate extends RouterDelegate<GetNavConfig>
@override
Future<void> setInitialRoutePath(GetNavConfig configuration) async {
history.clear();
pageRoutes.clear();
_resultCompleter.clear();
await pushHistory(configuration);
}
... ... @@ -242,11 +251,6 @@ class GetDelegate extends RouterDelegate<GetNavConfig>
return route;
}
GetPageRoute? get currentRoute {
final curPage = currentConfiguration?.currentPage;
return curPage == null ? null : pageRoutes[curPage];
}
Future<T?> toNamed<T>(String fullRoute) {
final decoder = Get.routeTree.matchRoute(fullRoute);
return pushHistory<T>(
... ... @@ -263,12 +267,12 @@ class GetDelegate extends RouterDelegate<GetNavConfig>
/// DOES NOT remove the [fullRoute]
void until(
String fullRoute, {
PopMode popMode = PopMode.History,
PopMode popMode = PopMode.Page,
}) {
// remove history or page entries until you meet route
final currentEntry = currentConfiguration;
var iterator = currentEntry;
while (history.length > 0 &&
var iterator = currentConfiguration;
while (_canPop(popMode) &&
iterator != null &&
iterator.location != fullRoute) {
_pop(popMode);
... ... @@ -304,7 +308,7 @@ class GetDelegate extends RouterDelegate<GetNavConfig>
@override
Future<bool> popRoute({
Object? result,
PopMode popMode = PopMode.History,
PopMode popMode = PopMode.Page,
}) async {
//Returning false will cause the entire app to be popped.
final wasPopup = await handlePopupRoutes(result: result);
... ...
... ... @@ -50,6 +50,7 @@ class _RouterOutletState<TDelegate extends RouterDelegate<T>, T extends Object>
@override
void initState() {
super.initState();
_getCurrentRoute();
delegate.addListener(onRouterDelegateChanged);
}
... ... @@ -60,11 +61,12 @@ class _RouterOutletState<TDelegate extends RouterDelegate<T>, T extends Object>
}
T? currentRoute;
void _getCurrentRoute() {
currentRoute = delegate.currentConfiguration;
}
void onRouterDelegateChanged() {
setState(() {
currentRoute = delegate.currentConfiguration;
});
setState(_getCurrentRoute);
}
@override
... ... @@ -92,8 +94,8 @@ class GetRouterOutlet extends RouterOutlet<GetDelegate, GetNavConfig> {
required List<GetPage> Function(GetNavConfig currentNavStack) pickPages,
}) : super(
pageBuilder: (context, rDelegate, page) {
final pageRoute = rDelegate.pageRoutes[page?.name];
if (pageRoute != null) {
final pageRoute = rDelegate.getPageRoute(page);
if (page != null) {
//TODO: transitions go here !
return pageRoute.buildPage(
context,
... ... @@ -104,8 +106,8 @@ class GetRouterOutlet extends RouterOutlet<GetDelegate, GetNavConfig> {
/// TODO: improve this logic abit
return (emptyPage?.call(rDelegate) ??
rDelegate.notFoundRoute?.page()) ??
SizedBox.shrink();
pageRoute.page?.call() ??
SizedBox.shrink());
},
pickPages: pickPages,
delegate: Get.getDelegate(),
... ...
... ... @@ -53,6 +53,7 @@ class ParseRouteTree {
.map(
(e) => e.value!.copy(
parameter: params,
name: e.key,
),
)
.toList();
... ... @@ -110,7 +111,7 @@ class ParseRouteTree {
GetPage _addChild(
GetPage origin, String parentPath, List<GetMiddleware> middlewares) =>
GetPage(
name: parentPath + origin.name,
name: (parentPath + origin.name).replaceAll(r'//', '/'),
page: origin.page,
title: origin.title,
alignment: origin.alignment,
... ...