Переглянути джерело

made Route<dynamic> [was <Null>], exposed Navigator.push Future<dynamic> [results]

Idan Aizik-Nissim 8 роки тому
батько
коміт
8c79b537d7

+ 9 - 3
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",

+ 23 - 2
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<double> animation, Animation<double> secondaryAnimation, Widget child) {
+          (BuildContext context, Animation<double> animation,
+          Animation<double> secondaryAnimation, Widget child) {
         return new ScaleTransition(
           scale: animation,
           child: new RotationTransition(

+ 2 - 1
example/lib/config/route_handlers.dart

@@ -14,11 +14,12 @@ import 'package:flutter/material.dart';
 var demoRouteHandler = new Handler(handlerFunc: (BuildContext context, Map<String, dynamic> 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,

+ 1 - 0
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';

+ 2 - 2
lib/src/common.dart

@@ -21,7 +21,7 @@ class Handler {
 }
 
 ///
-typedef Route<Null> RouteCreator(RouteSettings route, Map<String, dynamic> parameters);
+typedef Route<T> RouteCreator<T>(RouteSettings route, Map<String, dynamic> parameters);
 
 ///
 typedef Widget HandlerFunc(BuildContext context, Map<String, dynamic> parameters);
@@ -46,7 +46,7 @@ class RouteMatch {
     this.route = null,
     this.errorMessage = "Unable to match route. Please check the logs."
   });
-  final Route<Null> route;
+  final Route<dynamic> route;
   final RouteMatchType matchType;
   final String errorMessage;
 }

+ 20 - 12
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<Null> route = routeMatch.route;
+    Route<dynamic> 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<Null> _notFoundRoute(BuildContext context, String path) {
-    RouteCreator creator = (RouteSettings routeSettings, Map<String, dynamic> parameters) {
+    RouteCreator<Null> creator = (RouteSettings routeSettings, Map<String, dynamic> parameters) {
       return new MaterialPageRoute<Null>(settings: routeSettings, builder: (BuildContext context) {
         return notFoundHandler.handlerFunc(context, parameters);
       });
@@ -91,7 +99,7 @@ class Router {
     RouteCreator creator = (RouteSettings routeSettings, Map<String, dynamic> parameters) {
       bool isNativeTransition = (transitionType == TransitionType.native || transitionType == TransitionType.nativeModal);
       if (isNativeTransition) {
-        return new MaterialPageRoute<Null>(settings: routeSettings, fullscreenDialog: transitionType == TransitionType.nativeModal,
+        return new MaterialPageRoute<dynamic>(settings: routeSettings, fullscreenDialog: transitionType == TransitionType.nativeModal,
             builder: (BuildContext context) {
               return handler.handlerFunc(context, parameters);
             });