showSnackbar static method

void showSnackbar(
  1. BuildContext context, {
  2. String? title,
  3. String? body,
  4. String? background,
  5. String? textColor,
  6. String? url,
  7. int duration = 3000,
  8. int? scale,
})

Implementation

static void showSnackbar(
  BuildContext context, {
  String? title,
  String? body,
  String? background,
  String? textColor,
  String? url,
  int duration = 3000,
  int? scale,
}) {
  final overlay = Overlay.of(context);

  // Prevent multiple snackbars from appearing at the same time
  if (_overlayEntry != null) return;

  _overlayEntry = OverlayEntry(
    builder: (context) => Positioned(
      bottom: 40,
      left: 10,
      right: 10,
      child: Material(
        color: Colors.transparent,
        child: Container(
          // height: 80,
          padding: EdgeInsets.all(padding),
          decoration: BoxDecoration(
            color: background != null
                ? hexToColor(background)
                : Colors.black.withOpacity(0.8),
            borderRadius: BorderRadius.circular(8),
          ),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              body != null
                  ? Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        title != null
                            ? ConstrainedBox(
                                constraints: const BoxConstraints(
                                  maxWidth: 340,
                                ),
                                child: Text(
                                  title,
                                  maxLines: 1,
                                  overflow: TextOverflow.ellipsis,
                                  style: TextStyle(
                                    color: textColor != null
                                        ? hexToColor(textColor)
                                        : Colors.white,
                                    fontSize: 18,
                                    fontWeight: FontWeight.bold,
                                  ),
                                ),
                              )
                            : const SizedBox(),
                        body.toString().startsWith("<")
                            ? SizedBox(
                                height: 50,
                                width: 340,
                                child: WebViewWidget(
                                  controller: controller!,
                                ),
                              )
                            : ConstrainedBox(
                                constraints:
                                    const BoxConstraints(maxWidth: 300),
                                child: Text(
                                  maxLines: 2,
                                  overflow: TextOverflow.ellipsis,
                                  body,
                                  style: TextStyle(
                                    color: textColor != null
                                        ? hexToColor(textColor)
                                        : Colors.white,
                                  ),
                                ),
                              ),
                      ],
                    )
                  : SizedBox(
                      height: 80,
                      width: 340,
                      child: WebViewWidget(
                        controller: controller!,
                      ),
                    ),
              const Spacer(),
              IconButton(
                onPressed: () {
                  _overlayEntry?.remove();
                  _overlayEntry = null;

                  controller!.clearCache();
                },
                icon: const Icon(
                  Icons.close,
                  color: Colors.white,
                ),
              ),
            ],
          ),
        ),
      ),
    ),
  );

  // Insert the overlay entry after the frame is built
  WidgetsBinding.instance.addPostFrameCallback((_) {
    overlay.insert(_overlayEntry!);
  });
  // Automatically remove the snackbar after a delay
  Future.delayed(Duration(milliseconds: duration), () {
    _overlayEntry?.remove();
    _overlayEntry = null;
    controller!.clearCache();
  });

  if (url != null) {
    controller!.loadRequest(Uri.parse(url));
  }
  if (body.toString().startsWith("<")) {
    controller!.loadHtmlString(body.toString());
    adjustWebviewZoom(scale: scale ?? 2);
  }
}