Skip to content

Commit 6c61894

Browse files
committed
+Simplify setPreservationStrategy
1 parent f68966d commit 6c61894

2 files changed

Lines changed: 13 additions & 16 deletions

File tree

lib/src/route_controller.dart

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -124,17 +124,17 @@ class RouteController {
124124

125125
_TPreservationStrategy _preservationStrategy = defaultPreservationStrategy;
126126

127-
static _TPreservationStrategy defaultPreservationStrategy =
128-
(routeBuider, routeState) =>
129-
routeBuider.shouldPreserve || routeState.shouldPreserve;
127+
static _TPreservationStrategy defaultPreservationStrategy = (routeBuider) =>
128+
routeBuider.shouldPreserve || routeBuider.routeState.shouldPreserve;
130129

131130
void setPreservationStrategy(_TPreservationStrategy shouldPreserve) =>
132131
_preservationStrategy = shouldPreserve;
133132

134133
void _maybeRemoveStaleRoute(RouteState routeState) {
135-
final routeBuilder = _getBuilderByPath(routeState.uri);
134+
final routeBuilder =
135+
_getBuilderByPath(routeState.uri)?.copyWith(routeState: routeState);
136136
if (routeBuilder == null) return;
137-
if (!_preservationStrategy(routeBuilder, routeState)) {
137+
if (!_preservationStrategy(routeBuilder)) {
138138
_widgetCache[routeState] = SizedBox.shrink(key: routeState.key);
139139
}
140140
}
@@ -244,9 +244,8 @@ class RouteController {
244244
if (index < 0 || index >= state.routes.length) return false;
245245

246246
_previousRouteForTransition = currentRouteState;
247-
_nextAnimationEffect = index < state.index
248-
? backwardAnimationEffect
249-
: forwardAnimationEffect;
247+
_nextAnimationEffect =
248+
index < state.index ? backwardAnimationEffect : forwardAnimationEffect;
250249

251250
final newRoute = state.routes[index];
252251
addToCache([newRoute]); // Ensure widget exists before navigating.
@@ -386,9 +385,8 @@ class RouteController {
386385
},
387386
builder: (context, results) {
388387
final children = _widgetCache.values.toList();
389-
final layerEffects = results.isNotEmpty
390-
? results.map((e) => e.data).first
391-
: null;
388+
final layerEffects =
389+
results.isNotEmpty ? results.map((e) => e.data).first : null;
392390
return PrioritizedIndexedStack(
393391
indices: [
394392
_indexOfRouteState(routeState),
@@ -413,8 +411,8 @@ class RouteController {
413411
}
414412

415413
static RouteController of(BuildContext context) {
416-
final provider = context
417-
.dependOnInheritedWidgetOfExactType<RouteControllerProvider>();
414+
final provider =
415+
context.dependOnInheritedWidgetOfExactType<RouteControllerProvider>();
418416
if (provider == null) {
419417
throw FlutterError('No RouteControllerProvider found in context');
420418
}
@@ -445,5 +443,4 @@ class _NavigationState {
445443
}
446444
}
447445

448-
typedef _TPreservationStrategy =
449-
bool Function(RouteBuilder routeBuider, RouteState routeState);
446+
typedef _TPreservationStrategy = bool Function(RouteBuilder routeBuider);

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ funding:
1919
- https://github.com/sponsors/t0mb3rr
2020
- https://www.buymeacoffee.com/dev_cetera
2121
description: A lightweight router designed for ease of use and efficient state management.
22-
version: 0.5.3
22+
version: 0.5.4
2323
topics:
2424
- router
2525
- navigation

0 commit comments

Comments
 (0)