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);
  }
  double? angle;
  if (_angle != null) {
    angle = _angle;
  } else {
    Alignment point = end as Alignment;
    angle = math.atan2(point.x * rect.height, -point.y * rect.width) - math.pi * 2;
  }
  // https://drafts.csswg.org/css-images-3/#linear-gradient-syntax
  double sin = math.sin(angle!);
  double cos = math.cos(angle);

  double width = rect.width;
  double height = rect.height;
  // If width/height is null, x/y can be infinite.
  if (width == 0 || height == 0) {
    return ui.Gradient.linear(Offset.zero, Offset.zero, colors);
  }

  double length = (sin * width).abs() + (cos * height).abs();
  double x = sin * length / width;
  double y = cos * length / height;
  final double halfWidth = rect.width / 2.0;
  final double halfHeight = rect.height / 2.0;
  Offset beginOffset = Offset(
    rect.left + halfWidth + -x * halfWidth,
    rect.top + halfHeight + y * halfHeight,
  );
  Offset endOffset = Offset(
    rect.left + halfWidth + x * halfWidth,
    rect.top + halfHeight - y * halfHeight,
  );
  return ui.Gradient.linear(
      beginOffset, endOffset, colors, _impliedStops(), tileMode, _resolveTransform(rect, textDirection));
}