createHighlightOverlayContent method

OverlayContent createHighlightOverlayContent(
  1. BuildContext context,
  2. WidgetHighlightTutorialStep tutorialStep
)

Implementation

OverlayContent createHighlightOverlayContent(BuildContext context, WidgetHighlightTutorialStep tutorialStep) {
  GlobalKey? widgetKey = tutorialStep.loadFromRepository?.call();
  if (widgetKey == null || widgetKey.currentContext == null) {
    throw Exception("Widget key not found for highlight overlay");
  }

  RenderBox widgetRenderBox = widgetKey.currentContext!.findRenderObject() as RenderBox;
  Offset widgetPosition = widgetRenderBox.localToGlobal(Offset.zero);
  double widgetCenterX = widgetPosition.dx + widgetRenderBox.size.width / 2;

  Size tutorialTextSize = _calculateTextSize(context, tutorialStep.tutorialText);

  double clippingLeftPosition = widgetPosition.dx - overlayOffset.dx;
  double clippingTopPosition = widgetPosition.dy - widgetRenderBox.size.height / 2 - overlayOffset.dy;
  double clippingWidth = widgetRenderBox.size.width + overlayOffset.dx * 2;
  double clippingHeight = widgetRenderBox.size.height * 1.5 + overlayOffset.dy * 2;

  return OverlayContent(
    exclusionRect: Rect.fromLTWH(clippingLeftPosition, clippingTopPosition, clippingWidth, clippingHeight),
    textPosition:
        Offset(widgetCenterX - tutorialTextSize.width / 1.75, clippingTopPosition - tutorialTextSize.height),
    buttonPosition: Offset(widgetCenterX - buttonWidth / 2, widgetPosition.dy + widgetRenderBox.size.height),
    tutorialText: tutorialStep.tutorialText,
  );
}