| name | asyncredux-navigation |
| description | Handle navigation through actions using NavigateAction. Covers setting up the navigator key, dispatching NavigateAction for push/pop/replace, and testing navigation in isolation. |
Navigation with NavigateAction
AsyncRedux enables app navigation through action dispatching, making it easier to unit test navigation logic. This approach is optional and currently supports Navigator 1 only.
Setup
1. Create and Register the Navigator Key
Create a global navigator key and register it with NavigateAction during app initialization:
import 'package:async_redux/async_redux.dart';
import 'package:flutter/material.dart';
final navigatorKey = GlobalKey<NavigatorState>();
void main() async {
NavigateAction.setNavigatorKey(navigatorKey);
// ... rest of initialization
runApp(MyApp());
}
2. Configure MaterialApp
Pass the same navigator key to your MaterialApp:
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return StoreProvider<AppState>(
store: store,
child: MaterialApp(
routes: {
'/': (context) => HomePage(),
'/details': (context) => DetailsPage(),
'/settings': (context) => SettingsPage(),
},
navigatorKey: navigatorKey,
),
);
}
}
Dispatching Navigation Actions
Push Operations
// Push a named route
dispatch(NavigateAction.pushNamed('/details'));
// Push a route with a Route object
dispatch(NavigateAction.push(
MaterialPageRoute(builder: (context) => DetailsPage()),
));
// Push and replace current route (named)
dispatch(NavigateAction.pushReplacementNamed('/newRoute'));
// Push and replace current route (with Route object)
dispatch(NavigateAction.pushReplacement(
MaterialPageRoute(builder: (context) => NewPage()),
));
// Pop current route and push a new named route
dispatch(NavigateAction.popAndPushNamed('/otherRoute'));
// Push named route and remove all routes until predicate is true
dispatch(NavigateAction.pushNamedAndRemoveUntil(
'/home',
(route) => false, // Removes all routes
));
// Push named route and remove all routes (convenience method)
dispatch(NavigateAction.pushNamedAndRemoveAll('/home'));
// Push route and remove until predicate
dispatch(NavigateAction.pushAndRemoveUntil(
MaterialPageRoute(builder: (context) => HomePage()),
(route) => false,
));