Search packages, repositories and app categories

Package detailsv1.8.2

route_observer_mixin

RouteObserveMixin provides easy access to didPush/didPop/didPushNext/didPopNext.

AndroidiOSWebmacOSWindowsLinux
30-day downloads
5.7K
Likes
20
Pub points
160

Install package

flutter pub add route_observer_mixin

Package Overview

Package selection guidance compiled from public package information, community data, and project usage.

RouteObserveMixin is a Flutter utility mixin that simplifies handling route lifecycle events such as didPush, didPop, didPushNext, and didPopNext. It enables widgets to react to navigation changes without manually managing routes or using global observers. Ideal for state management and UI updates tied to navigation flow, it streamlines interactions in complex routing scenarios across multiple platforms.

Use cases

  • Handling navigation state changes
  • Updating UI on route transitions
  • Managing state during push/pop events
  • Synchronizing data with route changes
  • Implementing custom navigation behaviors

Key features

  • Easy integration with existing widgets
  • Automatic lifecycle event callbacks
  • Supports all major Flutter platforms
  • Minimal boilerplate code
  • Clear separation of navigation logic

Suitable for

  • Developers using complex navigation flows
  • State management with route awareness
  • Cross-platform Flutter apps
  • Widgets needing navigation context

Considerations

  • Requires mixin usage in widget classes
  • Limited to route lifecycle events only
  • Does not replace full navigator management
  • Best used with stateful widgets
  • Not suitable for simple navigation scenarios

Original README

Original README snapshot from the linked source repository. Visit the repository for the latest content.

route_observer_mixin

Gif

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(),
    ),
  );
  */
}
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(),
    );
  }
}
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() { }
}

Example

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

Categories

Dependencies

Dependency list 4 items

Source repository

Projects using this package

No data available yet.