show static method

void show({
  1. required BuildContext context,
  2. required String message,
  3. String? description,
  4. Duration duration = const Duration(seconds: 2),
  5. ToastPosition position = ToastPosition.bottom,
  6. ToastType type = ToastType.info,
  7. TextStyle textStyle = const TextStyle(color: Colors.white),
  8. BorderRadiusGeometry borderRadius = const BorderRadius.all(Radius.circular(8)),
  9. Widget? icon,
  10. VoidCallback? onTap,
})

Displays a toast with customizable parameters.

  • context: The BuildContext from which to display the toast. This must be provided.
  • message: The main message of the toast (required).
  • description: An optional description to display below the main message.
  • duration: The duration the toast will be visible. Default is Duration(seconds: 2).
  • position: The position where the toast will appear (top, bottom). Default is ToastPosition.bottom.
  • type: The type of the toast, which controls the background color. Options include:
    • ToastType.danger: Red background (for error or danger).
    • ToastType.warning: Orange background (for warnings).
    • ToastType.success: Green background (for success messages).
    • ToastType.info: Blue background (for informational messages).
  • textStyle: The text style for the toast message. Default is white text.
  • borderRadius: The border radius of the toast container. Default is rounded corners with a radius of 8.
  • icon: An optional icon to display at the start of the toast.
  • onTap: An optional callback that gets triggered when the user taps on the toast.

Example usage:

ToastManager.show(
  context: context,
  message: "Event has been created",
  description: "Sunday, December 03, 2023 at 9:00 AM",
  duration: Duration(seconds: 3),
  position: ToastPosition.bottom,
  type: ToastType.success,
  icon: Icon(Icons.check_circle, color: Colors.white),
  onTap: () {
    print("Toast tapped!");
  },
);

Implementation

static void show({
  required BuildContext context,
  required String message,
  String? description,
  Duration duration = const Duration(seconds: 2),
  ToastPosition position = ToastPosition.bottom,
  ToastType type = ToastType.info,
  TextStyle textStyle = const TextStyle(color: Colors.white),
  BorderRadiusGeometry borderRadius =
      const BorderRadius.all(Radius.circular(8)),
  Widget? icon,
  VoidCallback? onTap,
}) {
  final overlay = Overlay.of(context);
  Color backgroundColor;

  // Determine the background color based on the toast type
  switch (type) {
    case ToastType.danger:
      backgroundColor = Colors.red;
      break;
    case ToastType.warning:
      backgroundColor = Colors.orange;
      break;
    case ToastType.success:
      backgroundColor = Colors.green;
      break;
    case ToastType.info:
      backgroundColor = Colors.blue;
      break;
  }

  late final OverlayEntry overlayEntry;
  overlayEntry = OverlayEntry(
    builder: (context) => ToastWidget(
      message: message,
      description: description,
      duration: duration,
      position: position,
      backgroundColor: backgroundColor,
      textStyle: textStyle,
      borderRadius: borderRadius,
      icon: icon,
      onTap: () {
        onTap?.call();
        _removeToast(overlayEntry);
      },
    ),
  );

  _toasts.add(overlayEntry);
  overlay.insert(overlayEntry);

  // Remove the toast after the specified duration
  Future.delayed(duration, () {
    _removeToast(overlayEntry);
  });
}