Skip to content

Commit 4de7320

Browse files
committed
+Add shouldPreserve to RouteState
1 parent d1e6e6d commit 4de7320

3 files changed

Lines changed: 24 additions & 27 deletions

File tree

lib/src/route_controller.dart

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class RouteController {
5858
required List<RouteBuilder> builders,
5959
}) {
6060
_builderMap = {
61-
for (var builder in builders) builder.routeState.path: builder,
61+
for (var builder in builders) builder.routeState.uri.path: builder,
6262
};
6363

6464
platformNavigator.addStateCallback(pushUri);
@@ -125,7 +125,7 @@ class RouteController {
125125
void _maybeRemoveStaleRoute(RouteState routeState) {
126126
final builder = _getBuilderByPath(routeState.uri);
127127
if (builder == null) return;
128-
if (!builder.shouldPreserve) {
128+
if (!builder.shouldPreserve && !routeState.shouldPreserve) {
129129
_widgetCache[routeState] = SizedBox.shrink(key: routeState.key);
130130
}
131131
}
@@ -235,9 +235,8 @@ class RouteController {
235235
if (index < 0 || index >= state.routes.length) return false;
236236

237237
_previousRouteForTransition = currentRouteState;
238-
_nextAnimationEffect = index < state.index
239-
? backwardAnimationEffect
240-
: forwardAnimationEffect;
238+
_nextAnimationEffect =
239+
index < state.index ? backwardAnimationEffect : forwardAnimationEffect;
241240

242241
final newRoute = state.routes[index];
243242
addToCache([newRoute]); // Ensure widget exists before navigating.
@@ -377,9 +376,8 @@ class RouteController {
377376
},
378377
builder: (context, results) {
379378
final children = _widgetCache.values.toList();
380-
final layerEffects = results.isNotEmpty
381-
? results.map((e) => e.data).first
382-
: null;
379+
final layerEffects =
380+
results.isNotEmpty ? results.map((e) => e.data).first : null;
383381
return PrioritizedIndexedStack(
384382
indices: [
385383
_indexOfRouteState(routeState),
@@ -404,8 +402,8 @@ class RouteController {
404402
}
405403

406404
static RouteController of(BuildContext context) {
407-
final provider = context
408-
.dependOnInheritedWidgetOfExactType<RouteControllerProvider>();
405+
final provider =
406+
context.dependOnInheritedWidgetOfExactType<RouteControllerProvider>();
409407
if (provider == null) {
410408
throw FlutterError('No RouteControllerProvider found in context');
411409
}

lib/src/route_state.dart

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,19 @@
1111
// ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
1212
//.title~
1313

14+
import 'package:df_safer_dart/_common.dart';
15+
1416
import '/_common.dart';
1517

1618
// ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
1719

18-
class RouteState<TExtra extends Object?> {
20+
class RouteState<TExtra extends Object?> extends Equatable {
1921
late final Uri uri;
2022
final TExtra? extra;
2123
final bool skipCurrent;
2224
final AnimationEffect animationEffect;
2325
final TRouteConditionFn? condition;
26+
final bool shouldPreserve;
2427

2528
Key get key => ValueKey(uri.toString());
2629

@@ -31,6 +34,7 @@ class RouteState<TExtra extends Object?> {
3134
this.skipCurrent = true,
3235
this.animationEffect = const NoEffect(),
3336
this.condition,
37+
this.shouldPreserve = false,
3438
}) {
3539
final qp = {...uri.queryParameters, ...?queryParameters};
3640
this.uri = uri.replace(queryParameters: qp.isNotEmpty ? qp : null);
@@ -43,6 +47,7 @@ class RouteState<TExtra extends Object?> {
4347
this.skipCurrent = true,
4448
this.animationEffect = const NoEffect(),
4549
this.condition,
50+
this.shouldPreserve = false,
4651
}) {
4752
final uri0 = Uri.parse(pathAndQuery);
4853
final qp = {...uri0.queryParameters, ...?queryParameters};
@@ -55,15 +60,17 @@ class RouteState<TExtra extends Object?> {
5560
TExtra? extra,
5661
bool? skipCurrent,
5762
AnimationEffect? animationEffect,
58-
TRouteConditionFn? condiiton,
63+
TRouteConditionFn? condition,
64+
bool? shouldPreserve,
5965
}) {
6066
return RouteState<TExtra>(
6167
uri ?? this.uri,
6268
queryParameters: queryParameters,
6369
extra: extra ?? this.extra,
6470
skipCurrent: skipCurrent ?? this.skipCurrent,
6571
animationEffect: animationEffect ?? this.animationEffect,
66-
condition: condition ?? condition,
72+
condition: condition ?? this.condition,
73+
shouldPreserve: shouldPreserve ?? this.shouldPreserve,
6774
);
6875
}
6976

@@ -74,16 +81,17 @@ class RouteState<TExtra extends Object?> {
7481
bool skipCurrent = true,
7582
bool animationEffect = true,
7683
bool condition = true,
84+
bool shouldPreserve = true,
7785
}) {
7886
return RouteState<TExtra>(
7987
uri ? this.uri : Uri(),
8088
queryParameters: queryParameters ? this.uri.queryParameters : null,
8189
extra: extra ? this.extra : null,
8290
skipCurrent: skipCurrent ? this.skipCurrent : true,
83-
animationEffect: animationEffect
84-
? const NoEffect()
85-
: this.animationEffect,
91+
animationEffect:
92+
animationEffect ? const NoEffect() : this.animationEffect,
8693
condition: condition ? this.condition : null,
94+
shouldPreserve: shouldPreserve ? this.shouldPreserve : false,
8795
);
8896
}
8997

@@ -93,14 +101,5 @@ class RouteState<TExtra extends Object?> {
93101
bool matchPath(RouteState other) => uri.path == other.uri.path;
94102

95103
@override
96-
bool operator ==(Object other) {
97-
if (identical(this, other)) return true;
98-
if (other is! RouteState) return false;
99-
return uri == other.uri && extra == other.extra;
100-
}
101-
102-
String get path => uri.path;
103-
104-
@override
105-
int get hashCode => (RouteState).hashCode ^ uri.hashCode;
104+
List<Object?> get props => [uri, extra];
106105
}

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.2
22+
version: 0.5.3
2323
topics:
2424
- router
2525
- navigation

0 commit comments

Comments
 (0)