showFloatingWidget static method

void showFloatingWidget(
  1. BuildContext context, {
  2. String? title,
  3. String? body,
  4. String? background,
  5. String? textColor,
  6. String? url,
  7. String? position,
  8. int? scale,
  9. bool isDraggable = false,
  10. Widget? customWidget,
  11. void onTap()?,
  12. bool? isVisible = false,
})

Implementation

static void showFloatingWidget(
  BuildContext context, {
  String? title,
  String? body,
  String? background,
  String? textColor,
  String? url,
  String? position,
  int? scale,
  bool isDraggable = false,
  Widget? customWidget,
  void Function()? onTap,
  bool? isVisible = false,
}) {
  // Remove existing widget if any
  _entry?.remove();
  _entry = null;

  if (!(isVisible ?? false)) return;

  final overlay = Overlay.of(context);
  final screenSize = MediaQuery.of(context).size;
  const double margin = 30;

  Offset currentOffset;

  switch (position?.toLowerCase()) {
    case "topleft":
      currentOffset = const Offset(margin, margin);
      break;
    case "topcenter":
    case "top":
      currentOffset = Offset((screenSize.width - margin * 6) / 2, margin);
      break;
    case "topright":
      currentOffset = Offset(screenSize.width - margin * 6, margin);
      break;
    case "bottomleft":
      currentOffset = Offset(margin, screenSize.height - margin * 6);
      break;
    case "bottomcenter":
    case "bottom":
      currentOffset = Offset((screenSize.width - margin * 6) / 2,
          screenSize.height - margin * 6);
      break;
    case "bottomright":
      currentOffset = Offset(
          screenSize.width - margin * 6, screenSize.height - margin * 6);
      break;
    case "center":
      currentOffset = Offset((screenSize.width - margin * 6) / 2,
          (screenSize.height - margin * 5) / 2);
      break;
    case "leftcenter":
    case "left":
      currentOffset = Offset(margin, (screenSize.height - margin * 5) / 2);
      break;
    case "rightcenter":
    case "right":
      currentOffset = Offset(screenSize.width - margin * 6,
          (screenSize.height - margin * 5) / 2);
      break;
    default:
      currentOffset = Offset((screenSize.width - margin * 6) / 2,
          screenSize.height - margin * 6);
  }

  _entry = OverlayEntry(
    builder: (context) {
      return StatefulBuilder(
        builder: (context, setState) {
          return Positioned(
            top: currentOffset.dy,
            left: currentOffset.dx,
            child: SafeArea(
              child: Material(
                elevation: 0,
                color: Colors.transparent,
                child: GestureDetector(
                  onTap: onTap,
                  onPanUpdate: (details) {
                    if (isDraggable) {
                      setState(() {
                        currentOffset += details.delta;
                      });
                    }
                  },
                  child: customWidget ??
                      Container(
                        width: screenSize.width * 0.4,
                        padding: const EdgeInsets.all(10),
                        decoration: BoxDecoration(
                          color: background != null
                              ? hexToColor(background)
                              : isDarkMode
                                  ? Colors.black
                                  : Colors.white,
                          borderRadius: BorderRadius.circular(8),
                          boxShadow: const [
                            BoxShadow(
                              color: Colors.black26,
                              blurRadius: 10,
                              offset: Offset(2, 2),
                            ),
                          ],
                        ),
                        child: Column(
                          mainAxisSize: MainAxisSize.min,
                          children: [
                            if (title != null)
                              Text(
                                title,
                                style: TextStyle(
                                  fontSize: 18,
                                  fontWeight: FontWeight.bold,
                                  color: textColor != null
                                      ? hexToColor(textColor)
                                      : null,
                                ),
                              ),
                            const SizedBox(height: 8),
                            if (controller != null)
                              body != null && body.startsWith("<")
                                  ? SizedBox(
                                      height: 200,
                                      width: 200,
                                      child: WebViewWidget(
                                          controller: controller!),
                                    )
                                  : body != null
                                      ? Text(
                                          body,
                                          style: TextStyle(
                                            color: textColor != null
                                                ? hexToColor(textColor)
                                                : null,
                                          ),
                                        )
                                      : SizedBox(
                                          height: 200,
                                          width: 200,
                                          child: WebViewWidget(
                                              controller: controller!),
                                        ),
                          ],
                        ),
                      ),
                ),
              ),
            ),
          );
        },
      );
    },
  );

  overlay.insert(_entry!);

  if (controller != null) {
    if (body != null && body.startsWith("<")) {
      controller!.loadHtmlString(htmlBodyStart + body + htmlBodyEnd);
      adjustWebviewZoom(scale: scale ?? 4);
    } else if (url != null) {
      controller!.loadRequest(Uri.parse(url));
    }
  }
}