Skip to content
Open
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
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ packages:
path: ".."
relative: true
source: path
version: "1.2.6"
version: "1.2.8"
sky_engine:
dependency: transitive
description: flutter
Expand Down
50 changes: 45 additions & 5 deletions lib/src/router_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,11 @@ class RouterController<T> with ChangeNotifier {
}
}

Map<String, dynamic> getArguments({required Map<String, Handler> allRoutes}) {
String? pathUrl = _getPathUrlOrigin();
Map<String, dynamic> getArguments({
required Map<String, Handler> allRoutes,
String? path,
}) {
String? pathUrl = _getPathUrlOrigin(path: path);
if (pathUrl == null) return {};

List<String> args =
Expand Down Expand Up @@ -183,9 +186,21 @@ class RouterController<T> with ChangeNotifier {
break;
}
}

final Map<String, String> extractedArgs = extractRouteArguments(
routePattern: router,
pathUrl: pathUrl,
);

final Map<String, String> mergedArgs = Map<String, String>.from(mappedArgs);

extractedArgs.forEach((key, value) {
mergedArgs.putIfAbsent(key, () => value);
});

return {
"pageRouter": routerPageName,
"arguments": mappedArgs,
"arguments": mergedArgs,
"urlPage": pathUrl,
};
}
Expand Down Expand Up @@ -271,8 +286,8 @@ class RouterController<T> with ChangeNotifier {
}
}

String? _getPathUrlOrigin() {
String? pathUrl = web.window.location.href;
String? _getPathUrlOrigin({String? path}) {
String? pathUrl = path ?? web.window.location.href;
if (pathUrl == null) return null;

Uri uri = Uri.parse(pathUrl);
Expand All @@ -285,6 +300,31 @@ class RouterController<T> with ChangeNotifier {
return pathUrl;
}

Map<String, String> extractRouteArguments({
required String? routePattern,
required String pathUrl,
}) {
if (routePattern == null) return {};

final routeSegments =
routePattern.split('/').where((e) => e.isNotEmpty).toList();

final pathSegments = pathUrl.split('/').where((e) => e.isNotEmpty).toList();

final Map<String, String> args = {};

for (int i = 0; i < routeSegments.length && i < pathSegments.length; i++) {
final routeSegment = routeSegments[i];

if (routeSegment.startsWith(':')) {
final key = routeSegment.substring(1);
args[key] = pathSegments[i];
}
}

return args;
}

@override
void dispose() {
_disposed = true;
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: router_controller
description: A new Flutter project.
version: 1.2.8
version: 1.2.9

environment:
sdk: '>=3.7.0 <4.0.0'
Expand Down