fluro_router.dart 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * fluro
  3. * Created by Yakka
  4. * https://theyakka.com
  5. *
  6. * Copyright (c) 2019 Yakka, LLC. All rights reserved.
  7. * See LICENSE for distribution and usage details.
  8. */
  9. import 'dart:async';
  10. import 'package:fluro/fluro.dart';
  11. import 'package:fluro/src/common.dart';
  12. import 'package:flutter/foundation.dart';
  13. import 'package:flutter/cupertino.dart';
  14. import 'package:flutter/material.dart';
  15. class FluroRouter {
  16. static final appRouter = FluroRouter();
  17. /// The tree structure that stores the defined routes
  18. final RouteTree _routeTree = RouteTree();
  19. /// Generic handler for when a route has not been defined
  20. Handler notFoundHandler;
  21. /// Creates a [PageRoute] definition for the passed [RouteHandler]. You can optionally provide a default transition type.
  22. void define(String routePath,
  23. {@required Handler handler, TransitionType transitionType}) {
  24. _routeTree.addRoute(
  25. AppRoute(routePath, handler, transitionType: transitionType),
  26. );
  27. }
  28. /// Finds a defined [AppRoute] for the path value. If no [AppRoute] definition was found
  29. /// then function will return null.
  30. AppRouteMatch match(String path) {
  31. return _routeTree.matchRoute(path);
  32. }
  33. void pop<T>(BuildContext context, [T result]) =>
  34. Navigator.pop(context, result);
  35. ///
  36. Future navigateTo(BuildContext context, String path,
  37. {bool replace = false,
  38. bool clearStack = false,
  39. TransitionType transition,
  40. Duration transitionDuration = const Duration(milliseconds: 250),
  41. RouteTransitionsBuilder transitionBuilder}) {
  42. RouteMatch routeMatch = matchRoute(context, path,
  43. transitionType: transition,
  44. transitionsBuilder: transitionBuilder,
  45. transitionDuration: transitionDuration);
  46. Route<dynamic> route = routeMatch.route;
  47. Completer completer = Completer();
  48. Future future = completer.future;
  49. if (routeMatch.matchType == RouteMatchType.nonVisual) {
  50. completer.complete("Non visual route type.");
  51. } else {
  52. if (route == null && notFoundHandler != null) {
  53. route = _notFoundRoute(context, path);
  54. }
  55. if (route != null) {
  56. if (clearStack) {
  57. future =
  58. Navigator.pushAndRemoveUntil(context, route, (check) => false);
  59. } else {
  60. future = replace
  61. ? Navigator.pushReplacement(context, route)
  62. : Navigator.push(context, route);
  63. }
  64. completer.complete();
  65. } else {
  66. String error = "No registered route was found to handle '$path'.";
  67. print(error);
  68. completer.completeError(RouteNotFoundException(error, path));
  69. }
  70. }
  71. return future;
  72. }
  73. ///
  74. Route<Null> _notFoundRoute(BuildContext context, String path) {
  75. RouteCreator<Null> creator =
  76. (RouteSettings routeSettings, Map<String, List<String>> parameters) {
  77. return MaterialPageRoute<Null>(
  78. settings: routeSettings,
  79. builder: (BuildContext context) {
  80. return notFoundHandler.handlerFunc(context, parameters);
  81. });
  82. };
  83. return creator(RouteSettings(name: path), null);
  84. }
  85. ///
  86. RouteMatch matchRoute(BuildContext buildContext, String path,
  87. {RouteSettings routeSettings,
  88. TransitionType transitionType,
  89. Duration transitionDuration = const Duration(milliseconds: 250),
  90. RouteTransitionsBuilder transitionsBuilder}) {
  91. RouteSettings settingsToUse = routeSettings;
  92. if (routeSettings == null) {
  93. settingsToUse = RouteSettings(name: path);
  94. }
  95. AppRouteMatch match = _routeTree.matchRoute(path);
  96. AppRoute route = match?.route;
  97. Handler handler = (route != null ? route.handler : notFoundHandler);
  98. var transition = transitionType;
  99. if (transitionType == null) {
  100. transition = route != null ? route.transitionType : TransitionType.native;
  101. }
  102. if (route == null && notFoundHandler == null) {
  103. return RouteMatch(
  104. matchType: RouteMatchType.noMatch,
  105. errorMessage: "No matching route was found");
  106. }
  107. Map<String, List<String>> parameters =
  108. match?.parameters ?? <String, List<String>>{};
  109. if (handler.type == HandlerType.function) {
  110. handler.handlerFunc(buildContext, parameters);
  111. return RouteMatch(matchType: RouteMatchType.nonVisual);
  112. }
  113. RouteCreator creator =
  114. (RouteSettings routeSettings, Map<String, List<String>> parameters) {
  115. bool isNativeTransition = (transition == TransitionType.native ||
  116. transition == TransitionType.nativeModal);
  117. if (isNativeTransition) {
  118. if (Theme.of(buildContext).platform == TargetPlatform.iOS) {
  119. return CupertinoPageRoute<dynamic>(
  120. settings: routeSettings,
  121. fullscreenDialog: transition == TransitionType.nativeModal,
  122. builder: (BuildContext context) {
  123. return handler.handlerFunc(context, parameters);
  124. });
  125. } else {
  126. return MaterialPageRoute<dynamic>(
  127. settings: routeSettings,
  128. fullscreenDialog: transition == TransitionType.nativeModal,
  129. builder: (BuildContext context) {
  130. return handler.handlerFunc(context, parameters);
  131. });
  132. }
  133. } else if (transition == TransitionType.material ||
  134. transition == TransitionType.materialFullScreenDialog) {
  135. return MaterialPageRoute<dynamic>(
  136. settings: routeSettings,
  137. fullscreenDialog:
  138. transition == TransitionType.materialFullScreenDialog,
  139. builder: (BuildContext context) {
  140. return handler.handlerFunc(context, parameters);
  141. });
  142. } else if (transition == TransitionType.cupertino ||
  143. transition == TransitionType.cupertinoFullScreenDialog) {
  144. return CupertinoPageRoute<dynamic>(
  145. settings: routeSettings,
  146. fullscreenDialog:
  147. transition == TransitionType.cupertinoFullScreenDialog,
  148. builder: (BuildContext context) {
  149. return handler.handlerFunc(context, parameters);
  150. });
  151. } else {
  152. var routeTransitionsBuilder;
  153. if (transition == TransitionType.custom) {
  154. routeTransitionsBuilder = transitionsBuilder;
  155. } else {
  156. routeTransitionsBuilder = _standardTransitionsBuilder(transition);
  157. }
  158. return PageRouteBuilder<dynamic>(
  159. settings: routeSettings,
  160. pageBuilder: (BuildContext context, Animation<double> animation,
  161. Animation<double> secondaryAnimation) {
  162. return handler.handlerFunc(context, parameters);
  163. },
  164. transitionDuration: transitionDuration,
  165. transitionsBuilder: routeTransitionsBuilder,
  166. );
  167. }
  168. };
  169. return RouteMatch(
  170. matchType: RouteMatchType.visual,
  171. route: creator(settingsToUse, parameters),
  172. );
  173. }
  174. RouteTransitionsBuilder _standardTransitionsBuilder(
  175. TransitionType transitionType) {
  176. return (BuildContext context, Animation<double> animation,
  177. Animation<double> secondaryAnimation, Widget child) {
  178. if (transitionType == TransitionType.fadeIn) {
  179. return FadeTransition(opacity: animation, child: child);
  180. } else {
  181. const Offset topLeft = const Offset(0.0, 0.0);
  182. const Offset topRight = const Offset(1.0, 0.0);
  183. const Offset bottomLeft = const Offset(0.0, 1.0);
  184. Offset startOffset = bottomLeft;
  185. Offset endOffset = topLeft;
  186. if (transitionType == TransitionType.inFromLeft) {
  187. startOffset = const Offset(-1.0, 0.0);
  188. endOffset = topLeft;
  189. } else if (transitionType == TransitionType.inFromRight) {
  190. startOffset = topRight;
  191. endOffset = topLeft;
  192. }
  193. return SlideTransition(
  194. position: Tween<Offset>(
  195. begin: startOffset,
  196. end: endOffset,
  197. ).animate(animation),
  198. child: child,
  199. );
  200. }
  201. };
  202. }
  203. /// Route generation method. This function can be used as a way to create routes on-the-fly
  204. /// if any defined handler is found. It can also be used with the [MaterialApp.onGenerateRoute]
  205. /// property as callback to create routes that can be used with the [Navigator] class.
  206. Route<dynamic> generator(RouteSettings routeSettings) {
  207. RouteMatch match =
  208. matchRoute(null, routeSettings.name, routeSettings: routeSettings);
  209. return match.route;
  210. }
  211. /// Prints the route tree so you can analyze it.
  212. void printTree() {
  213. _routeTree.printTree();
  214. }
  215. }