showToast static method
void
showToast({
- required BuildContext context,
- String? msg,
- Widget? child,
- Duration toastDuration = const Duration(seconds: 2),
- ToastGravity? gravity,
- Duration fadeDuration = const Duration(milliseconds: 350),
Implementation
static void showToast({
required BuildContext context,
String? msg,
Widget? child,
Duration toastDuration = const Duration(seconds: 2),
ToastGravity? gravity,
Duration fadeDuration = const Duration(milliseconds: 350),
}) {
if (child == null && msg == null) {
throw ("Error: child or msg is null, Please call child or msg before showing toast.");
}
cancelToast();
gravity ??= ToastGravity.bottom;
child ??= _DefaultToastWidget(msg: msg!);
_ToastStateFul newChild = _ToastStateFul(
fadeDuration: fadeDuration,
duration: toastDuration,
child: child,
);
_entry = OverlayEntry(builder: (context) {
return _getWidgetBasedOnGravity(newChild, gravity);
});
try {
OverlayState overlay = Overlay.of(context);
overlay.insert(_entry!);
} catch (err) {
cancelToast();
}
}