createShader method

  1. @override
Shader createShader(
  1. Rect rect, {
  2. TextDirection? textDirection,
})
override

Creates a Shader for this gradient to fill the given rect.

If the gradient's configuration is text-direction-dependent, for example it uses AlignmentDirectional objects instead of Alignment objects, then the textDirection argument must not be null.

The shader's transform will be resolved from the transform of this gradient.

Implementation

@override
Shader createShader(Rect rect, {TextDirection? textDirection}) {
  if (borderEdge != null) {
    rect = Rect.fromLTRB(rect.left + borderEdge!.left, rect.top + borderEdge!.top, rect.right - borderEdge!.right,
        rect.bottom - borderEdge!.bottom);
  }
  Offset centerOffset = center.resolve(textDirection).withinRect(rect);
  // calculate the longest distance from center to cornor
  double centerX, centerY;
  if (centerOffset.dx < rect.left) {
    centerX = rect.left;
  } else if (centerOffset.dx < rect.right) {
    centerX = centerOffset.dx;
  } else {
    centerX = rect.right;
  }

  if (centerOffset.dy < rect.top) {
    centerY = rect.top;
  } else if (centerOffset.dy < rect.bottom) {
    centerY = centerOffset.dy;
  } else {
    centerY = rect.bottom;
  }
  double width = math.max((centerX - rect.left), (rect.right - centerX));
  double height = math.max((centerY - rect.top), (rect.bottom - centerY));
  double radiusValue = radius * 2 * math.sqrt(width * width + height * height);

  return ui.Gradient.radial(
    centerOffset,
    radiusValue,
    colors,
    _impliedStops(),
    tileMode,
    _resolveTransform(rect, textDirection),
    focal == null ? null : focal!.resolve(textDirection).withinRect(rect),
    focalRadius * rect.shortestSide,
  );
}