show method

void show({
  1. required BuildContext context,
  2. String? title,
  3. required String message,
  4. IconData? icon,
  5. Color? backgroundColor,
  6. Color? textColor,
  7. Color? iconColor,
  8. Duration duration = const Duration(seconds: 4),
  9. ToastPosition position = ToastPosition.topCenter,
  10. bool showCloseButton = true,
  11. EdgeInsetsGeometry margin = const EdgeInsets.all(16),
  12. double borderRadius = 8.0,
  13. double? width,
  14. double? height,
  15. ToastType type = ToastType.custom,
  16. BoxShadow? shadow,
  17. 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);
    }
  });
}