45 lines
1.5 KiB
Dart
45 lines
1.5 KiB
Dart
import 'package:flutter/foundation.dart';
|
|
import 'package:go_router/go_router.dart';
|
|
import 'package:shuffle/core/routing/redirect_source.dart';
|
|
|
|
/// Steuerbare [RedirectSource]: `targets` bildet Pfad -> Weiterleitungsziel ab.
|
|
/// [notifyChanged] löst eine Neubewertung der Redirects aus.
|
|
class FakeRedirectSource implements RedirectSource {
|
|
FakeRedirectSource([Map<String, String>? targets])
|
|
: targets = targets ?? <String, String>{};
|
|
|
|
final Map<String, String> targets;
|
|
final _notifier = ValueNotifier<int>(0);
|
|
|
|
void notifyChanged() => _notifier.value++;
|
|
|
|
@override
|
|
Listenable get changes => _notifier;
|
|
|
|
@override
|
|
String? redirect(GoRouterState state) => targets[state.uri.path];
|
|
}
|
|
|
|
/// Kleiner Test-Router mit eigenen [routes]. Für Screens, die ohne die
|
|
/// App-Routen geprüft werden. Der Aufrufer ruft am Ende `dispose` (z. B. per
|
|
/// `addTearDown(router.dispose)`).
|
|
GoRouter buildTestRouter({
|
|
required List<RouteBase> routes,
|
|
String initialLocation = '/',
|
|
RedirectSource? redirectSource,
|
|
}) => GoRouter(
|
|
initialLocation: initialLocation,
|
|
routes: routes,
|
|
refreshListenable: redirectSource?.changes,
|
|
redirect: redirectSource == null
|
|
? null
|
|
: (context, state) => redirectSource.redirect(state),
|
|
);
|
|
|
|
/// Aktueller Pfad des Routers (ohne Query).
|
|
String currentPath(GoRouter router) =>
|
|
router.routerDelegate.currentConfiguration.uri.path;
|
|
|
|
/// Aktuelle URI inklusive Query, z. B. für `?from=`-Prüfungen.
|
|
Uri currentUri(GoRouter router) =>
|
|
router.routerDelegate.currentConfiguration.uri;
|