From 9956b24eb248a518feb0249128ba2b9d5760d7ba Mon Sep 17 00:00:00 2001 From: Michael Pfaff Date: Fri, 24 Apr 2020 14:16:22 -0400 Subject: [PATCH] Take GlobalKey instead of BuildContext --- example/lib/components/app/app_component.dart | 1 + example/lib/components/home/home_component.dart | 10 +++++----- example/lib/config/application.dart | 3 +++ lib/src/router.dart | 16 ++++++++-------- 4 files changed, 17 insertions(+), 13 deletions(-) diff --git a/example/lib/components/app/app_component.dart b/example/lib/components/app/app_component.dart index f2247eb..d223998 100644 --- a/example/lib/components/app/app_component.dart +++ b/example/lib/components/app/app_component.dart @@ -35,6 +35,7 @@ class AppComponentState extends State { primarySwatch: Colors.blue, ), onGenerateRoute: Application.router.generator, + navigatorKey: Application.routerKey, ); // print("initial route = ${app.initialRoute}"); return app; diff --git a/example/lib/components/home/home_component.dart b/example/lib/components/home/home_component.dart index 0730d24..28e8410 100644 --- a/example/lib/components/home/home_component.dart +++ b/example/lib/components/home/home_component.dart @@ -232,10 +232,10 @@ class HomeComponentState extends State { } Application.router - .navigateTo(context, route, transition: transitionType) + .navigateTo(Application.routerKey, route, transition: transitionType) .then((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") { @@ -253,7 +253,7 @@ class HomeComponentState extends State { ); }; Application.router.navigateTo( - context, + Application.routerKey, "/demo?message=$message&color_hex=$hexCode", transition: TransitionType.custom, transitionBuilder: transition, @@ -261,10 +261,10 @@ class HomeComponentState extends State { ); } else if (key == "fixed-trans") { Application.router.navigateTo( - context, "/demo/fixedtrans?message=Hello!&color_hex=#f4424b"); + Application.routerKey, "/demo/fixedtrans?message=Hello!&color_hex=#f4424b"); } else { message = "You tapped the function button!"; - Application.router.navigateTo(context, "/demo/func?message=$message"); + Application.router.navigateTo(Application.routerKey, "/demo/func?message=$message"); } } } diff --git a/example/lib/config/application.dart b/example/lib/config/application.dart index 10dd398..51837ae 100644 --- a/example/lib/config/application.dart +++ b/example/lib/config/application.dart @@ -7,7 +7,10 @@ * See LICENSE for distribution and usage details. */ import 'package:fluro/fluro.dart'; +import 'package:flutter/material.dart'; class Application { static Router router; + + static GlobalKey routerKey = GlobalKey(); } diff --git a/lib/src/router.dart b/lib/src/router.dart index 0620079..889e5aa 100644 --- a/lib/src/router.dart +++ b/lib/src/router.dart @@ -38,16 +38,16 @@ class Router { return _routeTree.matchRoute(path); } - void pop(BuildContext context) => Navigator.pop(context); + void pop(GlobalKey key) => key.currentState.pop(); /// - Future navigateTo(BuildContext context, String path, + Future navigateTo(GlobalKey key, String path, {bool replace = false, bool clearStack = false, TransitionType transition, Duration transitionDuration = const Duration(milliseconds: 250), RouteTransitionsBuilder transitionBuilder}) { - RouteMatch routeMatch = matchRoute(context, path, + RouteMatch routeMatch = matchRoute(key.currentContext, path, transitionType: transition, transitionsBuilder: transitionBuilder, transitionDuration: transitionDuration); @@ -58,16 +58,16 @@ class Router { completer.complete("Non visual route type."); } else { if (route == null && notFoundHandler != null) { - route = _notFoundRoute(context, path); + route = _notFoundRoute(path); } if (route != null) { if (clearStack) { future = - Navigator.pushAndRemoveUntil(context, route, (check) => false); + key.currentState.pushAndRemoveUntil(route, (check) => false); } else { future = replace - ? Navigator.pushReplacement(context, route) - : Navigator.push(context, route); + ? key.currentState.pushReplacement(route) + : key.currentState.push(route); } completer.complete(); } else { @@ -81,7 +81,7 @@ class Router { } /// - Route _notFoundRoute(BuildContext context, String path) { + Route _notFoundRoute(String path) { RouteCreator creator = (RouteSettings routeSettings, Map> parameters) { return MaterialPageRoute(