snackBar static method
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,
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());
}