showToast function
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),
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 capturebuilder(ToastBuilder, required): Function that builds the toast content widgetlocation(ToastLocation, default: bottomRight): Screen position for the toastdismissible(bool, default: true): Whether users can dismiss via gesturecurve(Curve, default: easeOutCubic): Animation curve for entry/exit transitionsentryDuration(Duration, default: 500ms): Duration for toast entry animationonClosed(VoidCallback?, optional): Callback invoked when toast is dismissedshowDuration(Duration, default: 5s): Auto-dismiss timeout duration
Returns: A ToastOverlay instance that provides control methods for the displayed toast.
Throws:
- AssertionError if no ToastLayer is found in the widget tree.
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);
}