buildClip method

  1. @override
RRect buildClip()

Builds the clip to apply to the child.

This method is called lazily during painting or hit testing. The result is cached until the next time the render object is laid out or markNeedsClip is called.

Implementation

@override
RRect buildClip() {
  final Rect maxPaintRect = getMaxPaintRect();

  RRect newClip =
      clipper?.getClip(maxPaintRect.size).shift(maxPaintRect.topLeft) ??
      borderRadius.resolve(textDirection).toRRect(maxPaintRect);

  if (clipOverlap != ClipOverlapBehavior.none) {
    final double insideClipExtent = switch ((constraints.axis, clipOverlap)) {
      (Axis.horizontal, ClipOverlapBehavior.preserveShape) => newClip.middleRect.width,
      (Axis.vertical, ClipOverlapBehavior.preserveShape) => newClip.middleRect.height,
      (Axis.horizontal, _) => newClip.width,
      (Axis.vertical, _) => newClip.height,
    };
    final double clipOrigin = getClipOriginForOverlap(insideClipExtent);

    RRect copyNewClipWith({double? left, double? top, double? right, double? bottom}) =>
        RRect.fromLTRBAndCorners(
          left ?? newClip.left,
          top ?? newClip.top,
          right ?? newClip.right,
          bottom ?? newClip.bottom,
          topLeft: newClip.tlRadius,
          topRight: newClip.trRadius,
          bottomLeft: newClip.blRadius,
          bottomRight: newClip.brRadius,
        );

    newClip = switch (applyGrowthDirectionToAxisDirection(
      constraints.axisDirection,
      constraints.growthDirection,
    )) {
      AxisDirection.down => copyNewClipWith(top: math.max(newClip.top, clipOrigin)),
      AxisDirection.up => copyNewClipWith(
        bottom: math.min(newClip.bottom, geometry!.paintExtent - clipOrigin),
      ),
      AxisDirection.right => copyNewClipWith(left: math.max(newClip.left, clipOrigin)),
      AxisDirection.left => copyNewClipWith(
        right: math.min(newClip.right, geometry!.paintExtent - clipOrigin),
      ),
    };
  }

  return newClip;
}