nudgeFloater function

void nudgeFloater({
  1. required BuildContext context,
  2. required Root root,
  3. required Map<String, dynamic> assets,
  4. required String assetUrl,
})

Implementation

void nudgeFloater({
  required BuildContext context,
  required Root root,
  required Map<String, dynamic> assets,
  required String assetUrl,
}) {
  OverlayEntry? overlayEntry;
  GlobalKey<_FloaterState> floaterKey = GlobalKey<_FloaterState>();

  double initialX = root.props!.x!.toDouble();
  double initialY = root.props!.y!.toDouble();
  double initialWidth = root.props!.constWidth!.toDouble();
  double initialHeight = root.props!.constHeight!.toDouble();

  Size screenSize = MediaQuery.of(context).size;
  EdgeInsets padding = MediaQuery.of(context).padding;
  double widthScaleFactor = screenSize.width / 720;
  double heightScaleFactor = screenSize.height / 1440;

  double scrHeight = screenSize.height - padding.top;

  initialWidth *= widthScaleFactor;
  initialHeight *= heightScaleFactor;
  initialX *= widthScaleFactor;
  initialY *= heightScaleFactor;

  initialX = initialX - initialWidth / 2;
  initialY = initialY - initialHeight / 2;

  Offset floaterPosition = Offset(initialX, initialY);

  ValueNotifier<Offset> floaterPositionNotifier =
      ValueNotifier(floaterPosition);

  ValueNotifier<bool> floaterStateNotifier = ValueNotifier(false);

  CentralDataRepository.updateInstance(
    NudgeModalsUi.taskId,
    callBack: (rootId, widgetId, widgetType) async {
      overlayEntry?.remove();

      Nudge.getInstance().submit(
        taskId: NudgeModalsUi.taskId,
        response: [
          {
            "root_id": root.id,
            "widget_id": widgetId,
            "wt": widgetType,
            "answers": [],
            "action": "CTA_CLICKED",
          }
        ],
        onSuccessCallback: (response) {},
        onErrorCallback: (error, stackTrace) {},
      );
    },
    closeCallback: ({
      rootId,
      widgetId,
      widgetType,
      context,
    }) async {
      Nudge.getInstance().submit(
        taskId: NudgeModalsUi.taskId,
        response: [
          {
            "root_id": root.id,
            // "widget_id": root.widgets[0].id,
            "answers": [],
            "action": "DISMISSED",
          }
        ],
        onSuccessCallback: (response) {},
        onErrorCallback: (error, stackTrace) {},
      );
      overlayEntry?.remove();
    },
  );

  overlayEntry = OverlayEntry(
    builder: (context) {
      return ValueListenableBuilder(
        valueListenable: floaterPositionNotifier,
        builder: (context, value, child) {
          return Positioned(
            top: value.dy,
            left: value.dx,
            child: ValueListenableBuilder(
                valueListenable: floaterStateNotifier,
                builder: (context, isFullPage, child) {
                  return Draggable(
                    maxSimultaneousDrags: isFullPage ? 0 : 1,
                    feedback: Floater(
                      key: floaterKey,
                      initialX: initialX,
                      initialY: initialY,
                      initialWidth: initialWidth,
                      initialHeight: initialHeight,
                      assetUrl: assetUrl,
                      assets: assets,
                      asset: root.props!.asset!,
                      objectFit: root.props!.objectFit!,
                      taskId: NudgeModalsUi.taskId,
                      rootId: root.id!,
                      screenHeight: scrHeight,
                      screenWidth: screenSize.width,
                      paddingTop: padding.top,
                      widgets: root.widgets,
                      closeCallback: () {
                        overlayEntry?.remove();

                        Nudge.getInstance().submit(
                          taskId: NudgeModalsUi.taskId,
                          response: [
                            {
                              "root_id": root.id,
                              // "widget_id": root.widgets[0].id,
                              "answers": [],
                              "action": "DISMISSED",
                            }
                          ],
                          onSuccessCallback: (response) {},
                          onErrorCallback: (error, stackTrace) {},
                        );
                      },
                      updateFloaterPosition: floaterPositionNotifier,
                      isFullPageNotifier: floaterStateNotifier,
                    ),
                    childWhenDragging: Container(),
                    onDragEnd: (details) {
                      floaterPositionNotifier.value = details.offset;
                    },
                    child: Floater(
                      key: floaterKey,
                      initialX: initialX,
                      initialY: initialY,
                      initialWidth: initialWidth,
                      initialHeight: initialHeight,
                      assetUrl: assetUrl,
                      assets: assets,
                      asset: root.props!.asset!,
                      objectFit: root.props!.objectFit!,
                      taskId: NudgeModalsUi.taskId,
                      rootId: root.id!,
                      screenHeight: scrHeight,
                      screenWidth: screenSize.width,
                      paddingTop: padding.top,
                      widgets: root.widgets,
                      closeCallback: () {
                        overlayEntry?.remove();

                        Nudge.getInstance().submit(
                          taskId: NudgeModalsUi.taskId,
                          response: [
                            {
                              "root_id": root.id,
                              // "widget_id": root.widgets[0].id,
                              "answers": [],
                              "action": "DISMISSED",
                            }
                          ],
                          onSuccessCallback: (response) {},
                          onErrorCallback: (error, stackTrace) {},
                        );
                      },
                      updateFloaterPosition: floaterPositionNotifier,
                      isFullPageNotifier: floaterStateNotifier,
                    ),
                  );
                }),
          );
        },
      );
    },
  );

  NudgeProviderState.navigatorKey.currentState!.overlay!.insert(overlayEntry);
}