toast static method

void toast({
  1. required String message,
  2. String? title,
  3. UToastType type = UToastType.neutral,
  4. IconData? icon,
  5. UToastPosition position = UToastPosition.bottom,
  6. Duration? duration,
  7. Duration animationDuration = const Duration(milliseconds: 600),
  8. Color? backgroundColor,
  9. Color? foregroundColor,
  10. EdgeInsets margin = const EdgeInsets.all(24),
  11. double borderRadius = 12,
  12. double elevation = 6,
  13. VoidCallback? onTap,
  14. VoidCallback? onDismiss,
})

Floating pill message (top/center/bottom) that disappears by itself; works over dialogs too. UToast.toast(message: "Copied", position: UToastPosition.top)

Implementation

static void toast({
  required String message,
  String? title,
  UToastType type = UToastType.neutral,
  IconData? icon,
  UToastPosition position = UToastPosition.bottom,
  Duration? duration,
  Duration animationDuration = const Duration(milliseconds: 600),
  Color? backgroundColor,
  Color? foregroundColor,
  EdgeInsets margin = const EdgeInsets.all(24),
  double borderRadius = 12,
  double elevation = 6,
  VoidCallback? onTap,
  VoidCallback? onDismiss,
}) {
  if (message.isNullOrEmpty()) return;
  final OverlayState? overlay = navigatorKey.currentState?.overlay;
  if (overlay == null) return;
  clearToast();
  final BuildContext? context = navigatorKey.currentContext;
  if (context == null) return;
  final Color background = backgroundColor ?? (type == UToastType.neutral ? _scheme.inverseSurface : _backgroundFor(type));
  final Color foreground = foregroundColor ?? (type == UToastType.neutral ? _scheme.onInverseSurface : _foregroundFor(type));
  final IconData? resolvedIcon = icon ?? _iconFor(type);
  late final OverlayEntry entry;

  entry = OverlayEntry(
    builder: (BuildContext context) => _UToastCard(
      message: message,
      title: title,
      icon: resolvedIcon,
      background: background,
      foreground: foreground,
      position: position,
      margin: margin,
      borderRadius: borderRadius,
      elevation: elevation,
      animationDuration: animationDuration,
      onTap: onTap,
    ),
  );

  _toastEntry = entry;
  overlay.insert(entry);

  if (duration != Duration.zero) {
    Future<void>.delayed(duration ?? Duration(seconds: U.snackBarDuration), () {
      if (_toastEntry != entry) return;

      _toastEntry = null;

      if (entry.mounted) {
        entry.remove();
      }

      onDismiss?.call();
    });
  }
}