Skip to content

Instantly share code, notes, and snippets.

@mdebbar
Created March 13, 2020 07:08
Show Gist options
  • Select an option

  • Save mdebbar/e900c97208d4a9a3e8a051ce79866817 to your computer and use it in GitHub Desktop.

Select an option

Save mdebbar/e900c97208d4a9a3e8a051ce79866817 to your computer and use it in GitHub Desktop.
Combining route name parser with router delegate.
abstract class RoutingConfig {
RouteParsingResult parseRouteName(String routeName);
Widget build(BuildContext context);
}
class RouteParsingResult {
RouteParsingResult({});
/// The part of the route name that was matched/consumed by this routing node.
final String matchedRouteName;
/// The part of the route name that's remaining to be consumed by child
/// routing nodes.
final String remainingRouteName;
}
class DefaultRoutingConfig implements RoutingConfig {
DefaultRoutingConfig({this.routes});
final Map<String, RoutingConfig> routes;
RouteParsingResult parseRouteName(String routeName) {
for (final String key in routes) {
if (routeName.startsWith(key)) {
return RouteParsingResult(
matched: key,
remaining: routeName.substring(key.length),
childConfig: routes[key],
);
}
}
}
Widget build() { ... }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment