show static method

void show(
  1. BuildContext context, {
  2. required String message,
  3. Duration duration = const Duration(seconds: 2),
})

Implementation

static void show(
  BuildContext context, {
  required String message,
  Duration duration = const Duration(seconds: 2),
}) {
  // Remove previous toast if showing
  _currentToast?.remove();
  _currentToast = null;

  final overlay = Overlay.of(context);

  final overlayEntry = OverlayEntry(
    builder: (context) => Positioned(
      bottom: 60,
      left: 20,
      right: 20,
      child: IgnorePointer(
        child: SafeArea(
          child: Center(
            child: Material(
              color: Colors.transparent,
              child: AnimatedOpacity(
                opacity: 1,
                duration: const Duration(milliseconds: 200),
                child: Container(
                  constraints: const BoxConstraints(maxWidth: 400),
                  padding: const EdgeInsets.symmetric(
                    horizontal: 20,
                    vertical: 12,
                  ),
                  decoration: BoxDecoration(
                    color: Colors.black87,
                    borderRadius: BorderRadius.circular(25),
                    boxShadow: const [
                      BoxShadow(
                        color: Colors.black26,
                        blurRadius: 8,
                        offset: Offset(0, 3),
                      ),
                    ],
                  ),
                  child: Text(
                    message,
                    textAlign: TextAlign.center,
                    style: const TextStyle(
                      color: Colors.white,
                      fontSize: 14,
                    ),
                  ),
                ),
              ),
            ),
          ),
        ),
      ),
    ),
  );

  _currentToast = overlayEntry;
  overlay.insert(overlayEntry);

  Future.delayed(duration, () {
    if (_currentToast == overlayEntry) {
      overlayEntry.remove();
      _currentToast = null;
    }
  });
}