removePop static method

void removePop(
  1. String id
)

Dismisses a specific pop overlay by ID

This method handles the complete dismissal process including:

  • Triggering exit animations
  • Removing the overlay from the controller
  • Running any registered dismissal callbacks
  • Re-sorting the remaining overlays

The dismissal is animated, with a short delay to allow animations to complete before removing the overlay from the list.

Parameters:

  • id: The unique identifier of the overlay to dismiss

Example:

// Dismiss the notification overlay
PopOverlay.removePop("notification_1");

Implementation

static void removePop(String id) {
  // Early return if no overlay with this ID exists
  if (PopOverlay.isActiveById(id) == false) {
    return;
  }

  // Find the popup content by ID
  final popupIndex =
      _controller.state.indexWhere((element) => element.id == id);
  if (popupIndex != -1) {
    final popContent = _controller.state[popupIndex];

    // Trigger the exit animation by setting the animation controller state
    popContent.animationController.state = true;

    // Wait for the animation to finish before removing from the list
    // This creates a smooth dismissal experience
    Future.delayed(const Duration(milliseconds: 450), () {
      // Double-check that the overlay still exists (could have been removed by another process)
      if (PopOverlay.isActiveById(id)) {
        _controller.update<List<PopOverlayContent>>((state) {
          // Remove the overlay and call its dismissal callback
          state.removeWhere((element) {
            if (element.id == id) {
              // Execute the onDismissed callback if provided
              element.onDismissed?.call();
              // Clean up resources
              element.dispose();
            }
            return element.id == id;
          });

          // Remove the overlay from the invisible list
          //if it's being dismissed
          _invisibleController.update<List<String>>((invisibleState) {
            invisibleState.remove(id);
            popContent.onMadeInvisible?.call();

            return invisibleState;
          });

          // Re-sort the remaining overlays
          PopOverlay._sortPopList(state);
          return state;
        });
      }
    });
  }
}