getHorizontalShift method

double getHorizontalShift({
  1. required RenderBox parentBox,
  2. required Offset center,
  3. required TextPainter labelPainter,
  4. required double textScaleFactor,
  5. required Size sizeWithOverflow,
  6. required double scale,
})

Implementation

double getHorizontalShift({
  required RenderBox parentBox,
  required Offset center,
  required TextPainter labelPainter,
  required double textScaleFactor,
  required Size sizeWithOverflow,
  required double scale,
}) {
  assert(!sizeWithOverflow.isEmpty);

  const double edgePadding = 8.0;
  final double rectangleWidth =
      _upperRectangleWidth(labelPainter, scale, textScaleFactor);
  // 获取全局坐标
  final Offset globalCenter = parentBox.localToGlobal(center);

  // The rectangle must be shifted towards the center so that it minimizes the
  // chance of it rendering outside the bounds of the render box. If the shift
  // is negative, then the lobe is shifted from right to left, and if it is
  // positive, then the lobe is shifted from left to right.
  final double overflowLeft =
      math.max(0, rectangleWidth / 2 - globalCenter.dx + edgePadding);
  final double overflowRight = math.max(
      0,
      rectangleWidth / 2 -
          (sizeWithOverflow.width - globalCenter.dx - edgePadding));

  if (rectangleWidth < sizeWithOverflow.width) {
    return overflowLeft - overflowRight;
  } else if (overflowLeft - overflowRight > 0) {
    return overflowLeft - (edgePadding * textScaleFactor);
  } else {
    return -overflowRight + (edgePadding * textScaleFactor);
  }
}