wip - routable lifecycle

This commit is contained in:
Luke 2018-03-28 12:52:50 -07:00
parent d9ba273c88
commit ab8ef67243
3 changed files with 91 additions and 0 deletions

View File

@ -13,5 +13,6 @@ import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
part 'src/common.dart';
part 'src/routable.dart';
part 'src/router.dart';
part 'src/tree.dart';

69
lib/src/routable.dart Normal file
View File

@ -0,0 +1,69 @@
part of fluro;
abstract class Routable {
void didPush(Route<dynamic> route, Route<dynamic> previousRoute) {}
void didAppear(bool wasPushed, Route<dynamic> route, Route<dynamic> previousRoute) {}
void didPop(Route<dynamic> route, Route<dynamic> previousRoute) {}
}
typedef String ScreenNameExtractor(RouteSettings settings);
String defaultNameExtractor(RouteSettings settings) => settings.name;
class RoutableObserver extends RouteObserver<PageRoute<dynamic>> {
final ScreenNameExtractor nameExtractor = defaultNameExtractor;
@override
void didPush(Route<dynamic> route, Route<dynamic> previousRoute) {
super.didPush(route, previousRoute);
if (route is PageRoute) {
final routeWidget = route.buildPage(route.navigator.context, route.animation, route.secondaryAnimation);
if (routeWidget is Routable) {
Routable w = (routeWidget as Routable);
w.didPush(route, previousRoute);
w.didAppear(true, route, previousRoute);
}
}
}
@override
void didPop(Route<dynamic> route, Route<dynamic> previousRoute) {
super.didPop(route, previousRoute);
if (route is PageRoute) {
final leavingWidget = route.buildPage(route.navigator.context, route.animation, route.secondaryAnimation);
if (leavingWidget is Routable) {
Routable w = (leavingWidget as Routable);
w.didPop(route, previousRoute);
}
}
if (previousRoute is PageRoute) {
final returningWidget = previousRoute.buildPage(
previousRoute.navigator.context, previousRoute.animation, previousRoute.secondaryAnimation);
if (returningWidget is Routable) {
Routable w = (returningWidget as Routable);
w.didAppear(false, route, previousRoute);
}
}
}
@override
void didReplace({Route newRoute, Route oldRoute}) {
super.didReplace(newRoute: newRoute, oldRoute: oldRoute);
if (oldRoute is PageRoute) {
final leavingWidget = oldRoute.buildPage(oldRoute.navigator.context, oldRoute.animation, oldRoute.secondaryAnimation);
if (leavingWidget is Routable) {
Routable w = (leavingWidget as Routable);
w.didPop(oldRoute, newRoute);
}
}
if (newRoute is PageRoute) {
final returningWidget = newRoute.buildPage(
newRoute.navigator.context, newRoute.animation, newRoute.secondaryAnimation);
if (returningWidget is Routable) {
Routable w = (returningWidget as Routable);
w.didAppear(false, newRoute, oldRoute);
}
}
}
}

View File

@ -27,6 +27,19 @@ class Router {
/// Generic handler for when a route has not been defined
Handler notFoundHandler;
// /// The root navigator we are going to use for all navigation
// Navigator _navigator;
// Router() {
// _navigator = new Navigator(
// onGenerateRoute: _onGenerateRoute,
// onUnknownRoute: _onUnknownRoute,
// observers: <NavigatorObserver>[
// ],
// );
// }
/// Creates a [PageRoute] definition for the passed [RouteHandler]. You can optionally provide a custom
/// transition builder for the route.
void define(String routePath, {@required Handler handler}) {
@ -68,6 +81,14 @@ class Router {
return future;
}
bool pop(BuildContext context) => Navigator.of(context).pop();
List<NavigatorObserver> get routerObservers {
return [
new RoutableObserver(),
];
}
///
Route<Null> _notFoundRoute(BuildContext context, String path) {
RouteCreator<Null> creator = (RouteSettings routeSettings, Map<String, List<String>> parameters) {