updateParams static method

void updateParams({
  1. required String id,
  2. ModalWidgetBuilder? builder,
  3. String? builderId,
  4. double? blurAmount,
  5. bool? shouldBlurBackground,
  6. bool? isExpandable,
  7. double? size,
  8. bool resetSize = false,
  9. double? expandedPercentageSize,
  10. double? contentPaddingByDragHandle,
  11. SheetPosition? sheetPosition,
  12. bool? isDismissable,
  13. bool? blockBackgroundInteraction,
  14. bool? isDraggable,
  15. Function? onDismissed,
  16. Function? onExpanded,
  17. ModalType? modalType,
  18. Alignment? modalPosition,
  19. ModalAnimationType? modalAnimationType,
  20. bool? isSwipeable,
  21. Duration? autoDismissDuration,
  22. SnackbarDisplayMode? snackbarDisplayMode,
  23. int? maxStackedSnackbars,
  24. Color? backgroundColor,
  25. bool resetBackgroundColor = false,
  26. double? snackbarWidth,
  27. Color? barrierColor,
  28. Offset? offset = _noOffsetChange,
})

Updates specific parameters of an active modal without recreating the entire ModalContent

This method allows you to update individual modal properties while maintaining the existing widget and other settings. All parameters are optional - only provide the ones you want to change.

Parameters:

  • blurAmount: Update the background blur intensity (0.0 to 20.0)
  • shouldBlurBackground: Enable/disable background blur effect
  • height: Update the modal height
  • widget: Replace the modal's widget content
  • expandedHeightPercent: Update the expanded height percentage
  • isDismissable: Change whether the modal can be dismissed
  • onDismissed: Update the dismissal callback
  • onExpanded: Update the expansion callback

Example:

// Update blur amount only (for live slider updates)
Modal.updateParams(id: 'my_modal_id', blurAmount: 7.5);

// Toggle blur on/off
Modal.updateParams(id: 'my_modal_id', shouldBlurBackground: true);

// Update height
Modal.updateParams(id: 'my_modal_id', height: 500);

// Update multiple parameters at once
Modal.updateParams(
  id: 'my_modal_id',
  blurAmount: 5.0,
  height: 300,
  isDismissable: false,
);

// Update snackbar-specific parameters
Modal.updateParams(
  id: 'my_modal_id',
  isSwipeable: false,
  autoDismissDuration: Duration(seconds: 5),
);

Implementation

