diff --git a/.gitignore b/.gitignore index e050481..ada8eb5 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ pubspec.lock coverage __temp_coverage* .dart_tool/ +build/ diff --git a/README.md b/README.md index be19550..2baace2 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,9 @@ var usersHandler = Handler(handlerFunc: (BuildContext context, Map { Widget build(BuildContext context) { final app = new MaterialApp( title: 'Fluro', + debugShowCheckedModeBanner: false, theme: new ThemeData( primarySwatch: Colors.blue, ), diff --git a/example/lib/components/home/home_component.dart b/example/lib/components/home/home_component.dart index 14fcea5..c98d63e 100644 --- a/example/lib/components/home/home_component.dart +++ b/example/lib/components/home/home_component.dart @@ -100,8 +100,8 @@ class HomeComponentState extends State { width: 200.0), ), menuButton(context, "Native Animation", "native"), - menuButton(context, "Preset (In from Left)", "preset-from-left"), menuButton(context, "Preset (Fade In)", "preset-fade"), + menuButton(context, "Preset (Global transition)", "fixed-trans"), menuButton(context, "Custom Transition", "custom"), menuButton(context, "Navigator Result", "pop-result"), menuButton(context, "Function Call", "function-call"), @@ -154,7 +154,7 @@ class HomeComponentState extends State { String hexCode = "#FFFFFF"; String result; TransitionType transitionType = TransitionType.native; - if (key != "custom" && key != "function-call") { + if (key != "custom" && key != "function-call" && key != "fixed-trans") { if (key == "native") { hexCode = "#F76F00"; message = @@ -210,6 +210,9 @@ class HomeComponentState extends State { transitionBuilder: transition, transitionDuration: const Duration(milliseconds: 600), ); + } else if (key == "fixed-trans") { + Application.router.navigateTo( + context, "/demo/fixedtrans?message=Hello!&color_hex=#f4424b"); } else { message = "You tapped the function button!"; Application.router.navigateTo(context, "/demo/func?message=$message"); diff --git a/example/lib/config/routes.dart b/example/lib/config/routes.dart index 10032c1..536f362 100644 --- a/example/lib/config/routes.dart +++ b/example/lib/config/routes.dart @@ -13,6 +13,7 @@ import './route_handlers.dart'; class Routes { static String root = "/"; static String demoSimple = "/demo"; + static String demoSimpleFixedTrans = "/demo/fixedtrans"; static String demoFunc = "/demo/func"; static String deepLink = "/message"; @@ -23,6 +24,8 @@ class Routes { }); router.define(root, handler: rootHandler); router.define(demoSimple, handler: demoRouteHandler); + router.define(demoSimpleFixedTrans, + handler: demoRouteHandler, transitionType: TransitionType.inFromLeft); router.define(demoFunc, handler: demoFunctionHandler); router.define(deepLink, handler: deepLinkHandler); } diff --git a/lib/src/common.dart b/lib/src/common.dart index 1c2aceb..8e3e64c 100644 --- a/lib/src/common.dart +++ b/lib/src/common.dart @@ -34,7 +34,18 @@ typedef Widget HandlerFunc( class AppRoute { String route; dynamic handler; - AppRoute(this.route, this.handler); + TransitionType transitionType; + AppRoute(this.route, this.handler, {this.transitionType}); +} + +enum TransitionType { + native, + nativeModal, + inFromLeft, + inFromRight, + inFromBottom, + fadeIn, + custom, // if using custom then you must also provide a transition } enum RouteMatchType { diff --git a/lib/src/router.dart b/lib/src/router.dart index 4b5d47a..87ca55b 100644 --- a/lib/src/router.dart +++ b/lib/src/router.dart @@ -13,16 +13,6 @@ import 'package:fluro/fluro.dart'; import 'package:fluro/src/common.dart'; import 'package:flutter/material.dart'; -enum TransitionType { - native, - nativeModal, - inFromLeft, - inFromRight, - inFromBottom, - fadeIn, - custom, // if using custom then you must also provide a transition -} - class Router { static final appRouter = new Router(); @@ -32,10 +22,11 @@ class Router { /// Generic handler for when a route has not been defined Handler notFoundHandler; - /// Creates a [PageRoute] definition for the passed [RouteHandler]. You can optionally provide a custom - /// transition builder for the route. - void define(String routePath, {@required Handler handler}) { - _routeTree.addRoute(new AppRoute(routePath, handler)); + /// Creates a [PageRoute] definition for the passed [RouteHandler]. You can optionally provide a default transition type. + void define(String routePath, + {@required Handler handler, TransitionType transitionType}) { + _routeTree.addRoute( + new AppRoute(routePath, handler, transitionType: transitionType)); } /// Finds a defined [AppRoute] for the path value. If no [AppRoute] definition was found @@ -50,7 +41,7 @@ class Router { Future navigateTo(BuildContext context, String path, {bool replace = false, bool clearStack = false, - TransitionType transition = TransitionType.native, + TransitionType transition, Duration transitionDuration = const Duration(milliseconds: 250), RouteTransitionsBuilder transitionBuilder}) { RouteMatch routeMatch = matchRoute(context, path, @@ -112,6 +103,10 @@ class Router { AppRouteMatch match = _routeTree.matchRoute(path); AppRoute route = match?.route; Handler handler = (route != null ? route.handler : notFoundHandler); + var transition = transitionType; + if (transitionType == null) { + transition = route != null ? route.transitionType : TransitionType.native; + } if (route == null && notFoundHandler == null) { return new RouteMatch( matchType: RouteMatchType.noMatch, @@ -126,21 +121,21 @@ class Router { RouteCreator creator = (RouteSettings routeSettings, Map> parameters) { - bool isNativeTransition = (transitionType == TransitionType.native || - transitionType == TransitionType.nativeModal); + bool isNativeTransition = (transition == TransitionType.native || + transition == TransitionType.nativeModal); if (isNativeTransition) { return new MaterialPageRoute( settings: routeSettings, - fullscreenDialog: transitionType == TransitionType.nativeModal, + fullscreenDialog: transition == TransitionType.nativeModal, builder: (BuildContext context) { return handler.handlerFunc(context, parameters); }); } else { var routeTransitionsBuilder; - if (transitionType == TransitionType.custom) { + if (transition == TransitionType.custom) { routeTransitionsBuilder = transitionsBuilder; } else { - routeTransitionsBuilder = _standardTransitionsBuilder(transitionType); + routeTransitionsBuilder = _standardTransitionsBuilder(transition); } return new PageRouteBuilder( settings: routeSettings, diff --git a/test/parser_test.dart b/test/parser_test.dart index 323a591..6209b23 100644 --- a/test/parser_test.dart +++ b/test/parser_test.dart @@ -72,4 +72,14 @@ void main() { "number": ["7", "10", "13"], })); }); + testWidgets("Router correctly matches route and transition type", + (WidgetTester tester) async { + String path = "/users/1234"; + String route = "/users/:id"; + Router router = new Router(); + router.define(route, + handler: null, transitionType: TransitionType.inFromRight); + AppRouteMatch match = router.match(path); + expect(TransitionType.inFromRight, match.route.transitionType); + }); }