Created
March 13, 2020 07:08
-
-
Save mdebbar/e900c97208d4a9a3e8a051ce79866817 to your computer and use it in GitHub Desktop.
Combining route name parser with router delegate.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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