showSnackBar static method
void
showSnackBar(
- String message, {
- Color? background,
- String? title,
- SnackPosition? position,
})
Implementation
static void showSnackBar(String message, {Color? background, String? title, SnackPosition? position}) {
try {
final context = Get.context;
// Determine Toastification Style from preferences
ToastificationStyle toastStyle = ToastificationStyle.minimal; // default
try {
final storage = GetIt.I<StorageUtils>();
final savedToastStyleStr = storage.getToastStyle();
if (savedToastStyleStr != null) {
if (savedToastStyleStr == AppToastStyle.fillColor.name) {
toastStyle = ToastificationStyle.fillColored;
} else if (savedToastStyleStr == AppToastStyle.flat.name) {
toastStyle = ToastificationStyle.flat;
} else if (savedToastStyleStr == AppToastStyle.flatColored.name) {
toastStyle = ToastificationStyle.flatColored;
} else if (savedToastStyleStr == AppToastStyle.simple.name) {
toastStyle = ToastificationStyle.simple;
}
}
} catch (e) {
debugPrint('Failed to load toast style preference: $e');
}
// Determine Toastification Type based on title or content
ToastificationType toastType = ToastificationType.info;
if (title != null) {
final t = title.toLowerCase();
if (t.contains('error') || t.contains('fail') || t.contains('exception')) {
toastType = ToastificationType.error;
} else if (t.contains('success') || t.contains('done') || t.contains('complete') || t.contains('downloaded') || t.contains('ready')) {
toastType = ToastificationType.success;
} else if (t.contains('warning') || t.contains('alert') || t.contains('caution') || t.contains('queued')) {
toastType = ToastificationType.warning;
}
} else {
final m = message.toLowerCase();
if (m.contains('error') || m.contains('fail') || m.contains('exception')) {
toastType = ToastificationType.error;
} else if (m.contains('success') || m.contains('done') || m.contains('complete') || m.contains('downloaded') || m.contains('ready')) {
toastType = ToastificationType.success;
}
}
// Determine Alignment based on SnackPosition position & platform
Alignment alignment = kIsWeb ? Alignment.topRight : Alignment.bottomCenter;
if (position != null) {
if (position == SnackPosition.TOP) {
alignment = kIsWeb ? Alignment.topRight : Alignment.topCenter;
} else if (position == SnackPosition.BOTTOM) {
alignment = kIsWeb ? Alignment.topRight : Alignment.bottomCenter;
}
}
toastification.show(
context: context,
type: toastType,
style: toastStyle,
alignment: alignment,
title: title != null ? Text(title, style: const TextStyle(fontWeight: FontWeight.bold)) : null,
description: Text(message),
autoCloseDuration: const Duration(seconds: 3),
animationDuration: const Duration(milliseconds: 300),
primaryColor: background,
backgroundColor: background != null ? background.withOpacity(0.08) : null,
foregroundColor: background != null ? Colors.white : null,
);
return;
} catch (e) {
debugPrint('Toastification failed: $e');
}
// Fallback console log if context/overlay is unavailable
debugPrint('Toast: ${title ?? "Alert"}: $message');
}