snackBar static method

void snackBar({
  1. required String message,
  2. String? title,
  3. UToastType type = UToastType.neutral,
  4. IconData? icon,
  5. Duration duration = const Duration(seconds: 4),
  6. Color? backgroundColor,
  7. Color? foregroundColor,
  8. TextStyle? textStyle,
  9. SnackBarAction? action,
  10. String? actionLabel,
  11. VoidCallback? onAction,
  12. bool showCloseIcon = false,
  13. SnackBarBehavior? behavior,
  14. double? borderRadius,
  15. ShapeBorder? shape,
  16. double? elevation,
  17. DismissDirection dismissDirection = DismissDirection.down,
  18. EdgeInsets? margin,
  19. double? width,
  20. bool clearQueue = false,
  21. VoidCallback? onDismiss,
})

Implementation

static void snackBar({
  required String message,
  String? title,
  UToastType type = UToastType.neutral,
  IconData? icon,
  Duration duration = const Duration(seconds: 4),
  Color? backgroundColor,
  Color? foregroundColor,
  TextStyle? textStyle,
  SnackBarAction? action,
  String? actionLabel,
  VoidCallback? onAction,
  bool showCloseIcon = false,
  SnackBarBehavior? behavior,
  double? borderRadius,
  ShapeBorder? shape,
  double? elevation,
  DismissDirection dismissDirection = DismissDirection.down,
  EdgeInsets? margin,
  double? width,
  bool clearQueue = false,
  VoidCallback? onDismiss,
}) {
  if (message.isNullOrEmpty()) return;
  final BuildContext ctx = navigatorKey.currentContext!;
  // Resolve visuals from explicit overrides first, then the semantic type
  final Color background = backgroundColor ?? _backgroundFor(type);
  final Color foreground = foregroundColor ?? _foregroundFor(type);
  final IconData? resolvedIcon = icon ?? _iconFor(type);
  final ScaffoldMessengerState messenger = ScaffoldMessenger.of(ctx);
  if (clearQueue) messenger.clearSnackBars();

  // Build an action from either a ready SnackBarAction or a label+callback pair
  final SnackBarAction? resolvedAction = action ??
      (actionLabel != null
          ? SnackBarAction(label: actionLabel, textColor: foreground, onPressed: onAction ?? () {})
          : null);

  messenger
      .showSnackBar(
        SnackBar(
          content: Row(
            children: <Widget>[
              if (resolvedIcon != null) Icon(resolvedIcon, color: foreground).pOnly(right: 12),
              Expanded(
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    if (title != null) UTextTitleSmall(title, color: foreground),
                    if (textStyle != null) Text(message, style: textStyle) else UTextBodyMedium(message, color: foreground),
                  ],
                ),
              ),
            ],
          ),
          backgroundColor: background,
          duration: duration,
          action: resolvedAction,
          showCloseIcon: showCloseIcon,
          closeIconColor: foreground,
          dismissDirection: dismissDirection,
          behavior: behavior ?? ((margin != null || width != null) ? SnackBarBehavior.floating : null),
          margin: margin,
          width: width,
          elevation: elevation,
          shape: shape ?? (borderRadius != null ? RoundedRectangleBorder(borderRadius: BorderRadius.circular(borderRadius)) : null),
        ),
      )
      .closed
      .then((_) => onDismiss?.call());
}