Skip to main content
Mixins that allow classes to listen to navigation changes and receive callbacks when routes are paused or resumed. A mixin that provides navigation state listening capabilities to any class.

Properties

mounted
bool
Whether the listener is currently mounted.
paused
bool
Whether the route is currently paused.
navigationListener
StreamSubscription
The stream subscription for navigation events.

Methods

initNavigationListener

void initNavigationListener(BuildContext context, {String? routeName})
Initializes the navigation listener.
context
BuildContext
required
The build context to use for route detection.
routeName
String?
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.
oldRouteName
String
required
The paused route name.
newRouteName
String
required
The resumed route name.
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

void 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. 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

paused
bool
Whether the route is currently paused.
navigationListener
StreamSubscription
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.
routeName
String
required
The route name to track.

onRoutePause

void onRoutePause({
  required String oldRouteName,
  required String newRouteName
})
Route paused callback. Override this method to handle route pause events.

onRouteResume

void onRouteResume()
Called when the route becomes active (resumed). Override this method to handle route resume events. A mixin that provides navigation state listening capabilities to ChangeNotifier classes. The navigation listener is automatically disposed when the ChangeNotifier is disposed.

Properties

mounted
bool
Whether the listener is currently mounted.
paused
bool
Whether the route is currently paused.
navigationListener
StreamSubscription
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

void 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

Build docs developers (and LLMs) love