show static method
- BuildContext context,
- String message, {
- Duration? duration,
- ToastSeverity severity = ToastSeverity.info,
- CellStyle? style,
- ToastAction? action,
- Object? id,
- bool persistent = false,
Shows message as a toast via the nearest enclosing Toaster.
Throws if there is no Toaster above context.
severity picks a default color; style overrides it outright when
supplied (merged over the severity's style). An optional action
adds a hotkey affordance shown in the toast.
Reusing a non-null id replaces that toast in place and starts a fresh
lifetime. IDs are scoped to this host and compared with ==. Omitting an
ID creates a distinct toast. The returned handle belongs to this exact
instance, not to later replacements.
persistent disables expiry; explicit dismissal, replacement, eviction
and host disposal still remove the toast. It cannot be combined with an
explicit duration. Otherwise null duration inherits the host default.
A transient toast must have a positive duration.
Implementation
static ToastHandle show(
BuildContext context,
String message, {
Duration? duration,
ToastSeverity severity = ToastSeverity.info,
CellStyle? style,
ToastAction? action,
Object? id,
bool persistent = false,
}) {
if (persistent && duration != null) {
throw ArgumentError('A persistent toast cannot have a duration.');
}
// An action from a handler: locate the toaster without subscribing the
// caller to anything (the state never changes identity or notifies).
final state = context.findAncestorStateOfType<_ToasterState>();
if (state == null) {
throw StateError(
'No Toaster above this BuildContext. Wrap your app in a Toaster.',
);
}
final lifetime = persistent ? null : duration ?? state.widget.duration;
if (lifetime != null && lifetime <= Duration.zero) {
throw ArgumentError.value(lifetime, 'duration', 'Must be positive');
}
final colors = Theme.of(context).colorScheme;
final resolved = style == null
? _styleForSeverity(severity, colors)
: _styleForSeverity(severity, colors).merge(style);
return state._enqueue(message, lifetime, resolved, severity, action, id);
}