Skip to content

Instantly share code, notes, and snippets.

@wispborne
Last active March 31, 2024 21:55
Show Gist options
  • Save wispborne/3387a35b744afb51c4d6d2586e66fbd0 to your computer and use it in GitHub Desktop.
Save wispborne/3387a35b744afb51c4d6d2586e66fbd0 to your computer and use it in GitHub Desktop.
enum SnackBarType {
info,
warn,
error,
}
showSnackBar({
required BuildContext context,
required Widget content,
bool? clearPreviousSnackBars = true,
SnackBarType? type,
Color? backgroundColor,
double? elevation,
EdgeInsetsGeometry? margin,
EdgeInsetsGeometry? padding,
double? width,
ShapeBorder? shape,
HitTestBehavior? hitTestBehavior,
SnackBarBehavior? behavior,
SnackBarAction? action,
double? actionOverflowThreshold,
bool? showCloseIcon,
Color? closeIconColor,
Duration duration = const Duration(milliseconds: 4000),
Animation<double>? animation,
void Function()? onVisible,
DismissDirection? dismissDirection,
Clip clipBehavior = Clip.hardEdge,
}) {
if (clearPreviousSnackBars == true) {
ScaffoldMessenger.of(context).clearSnackBars();
}
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: type != null
? DefaultTextStyle.merge(
child: content, style: const TextStyle(color: Colors.white, fontWeight: FontWeight.bold))
: content,
backgroundColor: switch (type) {
SnackBarType.info => Colors.blue,
SnackBarType.warn => vanillaWarningColor,
SnackBarType.error => vanillaErrorColor,
null => Theme.of(context).snackBarTheme.backgroundColor
},
elevation: elevation,
margin: margin,
padding: padding,
width: width,
shape: shape,
hitTestBehavior: hitTestBehavior,
behavior: behavior,
action: action,
actionOverflowThreshold: actionOverflowThreshold,
showCloseIcon: showCloseIcon,
closeIconColor: closeIconColor,
duration: duration,
animation: animation,
onVisible: onVisible,
dismissDirection: dismissDirection,
clipBehavior: clipBehavior,
));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment