Let your Flutter app state drive navigation

Use declarative_nav to describe pages, dialogs, and sheets in state. Navigator 2.0 follows that state as you move through the app.

Keep page state as data

Keep page data in PageEntry and create the widgets in a resolver callback when it's time to build the screen.

Tie each overlay to its screen

Track a dialog or sheet with the screen it belongs to. With tabs, you can keep that state inside the tab too.

Use the state tools you already know

Use whichever state management approach fits your app, such as setState, Riverpod, Bloc, or Provider.

Install and set up navigation

Add the package, then track the page list and current overlay in your app state.

Install the package

flutter pub add declarative_nav

A first navigator setup

class AppRootState extends State<AppRoot> {
  OverlayRequest? _overlay;
  late List<PageEntry> _pages;

  @override
  void initState() {
    super.initState();
    _pages = const [PageEntry(key: 'home', name: '/home')];
  }

  Widget build(BuildContext context) {
    return DeclarativePagesNavigator(
      pages: _pages,
      buildPage: _buildPage,
      onPopTop: _popTop,
      canPopTop: () => _overlay == null,
    );
  }
}
Keep stack changes in one place

Keep List<PageEntry> as the only source of truth for a stack. Update the list to navigate and remove push and pop calls that would change the same stack.