Skip to content

Commit

Permalink
fix: Fixed a bug that prevented the first page in nested navigation f…
Browse files Browse the repository at this point in the history
…rom working properly.
  • Loading branch information
mathrunet committed Mar 7, 2023
1 parent b7d653c commit b83468b
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 27 deletions.
14 changes: 7 additions & 7 deletions packages/katana_router/example/pubspec.lock
Expand Up @@ -109,10 +109,10 @@ packages:
dependency: transitive
description:
name: built_value
sha256: "169565c8ad06adb760c3645bf71f00bff161b00002cace266cad42c5d22a7725"
sha256: "31b7c748fd4b9adf8d25d72a4c4a59ef119f12876cf414f94f8af5131d5fa2b0"
url: "https://pub.dev"
source: hosted
version: "8.4.3"
version: "8.4.4"
characters:
dependency: transitive
description:
Expand Down Expand Up @@ -181,10 +181,10 @@ packages:
dependency: transitive
description:
name: dart_style
sha256: "7a03456c3490394c8e7665890333e91ae8a49be43542b616e414449ac358acd4"
sha256: "5be16bf1707658e4c03078d4a9b90208ded217fb02c163e207d334082412f2fb"
url: "https://pub.dev"
source: hosted
version: "2.2.4"
version: "2.2.5"
encrypt:
dependency: transitive
description:
Expand Down Expand Up @@ -358,21 +358,21 @@ packages:
path: ".."
relative: true
source: path
version: "1.11.4"
version: "1.11.5"
katana_router_annotation:
dependency: "direct overridden"
description:
path: "../../katana_router_annotation"
relative: true
source: path
version: "1.11.0"
version: "1.11.1"
katana_router_builder:
dependency: "direct dev"
description:
path: "../../katana_router_builder"
relative: true
source: path
version: "1.11.1"
version: "1.11.2"
lints:
dependency: transitive
description:
Expand Down
68 changes: 56 additions & 12 deletions packages/katana_router/lib/src/app_router.dart
@@ -1,5 +1,6 @@
part of katana_router;

/// {@template katana_router.app_router}
/// Controller to define routing for the entire app.
///
/// You can define the routing for the entire app by passing it to `routerConfig` in [MaterialApp.router].
Expand Down Expand Up @@ -51,9 +52,11 @@ part of katana_router;
/// }
///
/// ```
/// {@endtemplate}
class AppRouter extends ChangeNotifier
with NavigatorObserver
implements RouterConfig<RouteQuery> {
/// {@template katana_router.app_router}
/// Controller to define routing for the entire app.
///
/// You can define the routing for the entire app by passing it to `routerConfig` in [MaterialApp.router].
Expand Down Expand Up @@ -105,6 +108,7 @@ class AppRouter extends ChangeNotifier
/// }
///
/// ```
/// {@endtemplate}
AppRouter({
UnknownRouteQueryBuilder? unknown,
BootRouteQueryBuilder? boot,
Expand All @@ -120,6 +124,7 @@ class AppRouter extends ChangeNotifier
bool reportsRouteUpdateToEngine = true,
Widget backgroundWidget = const Scaffold(),
List<LoggerAdapter> loggerAdapters = const [],
bool nested = false,
}) : _loggerAdapters = loggerAdapters {
navigatorKey ??= GlobalKey<NavigatorState>();

Expand All @@ -133,6 +138,7 @@ class AppRouter extends ChangeNotifier
defaultTransitionQuery: defaultTransitionQuery,
reportsRouteUpdateToEngine: reportsRouteUpdateToEngine,
backgroundWidget: backgroundWidget,
nested: nested,
);

_routerDelegate = _AppRouterDelegate(
Expand All @@ -143,17 +149,23 @@ class AppRouter extends ChangeNotifier

_routeInformationParser = _AppRouteInformationParser(this);

final effectiveInitialLocation = _effectiveInitialLocation(
initialPath ?? "/",
);
nested = nested || initialQuery?.nested == true;

final effectiveInitialLocation = nested
? null
: _effectiveInitialLocation(
initialPath,
);

_routeInformationProvider = _AppRouteInformationProvider(
router: this,
initialRouteInformation: InitialRouteInformation(
query: _effectiveInitialQuery(
effectiveInitialLocation,
initialQuery,
),
query: nested
? initialQuery
: _effectiveInitialQuery(
effectiveInitialLocation,
initialQuery,
),
location: effectiveInitialLocation,
),
);
Expand Down Expand Up @@ -447,22 +459,23 @@ class AppRouter extends ChangeNotifier
}
}

