showSnackbar static method

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

Implementation

static void showSnackbar(
  BuildContext context, {
  String? title,
  String? body,
  String? url,
  int duration = 3000,
  int? scale,
}) {
  if (context == null) {
    print("No Overlay widget found in the current context.");
    return;
  }

  final overlay = Overlay.of(context);
  if (overlay == null) {
    print("No Overlay available in current context.");
    return;
  }

  // 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: Colors.black.withOpacity(0.8),
            borderRadius: BorderRadius.circular(8),
          ),
          child: Row(
            children: [
              body != null
                  ? Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        title != null
                            ? Text(
                                title,
                                style: TextStyle(
                                  color: Colors.white,
                                  fontSize: 18,
                                  fontWeight: FontWeight.bold,
                                ),
                              )
                            : SizedBox(),
                        body.toString().startsWith("<")
                            ? Container(
                                height: 20,
                                width: 340,
                                child: WebViewWidget(
                                  controller: controller!,
                                ),
                              )
                            : Text(
                                body,
                                style: TextStyle(color: Colors.white),
                              ),
                      ],
                    )
                  : Container(
                      height: 80,
                      width: 340,
                      child: WebViewWidget(
                        controller: controller!,
                      ),
                    ),
              Spacer(),
              GestureDetector(
                onTap: () {
                  _overlayEntry?.remove();
                  _overlayEntry = null;

                  controller!.clearCache();
                },
                child: 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);
  }
}