Take GlobalKey<NavigatorState> instead of BuildContext
This commit is contained in:
parent
45949890cc
commit
9956b24eb2
|
@ -35,6 +35,7 @@ class AppComponentState extends State<AppComponent> {
|
||||||
primarySwatch: Colors.blue,
|
primarySwatch: Colors.blue,
|
||||||
),
|
),
|
||||||
onGenerateRoute: Application.router.generator,
|
onGenerateRoute: Application.router.generator,
|
||||||
|
navigatorKey: Application.routerKey,
|
||||||
);
|
);
|
||||||
// print("initial route = ${app.initialRoute}");
|
// print("initial route = ${app.initialRoute}");
|
||||||
return app;
|
return app;
|
||||||
|
|
|
@ -232,10 +232,10 @@ class HomeComponentState extends State<HomeComponent> {
|
||||||
}
|
}
|
||||||
|
|
||||||
Application.router
|
Application.router
|
||||||
.navigateTo(context, route, transition: transitionType)
|
.navigateTo(Application.routerKey, route, transition: transitionType)
|
||||||
.then((result) {
|
.then((result) {
|
||||||
if (key == "pop-result") {
|
if (key == "pop-result") {
|
||||||
Application.router.navigateTo(context, "/demo/func?message=$result");
|
Application.router.navigateTo(Application.routerKey, "/demo/func?message=$result");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else if (key == "custom") {
|
} else if (key == "custom") {
|
||||||
|
@ -253,7 +253,7 @@ class HomeComponentState extends State<HomeComponent> {
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
Application.router.navigateTo(
|
Application.router.navigateTo(
|
||||||
context,
|
Application.routerKey,
|
||||||
"/demo?message=$message&color_hex=$hexCode",
|
"/demo?message=$message&color_hex=$hexCode",
|
||||||
transition: TransitionType.custom,
|
transition: TransitionType.custom,
|
||||||
transitionBuilder: transition,
|
transitionBuilder: transition,
|
||||||
|
@ -261,10 +261,10 @@ class HomeComponentState extends State<HomeComponent> {
|
||||||
);
|
);
|
||||||
} else if (key == "fixed-trans") {
|
} else if (key == "fixed-trans") {
|
||||||
Application.router.navigateTo(
|
Application.router.navigateTo(
|
||||||
context, "/demo/fixedtrans?message=Hello!&color_hex=#f4424b");
|
Application.routerKey, "/demo/fixedtrans?message=Hello!&color_hex=#f4424b");
|
||||||
} else {
|
} else {
|
||||||
message = "You tapped the function button!";
|
message = "You tapped the function button!";
|
||||||
Application.router.navigateTo(context, "/demo/func?message=$message");
|
Application.router.navigateTo(Application.routerKey, "/demo/func?message=$message");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,10 @@
|
||||||
* See LICENSE for distribution and usage details.
|
* See LICENSE for distribution and usage details.
|
||||||
*/
|
*/
|
||||||
import 'package:fluro/fluro.dart';
|
import 'package:fluro/fluro.dart';
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
|
||||||
class Application {
|
class Application {
|
||||||
static Router router;
|
static Router router;
|
||||||
|
|
||||||
|
static GlobalKey<NavigatorState> routerKey = GlobalKey<NavigatorState>();
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,16 +38,16 @@ class Router {
|
||||||
return _routeTree.matchRoute(path);
|
return _routeTree.matchRoute(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
void pop(BuildContext context) => Navigator.pop(context);
|
void pop(GlobalKey<NavigatorState> key) => key.currentState.pop();
|
||||||
|
|
||||||
///
|
///
|
||||||
Future navigateTo(BuildContext context, String path,
|
Future navigateTo(GlobalKey<NavigatorState> key, String path,
|
||||||
{bool replace = false,
|
{bool replace = false,
|
||||||
bool clearStack = false,
|
bool clearStack = false,
|
||||||
TransitionType transition,
|
TransitionType transition,
|
||||||
Duration transitionDuration = const Duration(milliseconds: 250),
|
Duration transitionDuration = const Duration(milliseconds: 250),
|
||||||
RouteTransitionsBuilder transitionBuilder}) {
|
RouteTransitionsBuilder transitionBuilder}) {
|
||||||
RouteMatch routeMatch = matchRoute(context, path,
|
RouteMatch routeMatch = matchRoute(key.currentContext, path,
|
||||||
transitionType: transition,
|
transitionType: transition,
|
||||||
transitionsBuilder: transitionBuilder,
|
transitionsBuilder: transitionBuilder,
|
||||||
transitionDuration: transitionDuration);
|
transitionDuration: transitionDuration);
|
||||||
|
@ -58,16 +58,16 @@ class Router {
|
||||||
completer.complete("Non visual route type.");
|
completer.complete("Non visual route type.");
|
||||||
} else {
|
} else {
|
||||||
if (route == null && notFoundHandler != null) {
|
if (route == null && notFoundHandler != null) {
|
||||||
route = _notFoundRoute(context, path);
|
route = _notFoundRoute(path);
|
||||||
}
|
}
|
||||||
if (route != null) {
|
if (route != null) {
|
||||||
if (clearStack) {
|
if (clearStack) {
|
||||||
future =
|
future =
|
||||||
Navigator.pushAndRemoveUntil(context, route, (check) => false);
|
key.currentState.pushAndRemoveUntil(route, (check) => false);
|
||||||
} else {
|
} else {
|
||||||
future = replace
|
future = replace
|
||||||
? Navigator.pushReplacement(context, route)
|
? key.currentState.pushReplacement(route)
|
||||||
: Navigator.push(context, route);
|
: key.currentState.push(route);
|
||||||
}
|
}
|
||||||
completer.complete();
|
completer.complete();
|
||||||
} else {
|
} else {
|
||||||
|
@ -81,7 +81,7 @@ class Router {
|
||||||
}
|
}
|
||||||
|
|
||||||
///
|
///
|
||||||
Route<Null> _notFoundRoute(BuildContext context, String path) {
|
Route<Null> _notFoundRoute(String path) {
|
||||||
RouteCreator<Null> creator =
|
RouteCreator<Null> creator =
|
||||||
(RouteSettings routeSettings, Map<String, List<String>> parameters) {
|
(RouteSettings routeSettings, Map<String, List<String>> parameters) {
|
||||||
return MaterialPageRoute<Null>(
|
return MaterialPageRoute<Null>(
|
||||||
|
|
Loading…
Reference in New Issue