showToast static method

void showToast({
  1. required BuildContext context,
  2. required String message,
  3. Color backgroundColor = const Color(0xFF002147),
  4. TextStyle textStyle = const TextStyle(color: Colors.white, fontWeight: FontWeight.w600),
  5. Duration duration = const Duration(seconds: 3),
  6. Duration animationDuration = const Duration(milliseconds: 500),
})

Displays a custom toast message

Implementation

static void showToast({
  required BuildContext context,
  required String message,
  Color backgroundColor = const Color(0xFF002147),
  TextStyle textStyle = const TextStyle(color: Colors.white, fontWeight: FontWeight.w600),
  Duration duration = const Duration(seconds: 3),
  Duration animationDuration = const Duration(milliseconds: 500),
}) {
  // Remove the previous toast if any
  _removeToast();

  final overlayState = Overlay.of(context);
  if (overlayState == null) return;

  _overlayEntry = OverlayEntry(
    builder: (context) {
      return Positioned(
        bottom: 50.0,
        left: MediaQuery.of(context).size.width * 0.1,
        width: MediaQuery.of(context).size.width * 0.8,
        child: ToastAnimation(
          child: ToastWidget(
            message: message,
            backgroundColor: backgroundColor,
            textStyle: textStyle,
          ),
          animationDuration: animationDuration,
        ),
      );
    },
  );

  overlayState.insert(_overlayEntry!);

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