Mixins that allow classes to listen to navigation changes and receive callbacks when routes are paused or resumed.
NavigationListenerMixin
A mixin that provides navigation state listening capabilities to any class.
Properties
Whether the listener is currently mounted.
Whether the route is currently paused.
The stream subscription for navigation events.
Methods
initNavigationListener
void initNavigationListener(BuildContext context, {String? routeName})
Initializes the navigation listener.
The build context to use for route detection.
Optional route name to track. If not provided, the route name is extracted from the context.
onRoutePause
void onRoutePause({
required String oldRouteName,
required String newRouteName
})
Route paused callback. Override this method to handle route pause events.
The onRoutePause callback is useful for pausing a page’s updates and background operations that are no longer needed when in the background. Because Flutter keeps Page widgets mounted and preserves their state, “heavy” page processes should be paused in the background.
onRouteResume
Called when the route becomes active (resumed). Override this method to handle route resume events.
disposeNavigationListener
void disposeNavigationListener()
Disposes the navigation listener and cancels the subscription. This method should be called when navigation listening is no longer needed.
NavigationListenerStateMixin
A mixin that provides navigation state listening capabilities to State classes. This mixin automatically initializes navigation listening in initState and disposes it in dispose, making it convenient for use in StatefulWidgets.
Properties
Whether the route is currently paused.
The stream subscription for navigation events.
Methods
setRouteName
void setRouteName(String routeName)
Sets the route name to track. If not set, the route name is extracted from the context.
onRoutePause
void onRoutePause({
required String oldRouteName,
required String newRouteName
})
Route paused callback. Override this method to handle route pause events.
onRouteResume
Called when the route becomes active (resumed). Override this method to handle route resume events.
NavigationListenerChangeNotifierMixin
A mixin that provides navigation state listening capabilities to ChangeNotifier classes. The navigation listener is automatically disposed when the ChangeNotifier is disposed.
Properties
Whether the listener is currently mounted.
Whether the route is currently paused.
The stream subscription for navigation events.
Methods
initNavigationListener
void initNavigationListener(BuildContext context, {String? routeName})
Initializes the navigation listener.
onRoutePause
void onRoutePause({
required String oldRouteName,
required String newRouteName
})
Route paused callback. Override this method to handle route pause events.
onRouteResume
Called when the route becomes active (resumed). Override this method to handle route resume events.
Examples
Using NavigationListenerMixin
class MyPageController with NavigationListenerMixin {
Timer? _timer;
void init(BuildContext context) {
initNavigationListener(context);
_startTimer();
}
void _startTimer() {
_timer = Timer.periodic(Duration(seconds: 1), (_) {
print('Timer tick');
});
}
@override
void onRoutePause({required String oldRouteName, required String newRouteName}) {
print('Route paused: $oldRouteName -> $newRouteName');
_timer?.cancel();
}
@override
void onRouteResume() {
print('Route resumed');
_startTimer();
}
void dispose() {
_timer?.cancel();
disposeNavigationListener();
}
}
Using NavigationListenerStateMixin
class MyPage extends StatefulWidget {
@override
State<MyPage> createState() => _MyPageState();
}
class _MyPageState extends State<MyPage> with NavigationListenerStateMixin {
Timer? _timer;
@override
void initState() {
super.initState();
_startTimer();
}
void _startTimer() {
_timer = Timer.periodic(Duration(seconds: 1), (_) {
print('Timer tick');
});
}
@override
void onRoutePause({required String oldRouteName, required String newRouteName}) {
print('Route paused: $oldRouteName -> $newRouteName');
_timer?.cancel();
}
@override
void onRouteResume() {
print('Route resumed');
_startTimer();
}
@override
void dispose() {
_timer?.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('My Page')),
body: Center(child: Text('Content')),
);
}
}
Using NavigationListenerChangeNotifierMixin
class MyViewModel extends ChangeNotifier with NavigationListenerChangeNotifierMixin {
Timer? _timer;
void init(BuildContext context) {
initNavigationListener(context);
_startTimer();
}
void _startTimer() {
_timer = Timer.periodic(Duration(seconds: 1), (_) {
print('Timer tick');
notifyListeners();
});
}
@override
void onRoutePause({required String oldRouteName, required String newRouteName}) {
print('Route paused: $oldRouteName -> $newRouteName');
_timer?.cancel();
}
@override
void onRouteResume() {
print('Route resumed');
_startTimer();
}
@override
void dispose() {
_timer?.cancel();
super.dispose();
}
}
Notes
onRoutePause is always called before a page is disposed
- To differentiate between pause and close events, check if the route still exists in the navigation stack
- The mixin automatically handles subscription cleanup
- State mixin variant integrates with widget lifecycle automatically