close<T extends Object> method

void close<T extends Object>({
  1. bool closeAll = true,
  2. bool closeSnackbar = true,
  3. bool closeDialog = true,
  4. bool closeBottomSheet = true,
  5. String? id,
  6. T? result,
})

Closes the currently open snackbars, dialogs and bottom sheets.

When closeAll is true (the default), every open overlay of the selected kinds is closed; otherwise only the topmost one is. Dialogs and bottom sheets are closed returning result to the Future returned when they were opened.

If the topmost route handles the pop internally — for example a Scaffold with an open drawer, which registers a LocalHistoryEntry on the enclosing route — that entry is consumed first (e.g. the drawer closes) while the page itself stays.

id is for when you are using nested navigation, as explained in documentation

Implementation

void close<T extends Object>({
  bool closeAll = true,
  bool closeSnackbar = true,
  bool closeDialog = true,
  bool closeBottomSheet = true,
  String? id,
  T? result,
}) {
  if (_topRoute(id: id)?.willHandlePopInternally == true) {
    searchDelegate(id).navigatorKey.currentState?.pop();
  }

  void handleClose(
    bool closeCondition,
    void Function() closeAllFunction,
    void Function() closeSingleFunction, [
    bool Function()? isOpenCondition,
  ]) {
    if (closeCondition) {
      if (closeAll) {
        closeAllFunction();
      } else if (isOpenCondition?.call() == true) {
        closeSingleFunction();
      }
    }
  }

  handleClose(closeSnackbar, closeAllSnackbars, closeCurrentSnackbar);
  handleClose(
    closeDialog,
    () => closeAllDialogs(id: id, result: result),
    () => closeOverlay(id: id, result: result),
    () => _isDialogRoute(_topRoute(id: id)),
  );
  handleClose(
    closeBottomSheet,
    () => closeAllBottomSheets(id: id, result: result),
    () => closeOverlay(id: id, result: result),
    () => _isBottomSheetRoute(_topRoute(id: id)),
  );
}