|
|
@@ -10,6 +10,11 @@ part of router;
|
|
|
enum TransitionType {
|
|
|
native,
|
|
|
nativeModal,
|
|
|
+ inFromLeft,
|
|
|
+ inFromRight,
|
|
|
+ inFromBottom,
|
|
|
+ fadeIn,
|
|
|
+ custom, // if using custom then you must also provide a transition
|
|
|
}
|
|
|
|
|
|
class Router {
|
|
|
@@ -32,11 +37,12 @@ class Router {
|
|
|
}
|
|
|
|
|
|
///
|
|
|
- void navigateTo(BuildContext context, String path, {TransitionType transition = TransitionType.native}) {
|
|
|
- Route<Null> route;
|
|
|
- if (transition == TransitionType.native) {
|
|
|
- route = matchRoute(path);
|
|
|
- }
|
|
|
+ void navigateTo(BuildContext context, String path, {TransitionType transition = TransitionType.native,
|
|
|
+ Duration transitionDuration = const Duration(milliseconds: 250),
|
|
|
+ RouteTransitionsBuilder transitionBuilder})
|
|
|
+ {
|
|
|
+ Route<Null> route = matchRoute(path, transitionType: transition,
|
|
|
+ transitionsBuilder: transitionBuilder, transitionDuration: transitionDuration);
|
|
|
if (route == null && notFoundHandler != null) {
|
|
|
route = _notFoundRoute(context, path);
|
|
|
}
|
|
|
@@ -58,7 +64,10 @@ class Router {
|
|
|
}
|
|
|
|
|
|
///
|
|
|
- Route<Null> matchRoute(String path, {RouteSettings routeSettings = null}) {
|
|
|
+ Route<Null> matchRoute(String path, {RouteSettings routeSettings = null,
|
|
|
+ TransitionType transitionType, Duration transitionDuration = const Duration(milliseconds: 250),
|
|
|
+ RouteTransitionsBuilder transitionsBuilder})
|
|
|
+ {
|
|
|
RouteSettings settingsToUse = routeSettings;
|
|
|
if (routeSettings == null) {
|
|
|
settingsToUse = new RouteSettings(name: path);
|
|
|
@@ -71,13 +80,57 @@ class Router {
|
|
|
RouteHandler handler = (route != null ? route.handler : notFoundHandler);
|
|
|
Map<String, String> parameters = match?.parameters ?? <String, String>{};
|
|
|
RouteCreator creator = (RouteSettings routeSettings, Map<String, String> params) {
|
|
|
- return new MaterialPageRoute<Null>(settings: routeSettings, builder: (BuildContext context) {
|
|
|
- return handler(params);
|
|
|
- });
|
|
|
+ bool isNativeTransition = (transitionType == TransitionType.native || transitionType == TransitionType.nativeModal);
|
|
|
+ if (isNativeTransition) {
|
|
|
+ return new MaterialPageRoute<Null>(settings: routeSettings, fullscreenDialog: transitionType == TransitionType.nativeModal,
|
|
|
+ builder: (BuildContext context) {
|
|
|
+ return handler(params);
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ var routeTransitionsBuilder;
|
|
|
+ if (transitionType == TransitionType.custom) {
|
|
|
+ routeTransitionsBuilder = transitionsBuilder;
|
|
|
+ } else {
|
|
|
+ routeTransitionsBuilder = _standardTransitionsBuilder(transitionType);
|
|
|
+ }
|
|
|
+ return new PageRouteBuilder<Null>(settings: routeSettings,
|
|
|
+ pageBuilder: (BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation) {
|
|
|
+ return handler(params);
|
|
|
+ },
|
|
|
+ transitionDuration: transitionDuration,
|
|
|
+ transitionsBuilder: routeTransitionsBuilder,
|
|
|
+ );
|
|
|
+ }
|
|
|
};
|
|
|
return creator(settingsToUse, parameters);
|
|
|
}
|
|
|
|
|
|
+ RouteTransitionsBuilder _standardTransitionsBuilder(TransitionType transitionType) {
|
|
|
+ return (BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation, Widget child) {
|
|
|
+ if (transitionType == TransitionType.fadeIn) {
|
|
|
+ return new FadeTransition(opacity: animation, child: child);
|
|
|
+ } else {
|
|
|
+ FractionalOffset startOffset = FractionalOffset.bottomLeft;
|
|
|
+ FractionalOffset endOffset = FractionalOffset.topLeft;
|
|
|
+ if (transitionType == TransitionType.inFromLeft) {
|
|
|
+ startOffset = new FractionalOffset(-1.0, 0.0);
|
|
|
+ endOffset = FractionalOffset.topLeft;
|
|
|
+ } else if (transitionType == TransitionType.inFromRight) {
|
|
|
+ startOffset = FractionalOffset.topRight;
|
|
|
+ endOffset = FractionalOffset.topLeft;
|
|
|
+ }
|
|
|
+
|
|
|
+ return new SlideTransition(
|
|
|
+ position: new FractionalOffsetTween(
|
|
|
+ begin: startOffset,
|
|
|
+ end: endOffset,
|
|
|
+ ).animate(animation),
|
|
|
+ child: child,
|
|
|
+ );
|
|
|
+ }
|
|
|
+ };
|
|
|
+ }
|
|
|
+
|
|
|
/// 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.
|