Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve getpage #3194

Merged
merged 3 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## [5.0.0-release-candidate-11]

- Add canPop to GetPage

## [5.0.0-release-candidate-10]

- Fix Get.offNamedUntil
Expand Down
36 changes: 36 additions & 0 deletions example_nav2/lib/app/routes/app_pages.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import 'dart:developer';

import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../middleware/auth_middleware.dart';
Expand Down Expand Up @@ -104,3 +107,36 @@ class AppPages {
),
];
}

class MainMiddleware extends GetMiddleware {
@override
void onPageDispose() {
log('MainMiddleware onPageDispose');
super.onPageDispose();
}

@override
Widget onPageBuilt(Widget page) {
log('MainMiddleware onPageBuilt');
return super.onPageBuilt(page);
}

@override
GetPage? onPageCalled(GetPage? page) {
log('MainMiddleware onPageCalled for route: ${page?.name}');
return super.onPageCalled(page);
}

@override
List<R>? onBindingsStart<R>(List<R>? bindings) {
log('MainMiddleware onBindingsStart');
return super.onBindingsStart(bindings);
}

@override
GetPageBuilder? onPageBuildStart(GetPageBuilder? page) {
log('MainMiddleware onPageBuildStart');

return super.onPageBuildStart(page);
}
}
19 changes: 10 additions & 9 deletions lib/get_navigation/src/routes/default_route.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ class GetPageRoute<T> extends PageRoute<T>
this.maintainState = true,
super.fullscreenDialog,
this.middlewares,
}) : bindings = (binding == null) ? bindings : [...bindings, binding];
}) : bindings = (binding == null) ? bindings : [...bindings, binding],
_middlewareRunner = MiddlewareRunner(middlewares);

@override
final Duration transitionDuration;
Expand Down Expand Up @@ -101,26 +102,26 @@ class GetPageRoute<T> extends PageRoute<T>
@override
final bool maintainState;

final MiddlewareRunner _middlewareRunner;

@override
void dispose() {
super.dispose();
final middlewareRunner = MiddlewareRunner(middlewares);
middlewareRunner.runOnPageDispose();
_middlewareRunner.runOnPageDispose();
_child = null;
}

Widget? _child;

Widget _getChild() {
if (_child != null) return _child!;
final middlewareRunner = MiddlewareRunner(middlewares);

final localBinds = [if (binds != null) ...binds!];

final bindingsToBind = middlewareRunner
final bindingsToBind = _middlewareRunner
.runOnBindingsStart(bindings.isNotEmpty ? bindings : localBinds);

final pageToBuild = middlewareRunner.runOnPageBuildStart(page)!;
final pageToBuild = _middlewareRunner.runOnPageBuildStart(page)!;

if (bindingsToBind != null && bindingsToBind.isNotEmpty) {
if (bindingsToBind is List<BindingsInterface>) {
Expand All @@ -129,19 +130,19 @@ class GetPageRoute<T> extends PageRoute<T>
if (dep is List<Bind>) {
_child = Binds(
binds: dep,
child: middlewareRunner.runOnPageBuilt(pageToBuild()),
child: _middlewareRunner.runOnPageBuilt(pageToBuild()),
);
}
}
} else if (bindingsToBind is List<Bind>) {
_child = Binds(
binds: bindingsToBind,
child: middlewareRunner.runOnPageBuilt(pageToBuild()),
child: _middlewareRunner.runOnPageBuilt(pageToBuild()),
);
}
}

return _child ??= middlewareRunner.runOnPageBuilt(pageToBuild());
return _child ??= _middlewareRunner.runOnPageBuilt(pageToBuild());
}

@override
Expand Down
11 changes: 11 additions & 0 deletions lib/get_navigation/src/routes/get_route.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ class GetPage<T> extends Page<T> {

final PreventDuplicateHandlingMode preventDuplicateHandlingMode;

static void _defaultPopInvokedHandler(bool didPop, Object? result) {}

GetPage({
required this.name,
required this.page,
Expand Down Expand Up @@ -85,6 +87,9 @@ class GetPage<T> extends Page<T> {
this.completer,
this.inheritParentPath = true,
LocalKey? key,
super.canPop,
super.onPopInvoked = _defaultPopInvokedHandler,
super.restorationId,
}) : path = _nameToRegex(name),
assert(name.startsWith('/'),
'It is necessary to start route name [$name] with a slash: /$name'),
Expand Down Expand Up @@ -125,6 +130,9 @@ class GetPage<T> extends Page<T> {
bool? showCupertinoParallax,
Completer<T?>? completer,
bool? inheritParentPath,
bool? canPop,
PopInvokedWithResultCallback<T>? onPopInvoked,
String? restorationId,
}) {
return GetPage(
key: key ?? this.key,
Expand Down Expand Up @@ -158,6 +166,9 @@ class GetPage<T> extends Page<T> {
showCupertinoParallax ?? this.showCupertinoParallax,
completer: completer ?? this.completer,
inheritParentPath: inheritParentPath ?? this.inheritParentPath,
canPop: canPop ?? this.canPop,
onPopInvoked: onPopInvoked ?? this.onPopInvoked,
restorationId: restorationId ?? restorationId,
);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/get_navigation/src/routes/new_path_route.dart
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ class RouteTree {

void removeRoute(GetPage route) {
matcher.removeRoute(route.name);
tree.remove(route.path);
tree.remove(route.name);
}

void removeRoutes(List<GetPage> routes) {
Expand Down
22 changes: 6 additions & 16 deletions lib/get_navigation/src/routes/route_middleware.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import '../../../get.dart';
abstract class GetMiddleware {
GetMiddleware({this.priority = 0});

final bool _wasInitiated = false;

/// The Order of the Middlewares to run.
///
/// {@tool snippet}
Expand Down Expand Up @@ -104,12 +102,7 @@ class MiddlewareRunner {
MiddlewareRunner(List<GetMiddleware>? middlewares)
: _middlewares = middlewares != null
? (List.of(middlewares)..sort(_compareMiddleware))
: const [] {
// for (final middleware in _middlewares) {
// if (middleware._wasInitiated) continue;
// middleware._wasInitiated = true;
// }
}
: const [];

final List<GetMiddleware> _middlewares;

Expand All @@ -118,10 +111,6 @@ class MiddlewareRunner {

GetPage? runOnPageCalled(GetPage? page) {
for (final middleware in _middlewares) {
if (middleware._wasInitiated) {
_middlewares.remove(middleware);
continue;
}
page = middleware.onPageCalled(page);
}
return page;
Expand Down Expand Up @@ -225,14 +214,15 @@ class PageRedirect {
return false;
}

// No middlewares found return match.
if (match.route!.middlewares.isEmpty) {
return false;
}

final runner = MiddlewareRunner(match.route!.middlewares);
route = runner.runOnPageCalled(match.route);
addPageParameter(route!);

// // No middlewares found return match.
// if (match.route!.middlewares.isEmpty) {
// return false;
// }
final newSettings = runner.runRedirect(settings!.name);
if (newSettings == null) {
return false;
Expand Down
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: get
description: Open screens/snackbars/dialogs without context, manage states and inject dependencies easily with GetX.
version: 5.0.0-release-candidate-10
version: 5.0.0-release-candidate-11
homepage: https://github.com/jonataslaw/getx

environment:
Expand All @@ -12,7 +12,7 @@ dependencies:
sdk: flutter
flutter_web_plugins:
sdk: flutter
web: ">=1.0.0 <2.0.0"
web: ">=0.5.0 <2.0.0"

dev_dependencies:
flutter_lints: ^4.0.0
Expand Down
Loading