snackBar method
void
snackBar({
- Key? key,
- Widget? content,
- String? message,
- Color? backgroundColor,
- double? elevation,
- EdgeInsetsGeometry? margin,
- EdgeInsetsGeometry? padding,
- double? width,
- ShapeBorder? shape,
- SnackBarBehavior? behavior,
- SnackBarAction? action,
- Duration? duration,
- Animation<
double> ? animation, - VoidCallback? onVisible,
- DismissDirection? dismissDirection,
- Clip? clipBehavior,
- int? durationMillis,
- int? animationDurationMillis,
Display the SnackBar
Implementation
void snackBar({
Key? key,
Widget? content,
String? message,
Color? backgroundColor,
double? elevation,
EdgeInsetsGeometry? margin,
EdgeInsetsGeometry? padding,
double? width,
ShapeBorder? shape,
SnackBarBehavior? behavior,
SnackBarAction? action,
Duration? duration,
Animation<double>? animation,
VoidCallback? onVisible,
DismissDirection? dismissDirection,
Clip? clipBehavior,
int? durationMillis,
int? animationDurationMillis,
}) {
//
final context = this.context;
if (useMaterial) {
//
if (content == null) {
if (message != null) {
message = message.trim();
if (message.isNotEmpty) {
content = Text(message);
}
}
if (content == null) {
return; // Nothing to display
}
}
ScaffoldMessengerState? state;
if (context != null) {
state = ScaffoldMessenger.maybeOf(context);
}
state ??= appState?.scaffoldMessengerKey?.currentState;
state?.showSnackBar(
SnackBar(
key: key,
content: content,
backgroundColor: backgroundColor,
elevation: elevation,
margin: margin,
padding: padding,
width: width,
shape: shape,
behavior: behavior,
action: action,
duration: duration ?? const Duration(milliseconds: 4000),
animation: animation,
onVisible: onVisible,
dismissDirection: dismissDirection ?? DismissDirection.down,
clipBehavior: clipBehavior ?? Clip.hardEdge,
),
);
} else {
//
if (context == null) {
return;
}
if (message == null) {
return; // Nothing to display
}
message = message.trim();
if (message.isEmpty) {
return; // Nothing to display
}
animationDurationMillis ??= 200;
durationMillis ??= 3000;
final overlayEntry = OverlayEntry(
builder: (context) => _CupertinoSnackBar(
key: key,
message: message!,
animationDurationMillis: animationDurationMillis!,
waitDurationMillis: durationMillis!,
),
);
Future.delayed(
Duration(milliseconds: durationMillis + 2 * animationDurationMillis),
overlayEntry.remove,
);
Overlay.of(context).insert(overlayEntry);
}
}