From 4f472e556c1a8ce9c9404f482634c552b06f4609 Mon Sep 17 00:00:00 2001 From: Luke Freeman Date: Sun, 8 Mar 2020 11:15:54 -0700 Subject: [PATCH] wip - 2.0 --- lib/fluro.dart | 1 + lib/src/router.dart | 217 +------------------------------------------- pubspec.yaml | 2 + 3 files changed, 4 insertions(+), 216 deletions(-) diff --git a/lib/fluro.dart b/lib/fluro.dart index d015940..2598cb4 100644 --- a/lib/fluro.dart +++ b/lib/fluro.dart @@ -8,6 +8,7 @@ */ library fluro; +export 'package:dazza/dazza.dart'; export 'src/common.dart'; export 'src/router.dart'; export 'src/tree.dart'; diff --git a/lib/src/router.dart b/lib/src/router.dart index 0620079..d1b6aba 100644 --- a/lib/src/router.dart +++ b/lib/src/router.dart @@ -3,7 +3,7 @@ * Created by Yakka * https://theyakka.com * - * Copyright (c) 2019 Yakka, LLC. All rights reserved. + * Copyright (c) 2020 Yakka, LLC. All rights reserved. * See LICENSE for distribution and usage details. */ @@ -14,218 +14,3 @@ import 'package:fluro/src/common.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; - -class Router { - static final appRouter = Router(); - - /// The tree structure that stores the defined routes - final RouteTree _routeTree = RouteTree(); - - /// 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 default transition type. - void define(String routePath, - {@required Handler handler, TransitionType transitionType}) { - _routeTree.addRoute( - AppRoute(routePath, handler, transitionType: transitionType), - ); - } - - /// Finds a defined [AppRoute] for the path value. If no [AppRoute] definition was found - /// then function will return null. - AppRouteMatch match(String path) { - return _routeTree.matchRoute(path); - } - - void pop(BuildContext context) => Navigator.pop(context); - - /// - Future navigateTo(BuildContext context, String path, - {bool replace = false, - bool clearStack = false, - TransitionType transition, - Duration transitionDuration = const Duration(milliseconds: 250), - RouteTransitionsBuilder transitionBuilder}) { - RouteMatch routeMatch = matchRoute(context, path, - transitionType: transition, - transitionsBuilder: transitionBuilder, - transitionDuration: transitionDuration); - Route route = routeMatch.route; - Completer completer = Completer(); - Future future = completer.future; - if (routeMatch.matchType == RouteMatchType.nonVisual) { - completer.complete("Non visual route type."); - } else { - if (route == null && notFoundHandler != null) { - route = _notFoundRoute(context, path); - } - if (route != null) { - if (clearStack) { - future = - Navigator.pushAndRemoveUntil(context, route, (check) => false); - } else { - future = replace - ? Navigator.pushReplacement(context, route) - : Navigator.push(context, route); - } - completer.complete(); - } else { - String error = "No registered route was found to handle '$path'."; - print(error); - completer.completeError(RouteNotFoundException(error, path)); - } - } - - return future; - } - - /// - Route _notFoundRoute(BuildContext context, String path) { - RouteCreator creator = - (RouteSettings routeSettings, Map> parameters) { - return MaterialPageRoute( - settings: routeSettings, - builder: (BuildContext context) { - return notFoundHandler.handlerFunc(context, parameters); - }); - }; - return creator(RouteSettings(name: path), null); - } - - /// - RouteMatch matchRoute(BuildContext buildContext, String path, - {RouteSettings routeSettings, - TransitionType transitionType, - Duration transitionDuration = const Duration(milliseconds: 250), - RouteTransitionsBuilder transitionsBuilder}) { - RouteSettings settingsToUse = routeSettings; - if (routeSettings == null) { - settingsToUse = RouteSettings(name: path); - } - 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 RouteMatch( - matchType: RouteMatchType.noMatch, - errorMessage: "No matching route was found"); - } - Map> parameters = - match?.parameters ?? >{}; - if (handler.type == HandlerType.function) { - handler.handlerFunc(buildContext, parameters); - return RouteMatch(matchType: RouteMatchType.nonVisual); - } - - RouteCreator creator = - (RouteSettings routeSettings, Map> parameters) { - bool isNativeTransition = (transition == TransitionType.native || - transition == TransitionType.nativeModal); - if (isNativeTransition) { - if (Theme.of(buildContext).platform == TargetPlatform.iOS) { - return CupertinoPageRoute( - settings: routeSettings, - fullscreenDialog: transition == TransitionType.nativeModal, - builder: (BuildContext context) { - return handler.handlerFunc(context, parameters); - }); - } else { - return MaterialPageRoute( - settings: routeSettings, - fullscreenDialog: transition == TransitionType.nativeModal, - builder: (BuildContext context) { - return handler.handlerFunc(context, parameters); - }); - } - } else if (transition == TransitionType.material || - transition == TransitionType.materialFullScreenDialog) { - return MaterialPageRoute( - settings: routeSettings, - fullscreenDialog: - transition == TransitionType.materialFullScreenDialog, - builder: (BuildContext context) { - return handler.handlerFunc(context, parameters); - }); - } else if (transition == TransitionType.cupertino || - transition == TransitionType.cupertinoFullScreenDialog) { - return CupertinoPageRoute( - settings: routeSettings, - fullscreenDialog: - transition == TransitionType.cupertinoFullScreenDialog, - builder: (BuildContext context) { - return handler.handlerFunc(context, parameters); - }); - } else { - var routeTransitionsBuilder; - if (transition == TransitionType.custom) { - routeTransitionsBuilder = transitionsBuilder; - } else { - routeTransitionsBuilder = _standardTransitionsBuilder(transition); - } - return PageRouteBuilder( - settings: routeSettings, - pageBuilder: (BuildContext context, Animation animation, - Animation secondaryAnimation) { - return handler.handlerFunc(context, parameters); - }, - transitionDuration: transitionDuration, - transitionsBuilder: routeTransitionsBuilder, - ); - } - }; - return RouteMatch( - matchType: RouteMatchType.visual, - route: creator(settingsToUse, parameters), - ); - } - - RouteTransitionsBuilder _standardTransitionsBuilder( - TransitionType transitionType) { - return (BuildContext context, Animation animation, - Animation secondaryAnimation, Widget child) { - if (transitionType == TransitionType.fadeIn) { - return FadeTransition(opacity: animation, child: child); - } else { - const Offset topLeft = const Offset(0.0, 0.0); - const Offset topRight = const Offset(1.0, 0.0); - const Offset bottomLeft = const Offset(0.0, 1.0); - Offset startOffset = bottomLeft; - Offset endOffset = topLeft; - if (transitionType == TransitionType.inFromLeft) { - startOffset = const Offset(-1.0, 0.0); - endOffset = topLeft; - } else if (transitionType == TransitionType.inFromRight) { - startOffset = topRight; - endOffset = topLeft; - } - - return SlideTransition( - position: Tween( - 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. - Route generator(RouteSettings routeSettings) { - RouteMatch match = - matchRoute(null, routeSettings.name, routeSettings: routeSettings); - return match.route; - } - - /// Prints the route tree so you can analyze it. - void printTree() { - _routeTree.printTree(); - } -} diff --git a/pubspec.yaml b/pubspec.yaml index 253310d..617f8b9 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -17,6 +17,8 @@ dev_dependencies: flutter_test: sdk: flutter test: ^1.6.0 + dazza: + path: ../../dazza flutter: uses-material-design: false