showSnackBar static method
void
showSnackBar(
- BuildContext context, {
- required Widget content,
- bool hideWithAnimation = true,
- SnackBarAction? action,
- Animation<
double> ? animation, - Color? backgroundColor,
- SnackBarBehavior? behaviour,
- Duration duration = const Duration(seconds: 4),
- double? elevation,
- EdgeInsetsGeometry? margin,
- EdgeInsetsGeometry? padding,
- ShapeBorder? shape,
- double? width,
- SnackBarType snackBarType = SnackBarType.normal,
- SnackBarShape snackBarShape = SnackBarShape.normal,
Memunculkan snackbar
Implementation
static void showSnackBar(
BuildContext context, {
required Widget content,
bool hideWithAnimation = true,
SnackBarAction? action,
Animation<double>? animation,
Color? backgroundColor,
SnackBarBehavior? behaviour,
Duration duration = const Duration(seconds: 4),
double? elevation,
/// If margin not null , set default behaviour = SnackBarBehaviour.floating
EdgeInsetsGeometry? margin,
EdgeInsetsGeometry? padding,
ShapeBorder? shape,
double? width,
SnackBarType snackBarType = SnackBarType.normal,
SnackBarShape snackBarShape = SnackBarShape.normal,
}) {
final scaffoldMessager = ScaffoldMessenger.of(context);
Color? _backgroundColor = backgroundColor;
if (hideWithAnimation) {
scaffoldMessager.hideCurrentSnackBar();
} else {
scaffoldMessager.removeCurrentSnackBar();
}
if (margin != null) {
// ignore: parameter_assignments
behaviour = SnackBarBehavior.floating;
}
switch (snackBarType) {
case SnackBarType.success:
_backgroundColor = Colors.green;
break;
case SnackBarType.error:
_backgroundColor = Colors.red;
break;
case SnackBarType.warning:
_backgroundColor = Colors.orange;
break;
case SnackBarType.info:
_backgroundColor = Colors.lightBlue;
break;
default:
// ignore: parameter_assignments
_backgroundColor = backgroundColor;
break;
}
switch (snackBarShape) {
case SnackBarShape.rounded:
shape = RoundedRectangleBorder(borderRadius: BorderRadius.circular(15));
break;
default:
// ignore: parameter_assignments
shape = RoundedRectangleBorder(borderRadius: BorderRadius.circular(0));
break;
}
scaffoldMessager.showSnackBar(
SnackBar(
content: content,
action: action,
animation: animation,
backgroundColor: _backgroundColor,
behavior: behaviour,
duration: duration,
elevation: elevation,
margin: margin,
padding: padding,
shape: shape,
width: width,
),
);
}