show static method

void show(
  1. BuildContext context, {
  2. required String message,
  3. Color? backgroundColor,
  4. Color? textColor,
  5. IconData? icon,
  6. Duration? duration,
  7. SnackBarAction? action,
  8. SnackBarBehavior? behavior,
  9. EdgeInsets? margin,
  10. bool? showCloseButton,
  11. bool? showProgressIndicator,
  12. SnackbarPosition? position,
})

Shows a custom snackbar with full control over appearance. Parameters:

  • context: BuildContext for showing the snackbar
  • message: The text message to display
  • backgroundColor: Background color (defaults to grey)
  • textColor: Text color (defaults to white)
  • icon: Optional icon to display before the message
  • duration: How long the snackbar stays visible
  • action: Optional action button
  • behavior: Fixed or floating (defaults to config)
  • margin: Margin around the snackbar (defaults to config)
  • showCloseButton: Whether to show close button (defaults to config)
  • showProgressIndicator: Whether to show countdown progress bar (defaults to config)
  • position: Position of snackbar (top or bottom, defaults to config)

Implementation

static void show(
  BuildContext context, {
  required String message,
  Color? backgroundColor,
  Color? textColor,
  IconData? icon,
  Duration? duration,
  SnackBarAction? action,
  SnackBarBehavior? behavior,
  EdgeInsets? margin,
  bool? showCloseButton,
  bool? showProgressIndicator,
  SnackbarPosition? position,
}) {
  // Use provided values or fall back to config defaults
  final effectiveDuration = duration ?? SnackbarKitConfig.defaultDuration;
  final effectiveBehavior = behavior ?? SnackbarKitConfig.defaultBehavior;
  final effectiveMargin = margin ?? SnackbarKitConfig.defaultMargin;
  final effectiveShowClose =
      showCloseButton ?? SnackbarKitConfig.showCloseButton;
  final effectiveShowProgress =
      showProgressIndicator ?? SnackbarKitConfig.showProgressIndicator;
  final effectivePosition = position ?? SnackbarKitConfig.defaultPosition;
  final effectiveBackgroundColor =
      backgroundColor ?? SnackbarColors.normalBackground;
  final effectiveTextColor = textColor ?? Colors.white;

  // If position is TOP, use custom overlay
  if (effectivePosition == SnackbarPosition.top) {
    _showTopSnackbar(
      context,
      message: message,
      backgroundColor: effectiveBackgroundColor,
      textColor: effectiveTextColor,
      icon: icon,
      duration: effectiveDuration,
      action: action,
      margin: effectiveMargin,
      showCloseButton: effectiveShowClose,
      showProgressIndicator: effectiveShowProgress,
    );
    return;
  }

  // BOTTOM position - use native SnackBar
  if (effectiveShowProgress) {
    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(
        content: _SnackbarContentWithProgress(
          message: message,
          icon: icon,
          textColor: effectiveTextColor,
          duration: effectiveDuration,
          progressColor: effectiveTextColor.withValues(alpha: 0.3),
          progressActiveColor: effectiveTextColor,
          isTop: false,
        ),
        backgroundColor: effectiveBackgroundColor,
        duration: effectiveDuration,
        behavior: effectiveBehavior,
        margin: effectiveBehavior == SnackBarBehavior.floating
            ? effectiveMargin
            : null,
        action: action,
        showCloseIcon: effectiveShowClose,
        closeIconColor: effectiveTextColor,
        padding: EdgeInsets.zero,
      ),
    );
  } else {
    final content = Row(
      children: [
        if (icon != null) ...[
          Icon(icon, color: effectiveTextColor, size: 24),
          const SizedBox(width: 12),
        ],
        Expanded(
          child: Text(
            message,
            style: TextStyle(color: effectiveTextColor, fontSize: 14),
          ),
        ),
      ],
    );
    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(
        content: content,
        backgroundColor: effectiveBackgroundColor,
        duration: effectiveDuration,
        behavior: effectiveBehavior,
        margin: effectiveBehavior == SnackBarBehavior.floating
            ? effectiveMargin
            : null,
        action: action,
        showCloseIcon: effectiveShowClose,
        closeIconColor: effectiveTextColor,
      ),
    );
  }
}