FlutterNavigator is a library for dealing with the Navigator API without passing the build context. This package wraps the NavigatorKey and provides a cleaner service for navigating without context in your flutter application.
Use the pubspec.yaml file to install the dependency.
flutter_navigator: ^0.1.3
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Navigator Key',
navigatorKey: FlutterNavigator().navigatorKey,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const PageOne(),
);
}
}
You need to provide the navigator key to your MaterialApp as that is how the service will communicate with the navigator state without the build context.
final FlutterNavigator _flutterNavigator = FlutterNavigator();
_flutterNavigator.push(PageTwo.route());
The FlutterNavigator is a singleton service so it will always keep the same instance throughout the lifetime of the application. You can call the Navigator API methods through the service (like 'push', 'pop', 'pushAndRemoveUntil', etc)... It's that simple!
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Observers',
navigatorKey: FlutterNavigator().navigatorKey,
theme: ThemeData(
primarySwatch: Colors.blue,
),
navigatorObservers: [
FlutterNavigator().routeObserver,
],
home: const PageOne(),
);
}
}
As with the navigator key, you can use the route observer provided by the service and this can be accessed throughout the application.
class CustomFlutterNavigator implements IFlutterNavigator
To create your own implementation, all you need to do is implement the interface IFlutterNavigator and provide all the needed methods and properties.
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.