String _effectiveInitialLocation(String? initialLocation) {
String? _effectiveInitialLocation(String? initialLocation) {
final String platformDefault =
WidgetsBinding.instance.platformDispatcher.defaultRouteName;
if (initialLocation == null) {
return platformDefault;
} else if (platformDefault == "/") {
if (platformDefault == "/") {
return initialLocation;
} else {
return platformDefault;
}
}

RouteQuery? _effectiveInitialQuery(
String initialLocation,
String? initialLocation,
RouteQuery? initialQuery,
) {
if (initialLocation.isEmpty) {
return initialQuery;
}
for (final page in _config.pages) {
final query = page.resolve(initialLocation);
if (query != null) {
Expand All @@ -473,6 +486,35 @@ class AppRouter extends ChangeNotifier
}
}

/// Class for creating nested [AppRouter].
///
/// ネストされた[AppRouter]を作成するためのクラスです。
///
/// {@macro katana_router.app_router}
class NestedAppRouter extends AppRouter {
/// Class for creating nested [AppRouter].
///
/// ネストされた[AppRouter]を作成するためのクラスです。
///
/// {@macro katana_router.app_router}
NestedAppRouter({
super.unknown,
super.boot,
super.initialPath,
super.initialQuery,
required super.pages,
super.redirect = const [],
super.observers = const [],
super.redirectLimit = 5,
super.navigatorKey,
super.restorationScopeId,
super.defaultTransitionQuery,
super.reportsRouteUpdateToEngine = true,
super.backgroundWidget = const Scaffold(),
super.loggerAdapters = const [],
}) : super(nested: true);
}

/// [InheritedWidget] for placing [AppRouter] on the widget tree.
///
/// You can take the value of [AppRouter] passed here in [AppRouter.of].
Expand Down Expand Up @@ -525,7 +567,9 @@ class _AppRouterConfig {
this.defaultTransitionQuery,
this.backgroundWidget = const Scaffold(),
this.reportsRouteUpdateToEngine = true,
this.nested = false,
});
final bool nested;
final BootRouteQueryBuilder? boot;
final UnknownRouteQueryBuilder? unknown;
final List<RouteQueryBuilder> pages;
Expand Down
5 changes: 5 additions & 0 deletions packages/katana_router/lib/src/route_query.dart
Expand Up @@ -40,6 +40,11 @@ abstract class RouteQuery {
/// デフォルトは[path]と同じです。
String get name => path;

/// Returns `true` for nested pages (i.e., pages with no path information).
///
/// ネストされたページ(つまりパス情報が無いページ)の場合は`true`を返します。
bool get nested => false;

/// A key to identify the query.
///
/// It can be obtained as an object of [E] by specifying [E].
Expand Down
4 changes: 2 additions & 2 deletions packages/katana_router/pubspec.lock
Expand Up @@ -231,10 +231,10 @@ packages:
dependency: "direct main"
description:
name: katana_router_annotation
sha256: "52daf808cdeccf56586c26287784b8365897dac33d52e4cd00738f7cbcedff52"
sha256: "74e93bdc82781d22efa49c168925735f1f867eb76c8d4a8cf66975466dc238b5"
url: "https://pub.dev"
source: hosted
version: "1.11.0"
version: "1.11.1"
lints:
dependency: transitive
description:
Expand Down
9 changes: 9 additions & 0 deletions packages/katana_router_builder/lib/common/query_class.dart
Expand Up @@ -151,6 +151,15 @@ List<Class> queryClass(
annotation.name == null ? "path" : "\"${annotation.name}\"",
),
),
Method(
(m) => m
..name = "nested"
..annotations.addAll([const Reference("override")])
..type = MethodType.getter
..lambda = true
..returns = const Reference("bool")
..body = const Code("_path == null"),
),
Method(
(m) => m
..name = "key<E>"
Expand Down
12 changes: 6 additions & 6 deletions packages/katana_router_builder/pubspec.lock
Expand Up @@ -69,10 +69,10 @@ packages:
dependency: transitive
description:
name: built_value
sha256: "169565c8ad06adb760c3645bf71f00bff161b00002cace266cad42c5d22a7725"
sha256: "31b7c748fd4b9adf8d25d72a4c4a59ef119f12876cf414f94f8af5131d5fa2b0"
url: "https://pub.dev"
source: hosted
version: "8.4.3"
version: "8.4.4"
clock:
dependency: transitive
description:
Expand Down Expand Up @@ -125,10 +125,10 @@ packages:
dependency: "direct main"
description:
name: dart_style
sha256: "7a03456c3490394c8e7665890333e91ae8a49be43542b616e414449ac358acd4"
sha256: "5be16bf1707658e4c03078d4a9b90208ded217fb02c163e207d334082412f2fb"
url: "https://pub.dev"
source: hosted
version: "2.2.4"
version: "2.2.5"
encrypt:
dependency: transitive
description:
Expand Down Expand Up @@ -245,10 +245,10 @@ packages:
dependency: "direct main"
description:
name: katana_router_annotation
sha256: "52daf808cdeccf56586c26287784b8365897dac33d52e4cd00738f7cbcedff52"
sha256: "74e93bdc82781d22efa49c168925735f1f867eb76c8d4a8cf66975466dc238b5"
url: "https://pub.dev"
source: hosted
version: "1.11.0"
version: "1.11.1"
lints:
dependency: transitive
description:
Expand Down

0 comments on commit b83468b

Please sign in to comment.