showToast function

void showToast(
  1. BuildContext context,
  2. String message, {
  3. Duration duration = const Duration(seconds: 2),
  4. Alignment alignment = Alignment.bottomCenter,
  5. TextStyle? textStyle,
  6. BoxDecoration? background,
  7. EdgeInsets? padding,
  8. Offset? offsetAnimationStart,
})

Displays the toast.

The only method which is called. Only the context and message are required, the rest is optional.

Implementation

void showToast(
  BuildContext context,
  String message, {
  Duration duration = const Duration(seconds: 2),
  Alignment alignment = Alignment.bottomCenter,
  TextStyle? textStyle,
  BoxDecoration? background,
  EdgeInsets? padding,
  Offset? offsetAnimationStart,
}) {
  // Only initialize once. I *think* this is safe to do.
  _overlayState ??= Overlay.of(context);

  _toastWithCorrespondingOverlayEntry.forEach((key, value) {
    key.hide();
  });

  final ToastWidget toast = ToastWidget(
    message: message,
    duration: duration,
    alignment: alignment,
    textStyle: textStyle,
    background: background,
    padding: padding,
    offsetAnimationStart: offsetAnimationStart,
  );

  final OverlayEntry overlayEntry =
      OverlayEntry(builder: (BuildContext context) => toast);
  _overlayState!.insert(overlayEntry);
  _toastWithCorrespondingOverlayEntry[toast] = overlayEntry;
}