showFloatingWidget static method

void showFloatingWidget(
  1. BuildContext context, {
  2. String? title,
  3. String? body,
  4. String? url,
  5. String? position,
  6. int? scale,
  7. bool isDraggable = false,
})

Implementation

static void showFloatingWidget(
  BuildContext context, {
  String? title,
  String? body,
  String? url,
  String? position,
  int? scale,
  bool isDraggable = false,
}) {
  // Use an OverlayEntry to display the pulse animation
  final overlay = Overlay.of(context);
  OverlayEntry? entry;
  double margin = 30;

  final screenSize = MediaQuery.of(context).size;
  Offset currentOffset = Offset(0, 0);
  switch (position.toString().toLowerCase()) {
    case "topleft":
      currentOffset = Offset(margin, margin);
      break;
    case "topcenter":
    case "top":
      currentOffset = Offset(
        (screenSize.width - margin * 6) / 2,
        margin,
      );
      break;
    case "topright":
      currentOffset = Offset(
        (screenSize.width + margin) / 2,
        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) / 2,
        screenSize.height - margin * 6,
      );
      break;
    case "center":
      currentOffset = Offset(
        (screenSize.width - margin * 6) / 2,
        (screenSize.height - margin * 5) / 2,
      ); // Center
      break;
    case "leftcenter":
    case "left":
      currentOffset =
          Offset(margin, (screenSize.height - margin * 5) / 2); // Left Center
      break;
    case "rightcenter":
    case "right":
      currentOffset = Offset(
        (screenSize.width + margin) / 2,
        (screenSize.height - margin * 5) / 2,
      ); // Right Center
      break;
  }

  entry = OverlayEntry(
    builder: (context) {
      return StatefulBuilder(
        builder: (context, setState) {
          // final screenSize = MediaQuery.of(context).size;
          // Offset currentOffset = Offset(0, 0);
          return Positioned(
            top: currentOffset.dy,
            left: currentOffset.dx,
            child: SafeArea(
              child: Material(
                elevation: 4,
                color: Colors.transparent,
                child: GestureDetector(
                  onPanUpdate: (details) {
                    if (isDraggable) {
                      setState(() {
                        // Update position as the user drags
                        currentOffset += details.delta;
                        //entry?.markNeedsBuild();
                      });
                    }
                  },
                  child: Container(
                    width: MediaQuery.of(context).size.width * 0.40,
                    padding: EdgeInsets.all(10),
                    decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.circular(8),
                      boxShadow: [
                        BoxShadow(
                          color: Colors.black26,
                          blurRadius: 10,
                          offset: Offset(2, 2),
                        ),
                      ],
                    ),
                    child: Column(
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        title != null
                            ? Text(
                                title,
                                style: TextStyle(
                                  fontSize: 18,
                                  fontWeight: FontWeight.bold,
                                ),
                              )
                            : SizedBox(),
                        body != null
                            ? body.toString().startsWith("<")
                                ? Container(
                                    height: 200,
                                    width: 200,
                                    child: WebViewWidget(
                                        controller: controller!),
                                  )
                                : Text(body)
                            : Container(
                                height: 200,
                                width: 200,
                                child: WebViewWidget(controller: controller!),
                              ),
                      ],
                    ),
                  ),
                ),
              ),
            ),
          );
        },
      );
    },
  );

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

  overlay.insert(entry);
}