route_observer_mixin

Creator: coderz1093

Last updated:

Add to Cart

Description:

route observer mixin

route_observer_mixin #

RouteObserveMixin provides easy access to didPush/didPop/didPushNext/didPopNext.
Usage #
1. Wrap MaterialApp with RouteObserverProvider. #
void main() {
runApp(
MultiProvider(
providers: [
// 1. Wrap MaterialApp with RouteObserverProvider.
RouteObserverProvider(),
ChangeNotifierProvider(create: (context) => Logger())
],
child: const App(),
),
);

// Of course, this is also ok.
// I demonstrated that [MultiProvider] can be used for RouteObserverProvider👍

/*
runApp(
RouteObserverProvider(
child: App(),
),
);
*/
}
copied to clipboard
2. Pass RouteObserverProvider.of(context) to navigatorObservers. #
class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
// 2. Pass RouteObserverProvider.of(context) to navigatorObservers.
navigatorObservers: [RouteObserverProvider.of(context)],
home: const APage(),
);
}
}
copied to clipboard
3. Add with RouteAware, RouteObserverMixin to State and override RouteAware methods. #
class APage extends StatefulWidget {
const APage({Key key}) : super(key: key);

@override
_APageState createState() => _APageState();
}

// 3. Add `with RouteAware, RouteObserverMixin` to State and override RouteAware methods.
class _APageState extends State<APage> with RouteAware, RouteObserverMixin {

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('A Page'),
),
body: Center(
child: RaisedButton(
onPressed: () {
Navigator.of(context).push<void>(
MaterialPageRoute(
builder: (context) => const BPage(),
),
);
},
child: const Text('B Page'),
),
),
);
}

/// Called when the top route has been popped off, and the current route
/// shows up.
@override
void didPopNext() { }

/// Called when the current route has been pushed.
@override
void didPush() { }

/// Called when the current route has been popped off.
@override
void didPop() { }

/// Called when a new route has been pushed, and the current route is no
/// longer visible.
@override
void didPushNext() { }
}
copied to clipboard
Example #

https://github.com/mono0926/route_observer_mixin/tree/master/example

License

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Customer Reviews

There are no reviews.