Let screen state show or close each overlay

Keep the current dialog or sheet in an OverlayRequest? value and display it with ScreenOverlayHost or AnimatedScreenOverlayHost. Clear the request to close it or replace it to show another overlay.

Use one value for the active overlay

Keep one OverlayRequest? value for the overlay. Set it to null to close it. With a null value, the screen has no active overlay.

Send data to a dialog or sheet

Pass the information a dialog or sheet needs as the payload in a DialogRequest or BottomSheetRequest.

Close the overlay first when going back

Close the active overlay before removing a page. Use the back scope widget to handle the page removal so each step happens in order.

Connect state to the overlay host

Keep the overlay request in state and use overlayBuilder to create the dialog or sheet.

Animated Overlay Host

return AnimatedScreenOverlayHost(
  overlay: _overlay,
  onDismiss: _dismissOverlay,
  overlayBuilder: (context, req, dismiss) => switch (req) {
    DialogRequest(key: 'hello') => AlertDialog(
      title: const Text('Hello'),
      actions: [TextButton(onPressed: dismiss, child: const Text('Close'))],
    ),
    _ => null,
  },
  child: DeclarativePagesNavigator(
    pages: _pages,
    buildPage: _buildPage,
    onPopTop: _popTop,
    canPopTop: () => _overlay == null,
  ),
);
Handle the iOS back swipe

While an overlay is visible, use canPopTop to keep the iOS back swipe from competing with overlay dismissal.