show method
void
show({
- required BuildContext context,
- String? title,
- required String message,
- IconData? icon,
- Color? backgroundColor,
- Color? textColor,
- Color? iconColor,
- Duration duration = const Duration(seconds: 4),
- ToastPosition position = ToastPosition.topCenter,
- bool showCloseButton = true,
- EdgeInsetsGeometry margin = const EdgeInsets.all(16),
- double borderRadius = 8.0,
- double? width,
- double? height,
- ToastType type = ToastType.custom,
- BoxShadow? shadow,
- double? maxWidth,
Implementation
void show({
required BuildContext context,
String? title,
required String message,
IconData? icon,
Color? backgroundColor,
Color? textColor,
Color? iconColor,
Duration duration = const Duration(seconds: 4),
ToastPosition position = ToastPosition.topCenter,
bool showCloseButton = true,
EdgeInsetsGeometry margin = const EdgeInsets.all(16),
double borderRadius = 8.0,
double? width,
double? height,
ToastType type = ToastType.custom,
BoxShadow? shadow,
double? maxWidth,
}) {
final overlayState = Overlay.of(context);
// Remove oldest toast if we have too many
if (_activeToasts.length >= _maxToasts) {
_removeToast(_activeToasts.first);
}
late OverlayEntry overlayEntry;
final positioningData = _getPositioningData(position, context, width, maxWidth);
overlayEntry = OverlayEntry(
builder: (context) => Positioned(
top: positioningData['top'],
bottom: positioningData['bottom'],
left: positioningData['left'],
right: positioningData['right'],
width: positioningData['width'],
child: Material(
color: Colors.transparent,
child: ModToast(
title: title,
message: message,
icon: icon,
backgroundColor: backgroundColor,
textColor: textColor,
iconColor: iconColor,
duration: duration,
position: position,
showCloseButton: showCloseButton,
margin: margin,
borderRadius: borderRadius,
width: width,
height: height,
type: type,
shadow: shadow,
maxWidth: maxWidth,
onClose: () => _removeToast(overlayEntry),
),
),
),
);
overlayState.insert(overlayEntry);
_activeToasts.add(overlayEntry);
// Auto remove after duration + animation time
Future.delayed(duration + const Duration(milliseconds: 300), () {
if (_activeToasts.contains(overlayEntry)) {
_removeToast(overlayEntry);
}
});
}