show static method

String show({
  1. BuildContext? context,
  2. required String message,
  3. String? toastId,
  4. String? title,
  5. VoToastType type = VoToastType.info,
  6. VoToastPosition? position,
  7. VoToastStyle? style,
  8. Widget? leading,
  9. Widget? action,
  10. Duration? duration,
  11. VoidCallback? onDismiss,
})

Shows a new toast notification.

Pass context when available; otherwise falls back to voNavigatorKey. Returns the toast id for use with dismissById.

Implementation

static String show({
  BuildContext? context,

  /// The message of the toast.
  required String message,

  /// The ID of the toast. If not provided, a unique ID will be generated.
  String? toastId,

  /// The title of the toast.
  String? title,

  /// The type of the toast.
  VoToastType type = VoToastType.info,

  /// The position of the toast.
  VoToastPosition? position,

  /// The style of the toast.
  VoToastStyle? style,

  /// The leading widget of the toast.
  Widget? leading,

  /// The action widget of the toast.
  Widget? action,

  /// The duration of the toast.
  Duration? duration,

  /// The callback to be called when the toast is dismissed.
  VoidCallback? onDismiss,
}) {
  final ctx = _resolveContext(context);
  if (!ctx.mounted) {
    throw const VoToastException(
      'Cannot show toast: no mounted BuildContext is available.',
    );
  }

  if (message.trim().isEmpty) {
    throw const VoToastException('Message must not be empty.');
  }

  final id = toastId ?? DateTime.now().microsecondsSinceEpoch.toString();
  final toastTheme = ctx.componentTheme.toast;
  final maxWidth = toastTheme.maxWidth;
  final widthFactor = toastTheme.widthFactor;

  if (widthFactor != null && (widthFactor <= 0 || widthFactor > 1)) {
    throw const VoToastException(
      'widthFactor must be greater than 0 and less than or equal to 1.',
    );
  }

  if (maxWidth != null && maxWidth <= 0) {
    throw const VoToastException('maxWidth must be greater than 0.');
  }

  if (_controller.overlayFor(ctx) == null) {
    throw const VoToastException(
      'Cannot show toast: no Overlay is available.',
    );
  }

  if (toastId != null && _controller.containsId(toastId)) {
    _controller.dismissByIdImmediately(toastId);
  }

  final data = VoToastData(
    id: id,
    message: message,
    title: title,
    type: type,
    position: position ?? toastTheme.defaultPosition,
    style: style ?? toastTheme.defaultStyle,
    leading: leading,
    action: action,
    duration: duration ?? toastTheme.duration,
    onDismiss: onDismiss,
    theme: toastTheme,
  );

  _controller.show(ctx, data);
  return id;
}