From 8c79b537d7da1ba91655029b7d9f59dc75fd26e9 Mon Sep 17 00:00:00 2001 From: Idan Aizik-Nissim Date: Sat, 14 Oct 2017 03:10:21 +0300 Subject: [PATCH] made Route [was ], exposed Navigator.push Future [results] --- .../demo/demo_simple_component.dart | 12 +++++-- .../lib/components/home/home_component.dart | 25 +++++++++++++-- example/lib/config/route_handlers.dart | 3 +- lib/fluro.dart | 1 + lib/src/common.dart | 4 +-- lib/src/router.dart | 32 ++++++++++++------- 6 files changed, 57 insertions(+), 20 deletions(-) diff --git a/example/lib/components/demo/demo_simple_component.dart b/example/lib/components/demo/demo_simple_component.dart index 71414cd..858fe71 100644 --- a/example/lib/components/demo/demo_simple_component.dart +++ b/example/lib/components/demo/demo_simple_component.dart @@ -10,12 +10,14 @@ import 'package:flutter/material.dart'; class DemoSimpleComponent extends StatelessWidget { - DemoSimpleComponent({String message = "Testing", Color color = const Color(0xFFFFFFFF)}) + DemoSimpleComponent({String message = "Testing", Color color = const Color(0xFFFFFFFF), String result}) : this.message = message, - this.color = color; + this.color = color, + this.result = result; final String message; final Color color; + final String result; @override Widget build(BuildContext context) { @@ -44,7 +46,11 @@ class DemoSimpleComponent extends StatelessWidget { padding: new EdgeInsets.only(top: 15.0), child: new FlatButton( onPressed: () { - Navigator.pop(context); + if (result == null) { + Navigator.pop(context); + } else { + Navigator.pop(context, result); + } }, child: new Text( "OK", diff --git a/example/lib/components/home/home_component.dart b/example/lib/components/home/home_component.dart index cba3bf7..032bef3 100644 --- a/example/lib/components/home/home_component.dart +++ b/example/lib/components/home/home_component.dart @@ -23,6 +23,7 @@ class HomeComponent extends StatelessWidget { menuButton(context, "Preset (Fade In)", "preset-fade"), menuButton(context, "Custom Transition", "custom"), menuButton(context, "Function Call", "function-call"), + menuButton(context, "Navigator Result", "pop-result"), new Padding( padding: new EdgeInsets.only(top: 65.0, left: 60.0, right: 60.0), child: new Center( @@ -80,6 +81,7 @@ class HomeComponent extends StatelessWidget { void tappedMenuButton(BuildContext context, String key) { String message = ""; String hexCode = "#FFFFFF"; + String result; TransitionType transitionType = TransitionType.native; if (key != "custom" && key != "function-call") { if (key == "native") { @@ -93,13 +95,32 @@ class HomeComponent extends StatelessWidget { hexCode = "#F700D2"; message = "This screen should have appeared with a fade in transition"; transitionType = TransitionType.fadeIn; + } else if (key == "pop-result") { + transitionType = TransitionType.native; + hexCode = "#407F7F"; + message = "This screen should return the current weekday"; + result = new DateTime.now().weekday.toString(); } - Application.router.navigateTo(context, "/demo?message=$message&color_hex=$hexCode", transition: transitionType); + + String route = "/demo?message=$message&color_hex=$hexCode"; + + if (result != null) { + route = "$route&result=$result"; + } + + Application.router.navigateTo( + context, route, + transition: transitionType).then((result) { + if (key == "pop-result") { + Application.router.navigateTo(context, "/demo/func?message=$result"); + } + }); } else if (key == "custom") { hexCode = "#DFF700"; message = "This screen should have appeared with a crazy custom transition"; var transition = - (BuildContext context, Animation animation, Animation secondaryAnimation, Widget child) { + (BuildContext context, Animation animation, + Animation secondaryAnimation, Widget child) { return new ScaleTransition( scale: animation, child: new RotationTransition( diff --git a/example/lib/config/route_handlers.dart b/example/lib/config/route_handlers.dart index ad3df36..c564223 100644 --- a/example/lib/config/route_handlers.dart +++ b/example/lib/config/route_handlers.dart @@ -14,11 +14,12 @@ import 'package:flutter/material.dart'; var demoRouteHandler = new Handler(handlerFunc: (BuildContext context, Map params) { String message = params["message"]; String colorHex = params["color_hex"]; + String result = params["result"]; Color color = new Color(0xFFFFFFFF); if (colorHex != null && colorHex.length > 0) { color = new Color(ColorHelpers.fromHexString(colorHex)); } - return new DemoSimpleComponent(message: message, color: color); + return new DemoSimpleComponent(message: message, color: color, result: result); }); var demoFunctionHandler = new Handler(type: HandlerType.function, diff --git a/lib/fluro.dart b/lib/fluro.dart index 2d064df..4258c51 100644 --- a/lib/fluro.dart +++ b/lib/fluro.dart @@ -7,6 +7,7 @@ */ library fluro; +import 'dart:async'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter/widgets.dart'; diff --git a/lib/src/common.dart b/lib/src/common.dart index ec2b69b..1e62d0d 100644 --- a/lib/src/common.dart +++ b/lib/src/common.dart @@ -21,7 +21,7 @@ class Handler { } /// -typedef Route RouteCreator(RouteSettings route, Map parameters); +typedef Route RouteCreator(RouteSettings route, Map parameters); /// typedef Widget HandlerFunc(BuildContext context, Map parameters); @@ -46,7 +46,7 @@ class RouteMatch { this.route = null, this.errorMessage = "Unable to match route. Please check the logs." }); - final Route route; + final Route route; final RouteMatchType matchType; final String errorMessage; } \ No newline at end of file diff --git a/lib/src/router.dart b/lib/src/router.dart index f4b8867..7ba81d1 100644 --- a/lib/src/router.dart +++ b/lib/src/router.dart @@ -37,29 +37,37 @@ class Router { } /// - void navigateTo(BuildContext context, String path, {bool replace = false, TransitionType transition = TransitionType.native, + Future navigateTo(BuildContext context, String path, {bool replace = false, TransitionType transition = TransitionType.native, Duration transitionDuration = const Duration(milliseconds: 250), RouteTransitionsBuilder transitionBuilder}) { RouteMatch routeMatch = matchRoute(context, path, transitionType: transition, transitionsBuilder: transitionBuilder, transitionDuration: transitionDuration); - Route route = routeMatch.route; + Route route = routeMatch.route; + Completer completer = new Completer(); + Future future = completer.future; if (routeMatch.matchType == RouteMatchType.nonVisual) { - return; - } - if (route == null && notFoundHandler != null) { - route = _notFoundRoute(context, path); - } - if (route != null) { - replace ? Navigator.pushReplacement(context, route) : Navigator.push(context, route); + completer.complete("Non visual route type."); } else { - print("No registered route was found to handle '$path'."); + if (route == null && notFoundHandler != null) { + route = _notFoundRoute(context, path); + } + if (route != null) { + 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(error); + } } + + return future; } /// Route _notFoundRoute(BuildContext context, String path) { - RouteCreator creator = (RouteSettings routeSettings, Map parameters) { + RouteCreator creator = (RouteSettings routeSettings, Map parameters) { return new MaterialPageRoute(settings: routeSettings, builder: (BuildContext context) { return notFoundHandler.handlerFunc(context, parameters); }); @@ -91,7 +99,7 @@ class Router { RouteCreator creator = (RouteSettings routeSettings, Map parameters) { bool isNativeTransition = (transitionType == TransitionType.native || transitionType == TransitionType.nativeModal); if (isNativeTransition) { - return new MaterialPageRoute(settings: routeSettings, fullscreenDialog: transitionType == TransitionType.nativeModal, + return new MaterialPageRoute(settings: routeSettings, fullscreenDialog: transitionType == TransitionType.nativeModal, builder: (BuildContext context) { return handler.handlerFunc(context, parameters); });