showSnackBar method
void
showSnackBar({
- Key? key,
- Color? backgroundColor,
- double? elevation,
- EdgeInsetsGeometry? margin,
- EdgeInsetsGeometry? padding,
- double? width,
- HitTestBehavior? hitTestBehavior,
- SnackBarBehavior? behavior = SnackBarBehavior.floating,
- SnackBarAction? action,
- double? actionOverflowThreshold,
- bool? showCloseIcon,
- Color? closeIconColor,
- Animation<
double> ? animation, - void onVisible()?,
- DismissDirection dismissDirection = DismissDirection.down,
- Clip clipBehavior = Clip.hardEdge,
- Duration duration = const Duration(seconds: 4),
- TextStyle? style,
- TextDirection? direction,
- BorderRadius? borderRadius,
- double horizontalPadding = 16,
- double verticalPadding = 8,
- Widget? leading,
- double leadingPadding = 4,
- Widget? title,
- Widget? description,
- Widget? trailing,
- double trailingPadding = 4,
Displays a custom SnackBar from the build context with various customizable options.
This method allows for a fully customized SnackBar to be shown, including control over appearance, duration, actions, and layout. It provides flexibility to display a wide variety of SnackBar styles with different content, animations, and behaviors.
Parameters:
key
: The optional key for the SnackBar widget.backgroundColor
: The background color of the SnackBar.elevation
: The elevation (shadow) of the SnackBar.margin
: The margin around the SnackBar.padding
: The padding within the SnackBar.width
: The width of the SnackBar.hitTestBehavior
: Determines the behavior of hit testing (default isnull
).behavior
: Defines how the SnackBar behaves. The default isSnackBarBehavior.floating
.action
: The optional action button for the SnackBar.actionOverflowThreshold
: The threshold for action overflow (optional).showCloseIcon
: Whether or not to show a close icon on the SnackBar.closeIconColor
: The color of the close icon (if shown).animation
: An optional custom animation for the SnackBar.onVisible
: A callback function triggered when the SnackBar becomes visible.dismissDirection
: Direction to dismiss the SnackBar. Default isDismissDirection.down
.clipBehavior
: Defines how the content is clipped. Default isClip.hardEdge
.duration
: Duration for how long the SnackBar will be visible. Default is 4 seconds.style
: The text style for the SnackBar.direction
: The text direction for the SnackBar.borderRadius
: The border radius of the SnackBar.horizontalPadding
: Horizontal padding inside the SnackBar. Default is 16.verticalPadding
: Vertical padding inside the SnackBar. Default is 8.leading
: The leading widget in the SnackBar (e.g., an icon).leadingPadding
: The padding between the leading widget and content. Default is 4.title
: The optional title widget to display in the SnackBar.description
: The optional description widget to display in the SnackBar.trailing
: The trailing widget, typically an icon or button.trailingPadding
: The padding between the trailing widget and content. Default is 4.
Implementation
void showSnackBar({
Key? key,
Color? backgroundColor,
double? elevation,
EdgeInsetsGeometry? margin,
EdgeInsetsGeometry? padding,
double? width,
HitTestBehavior? hitTestBehavior,
SnackBarBehavior? behavior = SnackBarBehavior.floating,
SnackBarAction? action,
double? actionOverflowThreshold,
bool? showCloseIcon,
Color? closeIconColor,
Animation<double>? animation,
void Function()? onVisible,
DismissDirection dismissDirection = DismissDirection.down,
Clip clipBehavior = Clip.hardEdge,
Duration duration = const Duration(seconds: 4),
TextStyle? style,
TextDirection? direction,
BorderRadius? borderRadius,
double horizontalPadding = 16,
double verticalPadding = 8,
Widget? leading,
double leadingPadding = 4,
Widget? title,
Widget? description,
Widget? trailing,
double trailingPadding = 4,
}) {
try {
ScaffoldMessenger.of(this).showSnackBar(
SnackBar(
padding: padding ??
EdgeInsets.symmetric(
horizontal: horizontalPadding, vertical: verticalPadding),
shape: RoundedRectangleBorder(
borderRadius: borderRadius ?? BorderRadius.circular(8)),
backgroundColor: backgroundColor ?? primaryColor.withColorOpacity(.8),
content: Row(
children: [
if (leading != null) leading,
SizedBox(width: leadingPadding),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.start,
children: [
if (title != null) title,
if (title != null && description != null) 2.height(),
if (description != null) description
],
),
),
SizedBox(width: trailingPadding),
if (trailing != null) trailing,
],
),
duration: duration,
elevation: elevation,
margin: margin,
width: width,
// hitTestBehavior: hitTestBehavior,
behavior: behavior,
action: action,
actionOverflowThreshold: actionOverflowThreshold,
showCloseIcon: showCloseIcon,
closeIconColor: closeIconColor,
animation: animation,
onVisible: onVisible,
dismissDirection: dismissDirection,
clipBehavior: clipBehavior,
key: key,
),
);
} catch (e) {
if (kDebugMode) {
print('SnackBar Error $e');
}
}
}