static void updateParams({
  required String id,
  // Content parameters
  ModalWidgetBuilder? builder,
  String? builderId,
  // Blur parameters
  double? blurAmount,
  bool? shouldBlurBackground,
  // Size parameters (unified for all sheet types)
  bool? isExpandable,
  double? size,
  bool resetSize = false,
  double? expandedPercentageSize,
  double? contentPaddingByDragHandle,
  SheetPosition? sheetPosition,
  // Behavior parameters
  bool? isDismissable,
  bool? blockBackgroundInteraction,
  bool? isDraggable,
  Function? onDismissed,
  Function? onExpanded,
  // Modal type and position (use with caution - changing type mid-display may cause issues)
  ModalType? modalType,
  Alignment? modalPosition,
  ModalAnimationType? modalAnimationType,
  // Snackbar-specific parameters
  bool? isSwipeable,
  Duration? autoDismissDuration,
  SnackbarDisplayMode? snackbarDisplayMode,
  int? maxStackedSnackbars,
  // Appearance parameters
  Color? backgroundColor,
  bool resetBackgroundColor = false,
  double? snackbarWidth,
  Color? barrierColor,
  Offset? offset = _noOffsetChange,
}) {
  // 1. Find the target modal content
  _ModalContent? targetContent;
  bool isInQueue = false;
  Alignment? queuePosition;
  int? queueIndex;

  // Check active controllers
  if (_dialogController.state?.uniqueId == id ||
      _dialogController.state?.id == id) {
    targetContent = _dialogController.state;
  } else if (_sheetController.state?.uniqueId == id ||
      _sheetController.state?.id == id) {
    targetContent = _sheetController.state;
  } else if (_snackbarController.state?.uniqueId == id ||
      _snackbarController.state?.id == id) {
    targetContent = _snackbarController.state;
  } else if (Modal.controller.state?.uniqueId == id ||
      Modal.controller.state?.id == id) {
    // Fallback for custom or if active controller is set but specific one isn't
    targetContent = Modal.controller.state;
  }

  // Check snackbar queue if not found yet
  if (targetContent == null) {
    final queueMap = _snackbarQueueNotifier.state;
    for (final entry in queueMap.entries) {
      final index =
          entry.value.indexWhere((c) => c.uniqueId == id || c.id == id);
      if (index != -1) {
        targetContent = entry.value[index];
        isInQueue = true;
        queuePosition = entry.key;
        queueIndex = index;
        break;
      }
    }
  }

  if (targetContent == null) {
    if (_showDebugPrints) {
      debugPrint('Modal.updateParams: No modal found with ID=$id');
    }
    return;
  }

  final newModalType = modalType ?? targetContent.modalType;

  // Create a new ModalContent with updated parameters
  // IMPORTANT: Pass the original uniqueId to preserve widget identity
  final updatedContent = _ModalContent(
    // Preserve identity to avoid widget recreation
    id: targetContent.uniqueId,
    // Content parameters
    builder: builder ?? targetContent.builder,
    builderId: builderId ?? targetContent.builderId,
    // Blur parameters
    blurAmount: blurAmount ?? targetContent.blurAmount,
    shouldBlurBackground:
        shouldBlurBackground ?? targetContent.shouldBlurBackground,
    // Size parameters
    isExpandable: isExpandable ?? targetContent.isExpandable,
    size: resetSize ? null : (size ?? targetContent.size),
    expandedPercentageSize:
        expandedPercentageSize ?? targetContent.expandedPercentageSize,
    contentPaddingByDragHandle: contentPaddingByDragHandle ??
        targetContent.contentPaddingByDragHandle,
    sheetPosition: sheetPosition ?? targetContent.sheetPosition,
    // Behavior parameters
    isDismissable: isDismissable ?? targetContent.isDismissable,
    blockBackgroundInteraction: blockBackgroundInteraction ??
        targetContent.blockBackgroundInteraction,
    isDraggable: isDraggable ?? targetContent.isDraggable,
    onDismissed: onDismissed ?? targetContent.onDismissed,
    onExpanded: onExpanded ?? targetContent.onExpanded,
    // Modal type and position
    modalType: newModalType,
    modalPosition: modalPosition ?? targetContent.modalPosition,
    modalAnimationType:
        modalAnimationType ?? targetContent.modalAnimationType,
    // Snackbar-specific parameters
    isSwipeable: isSwipeable ?? targetContent.isSwipeable,
    autoDismissDuration:
        autoDismissDuration ?? targetContent.autoDismissDuration,
    snackbarDisplayMode:
        snackbarDisplayMode ?? targetContent.snackbarDisplayMode,
    maxStackedSnackbars:
        maxStackedSnackbars ?? targetContent.maxStackedSnackbars,
    // Appearance parameters
    backgroundColor: resetBackgroundColor
        ? null
        : (backgroundColor ?? targetContent.backgroundColor),
    offset: offset == _noOffsetChange ? targetContent.offset : offset,
    snackbarWidth: snackbarWidth ?? targetContent.snackbarWidth,
    barrierColor: barrierColor ?? targetContent.barrierColor,
  );

  // Apply update
  if (isInQueue && queuePosition != null && queueIndex != null) {
    // Update in queue
    final currentQueueMap = _snackbarQueueNotifier.state;
    final updatedQueue =
        List<_ModalContent>.from(currentQueueMap[queuePosition]!);
    updatedQueue[queueIndex] = updatedContent;

    final updatedQueueMap =
        Map<Alignment, List<_ModalContent>>.from(currentQueueMap);
    updatedQueueMap[queuePosition] = updatedQueue;
    _snackbarQueueNotifier.state = updatedQueueMap;
  } else {
    // Update active controller(s)
    // If it's the active global modal, update it
    if (Modal.controller.state?.uniqueId == targetContent.uniqueId) {
      Modal.controller.state = updatedContent;
    }

    // Update type-specific controller based on NEW modal type
    switch (newModalType) {
      case ModalType.dialog:
        _dialogController.state = updatedContent;
        // Clear other controllers if type changed
        if (targetContent.modalType != ModalType.dialog) {
          _sheetController.state = null;
          _snackbarController.state = null;
        }
        break;
      case ModalType.sheet:
        _sheetController.state = updatedContent;
        // Clear other controllers if type changed
        if (targetContent.modalType != ModalType.sheet) {
          _dialogController.state = null;
          _snackbarController.state = null;
        }
        break;
      case ModalType.snackbar:
        _snackbarController.state = updatedContent;
        // Clear other controllers if type changed
        if (targetContent.modalType != ModalType.snackbar) {
          _dialogController.state = null;
          _sheetController.state = null;
        }
        break;
      case ModalType.custom:
        // Custom modals use only active modal controller
        // Clear type-specific controllers if type changed
        if (targetContent.modalType != ModalType.custom) {
          _dialogController.state = null;
          _sheetController.state = null;
          _snackbarController.state = null;
        }
        break;
    }
  }
}