fluro/lib/src/routable.dart

85 lines
2.7 KiB
Dart
Raw Normal View History

2018-04-05 17:07:43 -04:00
/*
* fluro
* A Posse Production
* http://goposse.com
* Copyright (c) 2018 Posse Productions LLC. All rights reserved.
* See LICENSE for distribution and usage details.
*/
2018-03-28 15:52:50 -04:00
part of fluro;
abstract class Routable {
void didPush(Route<dynamic> route, Route<dynamic> previousRoute) {}
2018-04-05 17:32:06 -04:00
void didAppear(
bool wasPushed,
Route<dynamic> route,
Route<dynamic> previousRoute,
) {}
2018-03-28 15:52:50 -04:00
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) {
2018-04-05 17:32:06 -04:00
final routeWidget = route.buildPage(
route.navigator.context, route.animation, route.secondaryAnimation);
2018-03-28 15:52:50 -04:00
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) {
2018-04-05 17:32:06 -04:00
final leavingWidget = route.buildPage(
route.navigator.context, route.animation, route.secondaryAnimation);
2018-03-28 15:52:50 -04:00
if (leavingWidget is Routable) {
Routable w = (leavingWidget as Routable);
w.didPop(route, previousRoute);
}
}
if (previousRoute is PageRoute) {
final returningWidget = previousRoute.buildPage(
2018-04-05 17:32:06 -04:00
previousRoute.navigator.context,
previousRoute.animation,
previousRoute.secondaryAnimation);
2018-03-28 15:52:50 -04:00
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) {
2018-04-05 17:32:06 -04:00
final leavingWidget = oldRoute.buildPage(oldRoute.navigator.context,
oldRoute.animation, oldRoute.secondaryAnimation);
2018-03-28 15:52:50 -04:00
if (leavingWidget is Routable) {
Routable w = (leavingWidget as Routable);
w.didPop(oldRoute, newRoute);
}
}
if (newRoute is PageRoute) {
2018-04-05 17:32:06 -04:00
final returningWidget = newRoute.buildPage(newRoute.navigator.context,
newRoute.animation, newRoute.secondaryAnimation);
2018-03-28 15:52:50 -04:00
if (returningWidget is Routable) {
Routable w = (returningWidget as Routable);
w.didAppear(false, newRoute, oldRoute);
}
}
}
}