Merge pull request #28 from BBB/feature/route_result
Navigator.pop results
This commit is contained in:
commit
b5ab39169f
|
@ -10,12 +10,14 @@ import 'package:flutter/material.dart';
|
||||||
|
|
||||||
class DemoSimpleComponent extends StatelessWidget {
|
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.message = message,
|
||||||
this.color = color;
|
this.color = color,
|
||||||
|
this.result = result;
|
||||||
|
|
||||||
final String message;
|
final String message;
|
||||||
final Color color;
|
final Color color;
|
||||||
|
final String result;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
|
@ -44,7 +46,11 @@ class DemoSimpleComponent extends StatelessWidget {
|
||||||
padding: new EdgeInsets.only(top: 15.0),
|
padding: new EdgeInsets.only(top: 15.0),
|
||||||
child: new FlatButton(
|
child: new FlatButton(
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
|
if (result == null) {
|
||||||
Navigator.pop(context);
|
Navigator.pop(context);
|
||||||
|
} else {
|
||||||
|
Navigator.pop(context, result);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
child: new Text(
|
child: new Text(
|
||||||
"OK",
|
"OK",
|
||||||
|
|
|
@ -23,6 +23,7 @@ class HomeComponent extends StatelessWidget {
|
||||||
menuButton(context, "Preset (Fade In)", "preset-fade"),
|
menuButton(context, "Preset (Fade In)", "preset-fade"),
|
||||||
menuButton(context, "Custom Transition", "custom"),
|
menuButton(context, "Custom Transition", "custom"),
|
||||||
menuButton(context, "Function Call", "function-call"),
|
menuButton(context, "Function Call", "function-call"),
|
||||||
|
menuButton(context, "Navigator Result", "pop-result"),
|
||||||
new Padding(
|
new Padding(
|
||||||
padding: new EdgeInsets.only(top: 65.0, left: 60.0, right: 60.0),
|
padding: new EdgeInsets.only(top: 65.0, left: 60.0, right: 60.0),
|
||||||
child: new Center(
|
child: new Center(
|
||||||
|
@ -80,6 +81,7 @@ class HomeComponent extends StatelessWidget {
|
||||||
void tappedMenuButton(BuildContext context, String key) {
|
void tappedMenuButton(BuildContext context, String key) {
|
||||||
String message = "";
|
String message = "";
|
||||||
String hexCode = "#FFFFFF";
|
String hexCode = "#FFFFFF";
|
||||||
|
String result;
|
||||||
TransitionType transitionType = TransitionType.native;
|
TransitionType transitionType = TransitionType.native;
|
||||||
if (key != "custom" && key != "function-call") {
|
if (key != "custom" && key != "function-call") {
|
||||||
if (key == "native") {
|
if (key == "native") {
|
||||||
|
@ -93,13 +95,32 @@ class HomeComponent extends StatelessWidget {
|
||||||
hexCode = "#F700D2";
|
hexCode = "#F700D2";
|
||||||
message = "This screen should have appeared with a fade in transition";
|
message = "This screen should have appeared with a fade in transition";
|
||||||
transitionType = TransitionType.fadeIn;
|
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") {
|
} else if (key == "custom") {
|
||||||
hexCode = "#DFF700";
|
hexCode = "#DFF700";
|
||||||
message = "This screen should have appeared with a crazy custom transition";
|
message = "This screen should have appeared with a crazy custom transition";
|
||||||
var 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(
|
return new ScaleTransition(
|
||||||
scale: animation,
|
scale: animation,
|
||||||
child: new RotationTransition(
|
child: new RotationTransition(
|
||||||
|
|
|
@ -14,11 +14,12 @@ import 'package:flutter/material.dart';
|
||||||
var demoRouteHandler = new Handler(handlerFunc: (BuildContext context, Map<String, dynamic> params) {
|
var demoRouteHandler = new Handler(handlerFunc: (BuildContext context, Map<String, dynamic> params) {
|
||||||
String message = params["message"];
|
String message = params["message"];
|
||||||
String colorHex = params["color_hex"];
|
String colorHex = params["color_hex"];
|
||||||
|
String result = params["result"];
|
||||||
Color color = new Color(0xFFFFFFFF);
|
Color color = new Color(0xFFFFFFFF);
|
||||||
if (colorHex != null && colorHex.length > 0) {
|
if (colorHex != null && colorHex.length > 0) {
|
||||||
color = new Color(ColorHelpers.fromHexString(colorHex));
|
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,
|
var demoFunctionHandler = new Handler(type: HandlerType.function,
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
*/
|
*/
|
||||||
library fluro;
|
library fluro;
|
||||||
|
|
||||||
|
import 'dart:async';
|
||||||
import 'package:flutter/foundation.dart';
|
import 'package:flutter/foundation.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter/widgets.dart';
|
import 'package:flutter/widgets.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);
|
typedef Widget HandlerFunc(BuildContext context, Map<String, dynamic> parameters);
|
||||||
|
@ -46,7 +46,7 @@ class RouteMatch {
|
||||||
this.route = null,
|
this.route = null,
|
||||||
this.errorMessage = "Unable to match route. Please check the logs."
|
this.errorMessage = "Unable to match route. Please check the logs."
|
||||||
});
|
});
|
||||||
final Route<Null> route;
|
final Route<dynamic> route;
|
||||||
final RouteMatchType matchType;
|
final RouteMatchType matchType;
|
||||||
final String errorMessage;
|
final String errorMessage;
|
||||||
}
|
}
|
|
@ -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),
|
Duration transitionDuration = const Duration(milliseconds: 250),
|
||||||
RouteTransitionsBuilder transitionBuilder})
|
RouteTransitionsBuilder transitionBuilder})
|
||||||
{
|
{
|
||||||
RouteMatch routeMatch = matchRoute(context, path, transitionType: transition,
|
RouteMatch routeMatch = matchRoute(context, path, transitionType: transition,
|
||||||
transitionsBuilder: transitionBuilder, transitionDuration: transitionDuration);
|
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) {
|
if (routeMatch.matchType == RouteMatchType.nonVisual) {
|
||||||
return;
|
completer.complete("Non visual route type.");
|
||||||
}
|
} else {
|
||||||
if (route == null && notFoundHandler != null) {
|
if (route == null && notFoundHandler != null) {
|
||||||
route = _notFoundRoute(context, path);
|
route = _notFoundRoute(context, path);
|
||||||
}
|
}
|
||||||
if (route != null) {
|
if (route != null) {
|
||||||
replace ? Navigator.pushReplacement(context, route) : Navigator.push(context, route);
|
future = replace ? Navigator.pushReplacement(context, route) : Navigator.push(context, route);
|
||||||
|
completer.complete();
|
||||||
} else {
|
} else {
|
||||||
print("No registered route was found to handle '$path'.");
|
String error = "No registered route was found to handle '$path'.";
|
||||||
|
print(error);
|
||||||
|
completer.completeError(error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return future;
|
||||||
|
}
|
||||||
|
|
||||||
///
|
///
|
||||||
Route<Null> _notFoundRoute(BuildContext context, String path) {
|
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 new MaterialPageRoute<Null>(settings: routeSettings, builder: (BuildContext context) {
|
||||||
return notFoundHandler.handlerFunc(context, parameters);
|
return notFoundHandler.handlerFunc(context, parameters);
|
||||||
});
|
});
|
||||||
|
@ -91,7 +99,7 @@ class Router {
|
||||||
RouteCreator creator = (RouteSettings routeSettings, Map<String, dynamic> parameters) {
|
RouteCreator creator = (RouteSettings routeSettings, Map<String, dynamic> parameters) {
|
||||||
bool isNativeTransition = (transitionType == TransitionType.native || transitionType == TransitionType.nativeModal);
|
bool isNativeTransition = (transitionType == TransitionType.native || transitionType == TransitionType.nativeModal);
|
||||||
if (isNativeTransition) {
|
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) {
|
builder: (BuildContext context) {
|
||||||
return handler.handlerFunc(context, parameters);
|
return handler.handlerFunc(context, parameters);
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue