showToast method

void showToast({
  1. required BuildContext context,
  2. String? icon,
  3. String? title,
  4. Duration? duration,
  5. bool? mask,
})

Implementation

void showToast({
  required BuildContext context,
  String? icon,
  String? title,
  Duration? duration,
  bool? mask,
}) {
  if (activeHUD != null) {
    try {
      activeHUD!.remove();
      // ignore: empty_catches
    } catch (e) {}
  }
  final overlayEntry = OverlayEntry(builder: (context) {
    final content = Center(
      child: Container(
        height: 128,
        width: 128,
        decoration: BoxDecoration(
          color: Colors.black.withOpacity(0.6),
          borderRadius: BorderRadius.circular(12),
        ),
        child: Padding(
          padding: const EdgeInsets.only(
            left: 32.0,
            right: 32.0,
            top: 16.0,
            bottom: 16.0,
          ),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              icon != null
                  ? (() {
                      switch (icon) {
                        case 'ToastIcon.success':
                          return const Icon(
                            Icons.check_circle,
                            color: Colors.white,
                            size: 36,
                          );
                        case 'ToastIcon.error':
                          return const Icon(
                            Icons.error,
                            color: Colors.white,
                            size: 36,
                          );
                        case 'ToastIcon.loading':
                          return Transform.scale(
                            scale: 0.75,
                            child: const CircularProgressIndicator(
                              valueColor: AlwaysStoppedAnimation(
                                Colors.white,
                              ),
                            ),
                          );
                        default:
                          return const SizedBox();
                      }
                    })()
                  : const SizedBox(),
              title != null
                  ? Padding(
                      padding: const EdgeInsets.only(top: 12.0),
                      child: Text(title,
                          style: const TextStyle(
                            fontSize: 14,
                            color: Colors.white,
                            fontWeight: FontWeight.w500,
                            decoration: TextDecoration.none,
                          )),
                    )
                  : const SizedBox(),
            ],
          ),
        ),
      ),
    );
    if (mask == true) {
      return Container(
        color: Colors.transparent,
        child: content,
      );
    } else {
      return content;
    }
  });
  activeHUD = overlayEntry;
  Overlay.of(context)?.insert(overlayEntry);
  if (duration != null) {
    Future.delayed(duration).then((value) {
      try {
        overlayEntry.remove();
        if (activeHUD == overlayEntry) {
          activeHUD = null;
        }
        // ignore: empty_catches
      } catch (e) {}
    });
  }
}