2017-05-04 15:33:40 -04:00
|
|
|
/*
|
|
|
|
* router
|
|
|
|
* A Posse Production
|
|
|
|
* http://goposse.com
|
|
|
|
* Copyright (c) 2017 Posse Productions LLC. All rights reserved.
|
|
|
|
* See LICENSE for distribution and usage details.
|
|
|
|
*/
|
2017-04-25 03:24:14 -04:00
|
|
|
part of router;
|
|
|
|
|
2017-05-04 15:27:32 -04:00
|
|
|
enum TransitionType {
|
|
|
|
native,
|
|
|
|
nativeModal,
|
|
|
|
}
|
|
|
|
|
2017-04-25 03:24:14 -04:00
|
|
|
class Router {
|
|
|
|
/// The tree structure that stores the defined routes
|
|
|
|
RouteTree _routeTree = new RouteTree();
|
|
|
|
|
|
|
|
/// Generic handler for when a route has not been defined
|
2017-05-04 15:27:32 -04:00
|
|
|
RouteHandler notFoundHandler;
|
2017-04-26 19:34:56 -04:00
|
|
|
|
|
|
|
/// Creates a [PageRoute] definition for the passed [RouteHandler]. You can optionally provide a custom
|
|
|
|
/// transition builder for the route.
|
2017-05-04 15:27:32 -04:00
|
|
|
void define(String routePath, {@required RouteHandler handler}) {
|
|
|
|
_routeTree.addRoute(new AppRoute(routePath, handler));
|
2017-04-25 03:24:14 -04:00
|
|
|
}
|
|
|
|
|
2017-05-04 15:27:32 -04:00
|
|
|
/// Finds a defined [AppRoute] for the path value. If no [AppRoute] definition was found
|
|
|
|
/// then function will return null.
|
2017-05-06 02:20:37 -04:00
|
|
|
AppRouteMatch match(String path) {
|
|
|
|
return _routeTree.matchRoute(path);
|
2017-04-25 03:24:14 -04:00
|
|
|
}
|
|
|
|
|
2017-05-04 15:27:32 -04:00
|
|
|
///
|
|
|
|
void navigateTo(BuildContext context, String path, {TransitionType transition = TransitionType.native}) {
|
|
|
|
Route<Null> route;
|
|
|
|
if (transition == TransitionType.native) {
|
|
|
|
route = matchRoute(path);
|
|
|
|
}
|
|
|
|
if (route == null && notFoundHandler != null) {
|
|
|
|
route = _notFoundRoute(context, path);
|
|
|
|
}
|
|
|
|
if (route != null) {
|
|
|
|
Navigator.push(context, route);
|
|
|
|
} else {
|
|
|
|
print("No registered route was found to handle '$path'.");
|
|
|
|
}
|
2017-04-25 03:24:14 -04:00
|
|
|
}
|
|
|
|
|
2017-05-04 15:27:32 -04:00
|
|
|
///
|
|
|
|
Route<Null> _notFoundRoute(BuildContext context, String path) {
|
|
|
|
RouteCreator creator = (RouteSettings routeSettings, Map<String, String> params) {
|
|
|
|
return new MaterialPageRoute<Null>(settings: routeSettings, builder: (BuildContext context) {
|
|
|
|
return notFoundHandler(params);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
return creator(new RouteSettings(name: path), null);
|
2017-04-25 03:24:14 -04:00
|
|
|
}
|
|
|
|
|
2017-05-04 15:27:32 -04:00
|
|
|
///
|
2017-04-26 19:57:21 -04:00
|
|
|
Route<Null> matchRoute(String path, {RouteSettings routeSettings = null}) {
|
|
|
|
RouteSettings settingsToUse = routeSettings;
|
|
|
|
if (routeSettings == null) {
|
|
|
|
settingsToUse = new RouteSettings(name: path);
|
|
|
|
}
|
|
|
|
AppRouteMatch match = _routeTree.matchRoute(path);
|
2017-05-04 15:27:32 -04:00
|
|
|
AppRoute route = match?.route;
|
|
|
|
if (route == null && notFoundHandler == null) {
|
2017-04-25 03:24:14 -04:00
|
|
|
return null;
|
|
|
|
}
|
2017-05-04 15:27:32 -04:00
|
|
|
RouteHandler handler = (route != null ? route.handler : notFoundHandler);
|
2017-04-25 03:24:14 -04:00
|
|
|
Map<String, String> parameters = match?.parameters ?? <String, String>{};
|
2017-05-04 15:27:32 -04:00
|
|
|
RouteCreator creator = (RouteSettings routeSettings, Map<String, String> params) {
|
|
|
|
return new MaterialPageRoute<Null>(settings: routeSettings, builder: (BuildContext context) {
|
|
|
|
return handler(params);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
return creator(settingsToUse, parameters);
|
2017-04-26 19:57:21 -04:00
|
|
|
}
|
|
|
|
|
2017-05-04 15:27:32 -04:00
|
|
|
/// Route generation method. This function can be used as a way to create routes on-the-fly
|
|
|
|
/// if any defined handler is found. It can also be used with the [MaterialApp.onGenerateRoute]
|
|
|
|
/// property as callback to create routes that can be used with the [Navigator] class.
|
2017-04-26 19:57:21 -04:00
|
|
|
Route<Null> generator(RouteSettings routeSettings) {
|
|
|
|
return matchRoute(routeSettings.name, routeSettings: routeSettings);
|
2017-04-25 03:24:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Prints the route tree so you can analyze it.
|
|
|
|
void printTree() {
|
|
|
|
_routeTree.printTree();
|
|
|
|
}
|
|
|
|
}
|