show static method
void
show({
- required BuildContext context,
- required String message,
- String? title,
- QuickSnackBarType type = QuickSnackBarType.success,
- SnackBarPosition position = SnackBarPosition.bottom,
- Color? customColor,
- Duration duration = const Duration(seconds: 3),
- double elevation = 8.0,
- bool showIcon = true,
- bool isDismissible = true,
Displays an animated snackbar with the specified configuration.
This method creates and shows a customizable snackbar overlay that appears with smooth animations and automatically dismisses after the specified duration.
Parameters:
Required Parameters:
context: The BuildContext used to access the Overlay. Must have an Overlay ancestor.message: The main text content to display in the snackbar.
Optional Parameters:
title: Optional title text displayed above the message in bold.type: The visual style of the snackbar. Defaults to QuickSnackBarType.success.position: Where the snackbar appears on screen. Defaults to SnackBarPosition.bottom.customColor: Custom background color when using QuickSnackBarType.custom.duration: How long the snackbar stays visible. Defaults to 3 seconds.elevation: The visual elevation/shadow depth. Defaults to 8.0 (currently unused).showIcon: Whether to display the type-specific icon. Defaults to true.isDismissible: Whether users can tap to dismiss early. Defaults to true.
Example Usage:
QuickSnackBarUtil.show(
context: context,
title: 'Success',
message: 'Your changes have been saved successfully!',
type: QuickSnackBarType.success,
position: SnackBarPosition.top,
duration: Duration(seconds: 4),
);
Notes:
- The snackbar will automatically remove itself after
duration+ 400ms - If the overlay is not available in the context, the method returns early
- Multiple snackbars can be shown simultaneously and will stack appropriately
Implementation
static void show({
required BuildContext context,
required String message,
String? title,
QuickSnackBarType type = QuickSnackBarType.success,
SnackBarPosition position = SnackBarPosition.bottom,
Color? customColor,
Duration duration = const Duration(seconds: 3),
double elevation = 8.0,
bool showIcon = true,
bool isDismissible = true,
}) {
final overlay = Overlay.of(context);
final (backgroundColor, iconData) = _getTypeProperties(type, customColor);
final overlayEntry = OverlayEntry(
builder: (context) => Positioned(
top: position == SnackBarPosition.top ? 50 : null,
bottom: position == SnackBarPosition.bottom ? 50 : null,
left: 16,
right: 16,
child: _AnimatedSnackBar(
title: title,
message: message,
backgroundColor: backgroundColor,
duration: duration,
elevation: elevation,
position: position,
iconData: showIcon ? iconData : null,
isDismissible: isDismissible,
onDismiss: () {},
//onDismiss: () => overlayEntry.remove(),
),
),
);
overlay.insert(overlayEntry);
// Auto-remove the snackbar after duration + animation time
Future.delayed(duration + const Duration(milliseconds: 400), () {
if (overlayEntry.mounted) {
overlayEntry.remove();
}
});
}