showToast function

ToastOverlay showToast({
  1. required BuildContext context,
  2. required ToastBuilder builder,
  3. ToastLocation location = ToastLocation.bottomRight,
  4. bool dismissible = true,
  5. Curve curve = Curves.easeOutCubic,
  6. Duration entryDuration = const Duration(milliseconds: 500),
  7. VoidCallback? onClosed,
  8. Duration showDuration = const Duration(seconds: 5),
})

Displays a toast notification with sophisticated positioning and animation.

Creates and shows a toast notification using the provided builder function within the nearest ToastLayer in the widget tree. The toast appears at the specified location with configurable animation, dismissal behavior, and automatic timeout.

The function handles theme capture and data inheritance to ensure the toast maintains consistent styling and access to inherited data from the calling context. Toast notifications are managed through a layered system that supports stacking, expansion, and smooth animations.

Parameters:

  • context (BuildContext, required): The build context for theme and data capture
  • builder (ToastBuilder, required): Function that builds the toast content widget
  • location (ToastLocation, default: bottomRight): Screen position for the toast
  • dismissible (bool, default: true): Whether users can dismiss via gesture
  • curve (Curve, default: easeOutCubic): Animation curve for entry/exit transitions
  • entryDuration (Duration, default: 500ms): Duration for toast entry animation
  • onClosed (VoidCallback?, optional): Callback invoked when toast is dismissed
  • showDuration (Duration, default: 5s): Auto-dismiss timeout duration

Returns: A ToastOverlay instance that provides control methods for the displayed toast.

Throws:

Example:

final toast = showToast(
  context: context,
  builder: (context, overlay) => AlertCard(
    title: 'Success',
    message: 'Operation completed successfully',
    onDismiss: overlay.close,
  ),
  location: ToastLocation.topRight,
  showDuration: Duration(seconds: 3),
);

Implementation

ToastOverlay showToast({
  required BuildContext context,
  required ToastBuilder builder,
  ToastLocation location = ToastLocation.bottomRight,
  bool dismissible = true,
  Curve curve = Curves.easeOutCubic,
  Duration entryDuration = const Duration(milliseconds: 500),
  VoidCallback? onClosed,
  Duration showDuration = const Duration(seconds: 5),
}) {
  $shadEvent?.onToastOpened(context);
  CapturedThemes? themes;
  CapturedData? data;
  _ToastLayerState? layer = Data.maybeFind<_ToastLayerState>(context);
  if (layer != null) {
    themes = InheritedTheme.capture(from: context, to: layer.context);
    data = Data.capture(from: context, to: layer.context);
  } else {
    layer = Data.maybeFindMessenger<_ToastLayerState>(context);
  }
  assert(layer != null, 'No ToastLayer found in context');
  final entry = ToastEntry(
    builder: builder,
    location: location,
    dismissible: dismissible,
    curve: curve,
    duration: entryDuration,
    themes: themes,
    data: data,
    onClosed: onClosed,
    showDuration: showDuration,
  );
  return layer!.addEntry(entry);
}