showToast function

void showToast(
  1. BuildContext context,
  2. Toast toast, {
  3. AlignmentGeometry alignment = Alignment.topRight,
  4. double? width,
})

Displays a Toast above the current contents of the app.

Implementation

void showToast(
  BuildContext context,
  Toast toast, {
  AlignmentGeometry alignment = Alignment.topRight,
  double? width,
}) {
  final overlayState = Overlay.of(context, rootOverlay: true);
  final controller = ToastifyController.instance;
  final key = controller.genKey(width, alignment);
  if (!controller.has(key)) {
    final toast = Toastify(
      key: key,
      alignment: alignment,
      width: width,
    );
    controller.add(key, toast);
    final overlayEntry = OverlayEntry(
      builder: (_) => toast,
    );
    toast.overlayEntry = overlayEntry;
    overlayState.insert(overlayEntry);
  }
  WidgetsBinding.instance.addPostFrameCallback((_) {
    controller.get(key).add(toast);
  });
}