diff --git a/lib/src/common.dart b/lib/src/common.dart index 402dccc..450ce13 100644 --- a/lib/src/common.dart +++ b/lib/src/common.dart @@ -25,13 +25,13 @@ class Handler { /// A function that creates new routes. typedef Route RouteCreator( - RouteSettings route, Map>? parameters); + RouteSettings route, Map> parameters); /// Builds out a screen based on string path [parameters] and context. /// /// Note: you can access [RouteSettings] with the [context.settings] extension typedef Widget? HandlerFunc( - BuildContext? context, Map>? parameters); + BuildContext? context, Map> parameters); /// A route that is added to the router tree. class AppRoute { diff --git a/lib/src/fluro_router.dart b/lib/src/fluro_router.dart index 163863b..c0e387c 100644 --- a/lib/src/fluro_router.dart +++ b/lib/src/fluro_router.dart @@ -34,11 +34,14 @@ class FluroRouter { /// Generic handler for when a route has not been defined Handler? notFoundHandler; + /// The default transition duration to use throughout Fluro + static const defaultTransitionDuration = const Duration(milliseconds: 250); + /// 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, - Duration? transitionDuration = const Duration(milliseconds: 250), + Duration transitionDuration = defaultTransitionDuration, RouteTransitionsBuilder? transitionBuilder}) { _routeTree.addRoute( AppRoute(routePath, handler, @@ -94,7 +97,7 @@ class FluroRouter { } completer.complete(); } else { - String error = "No registered route was found to handle '$path'."; + final error = "No registered route was found to handle '$path'."; print(error); completer.completeError(RouteNotFoundException(error, path)); } @@ -106,16 +109,16 @@ class FluroRouter { Route _notFoundRoute(BuildContext context, String path, {bool? maintainState}) { RouteCreator creator = - (RouteSettings? routeSettings, Map>? parameters) { + (RouteSettings? routeSettings, Map> parameters) { return MaterialPageRoute( settings: routeSettings, maintainState: maintainState ?? true, builder: (BuildContext context) { - return notFoundHandler!.handlerFunc(context, parameters) ?? + return notFoundHandler?.handlerFunc(context, parameters) ?? SizedBox.shrink(); }); }; - return creator(RouteSettings(name: path), null); + return creator(RouteSettings(name: path), {}); } /// Attempt to match a route to the provided [path]. @@ -125,11 +128,9 @@ class FluroRouter { Duration? transitionDuration, RouteTransitionsBuilder? transitionsBuilder, bool maintainState = true}) { - RouteSettings? settingsToUse = routeSettings; - if (routeSettings == null) { - settingsToUse = RouteSettings(name: path); - } - if (settingsToUse!.name == null) { + RouteSettings settingsToUse = routeSettings ?? RouteSettings(name: path); + + if (settingsToUse.name == null) { settingsToUse = settingsToUse.copyWith(name: path); } AppRouteMatch? match = _routeTree.matchRoute(path!); @@ -157,7 +158,7 @@ class FluroRouter { } RouteCreator creator = - (RouteSettings? routeSettings, Map>? parameters) { + (RouteSettings? routeSettings, Map> parameters) { bool isNativeTransition = (transition == TransitionType.native || transition == TransitionType.nativeModal); if (isNativeTransition) { @@ -211,10 +212,14 @@ class FluroRouter { }, transitionDuration: transition == TransitionType.none ? Duration.zero - : (transitionDuration ?? route?.transitionDuration)!, + : (transitionDuration ?? + route?.transitionDuration ?? + defaultTransitionDuration), reverseTransitionDuration: transition == TransitionType.none ? Duration.zero - : (transitionDuration ?? route?.transitionDuration)!, + : (transitionDuration ?? + route?.transitionDuration ?? + defaultTransitionDuration), transitionsBuilder: transition == TransitionType.none ? (_, __, ___, child) => child : routeTransitionsBuilder!, diff --git a/lib/src/tree.dart b/lib/src/tree.dart index d1edd3f..965ee48 100644 --- a/lib/src/tree.dart +++ b/lib/src/tree.dart @@ -51,8 +51,8 @@ class RouteTreeNode { // properties String part; RouteTreeNodeType? type; - List? routes = []; - List? nodes = []; + List routes = []; + List nodes = []; RouteTreeNode? parent; bool isParameter() { @@ -97,15 +97,11 @@ class RouteTree { if (parent == null) { _nodes.add(node); } else { - parent.nodes!.add(node); + parent.nodes.add(node); } } if (i == pathComponents.length - 1) { - if (node.routes == null) { - node.routes = [route]; - } else { - node.routes!.add(route); - } + node.routes.add(route); } parent = node; } @@ -149,9 +145,7 @@ class RouteTree { match.parameters.addAll(queryMap); } currentMatches[node] = match; - if (node.nodes != null) { - nextNodes.addAll(node.nodes!); - } + nextNodes.addAll(node.nodes); } } nodeMatches = currentMatches; @@ -161,11 +155,11 @@ class RouteTree { } } List matches = nodeMatches.values.toList(); - if (matches.length > 0) { + if (matches.isNotEmpty) { RouteTreeNodeMatch match = matches.first; RouteTreeNode? nodeToUse = match.node; final routes = nodeToUse.routes; - if (routes != null && routes.length > 0) { + if (routes.isNotEmpty) { AppRouteMatch routeMatch = AppRouteMatch(routes[0]); routeMatch.parameters = match.parameters; return routeMatch; @@ -179,14 +173,14 @@ class RouteTree { } void _printSubTree({RouteTreeNode? parent, int level = 0}) { - List? nodes = parent != null ? parent.nodes : _nodes; - for (RouteTreeNode node in nodes!) { + List nodes = parent != null ? parent.nodes : _nodes; + for (RouteTreeNode node in nodes) { String indent = ""; for (int i = 0; i < level; i++) { indent += " "; } - print("$indent${node.part}: total routes=${node.routes!.length}"); - if (node.nodes != null && node.nodes!.length > 0) { + print("$indent${node.part}: total routes=${node.routes.length}"); + if (node.nodes.isNotEmpty) { _printSubTree(parent: node, level: level + 1); } } @@ -196,7 +190,7 @@ class RouteTree { List nodes = _nodes; if (parent != null) { // search parent for sub-node matches - nodes = parent.nodes!; + nodes = parent.nodes; } for (RouteTreeNode node in nodes) { if (node.part == component) { @@ -220,13 +214,14 @@ class RouteTree { } Map> parseQueryString(String query) { - var search = RegExp('([^&=]+)=?([^&]*)'); - var params = Map>(); + final search = RegExp('([^&=]+)=?([^&]*)'); + final params = Map>(); if (query.startsWith('?')) query = query.substring(1); decode(String s) => Uri.decodeComponent(s.replaceAll('+', ' ')); for (Match match in search.allMatches(query)) { String key = decode(match.group(1)!); String value = decode(match.group(2)!); + if (params.containsKey(key)) { params[key]!.add(value); } else {