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}) {
  final startPoint = begin.resolve(textDirection).withinRect(rect);
  final endPoint = end.resolve(textDirection).withinRect(rect);
  final span = ((startPoint.dx - endPoint.dx).squared() +
          (startPoint.dy - endPoint.dy).squared())
      .sqrt();
  final size = (span * devicePixelRatio * density).ceil();

  if (size < colors.length) {
    return super.createShader(rect, textDirection: textDirection);
  }

  return ui.Gradient.linear(
    startPoint,
    endPoint,
    colors.augment(size,
        colorSpace: colorSpace, stops: stops, invert: invert),
    buildStops(size),
    tileMode,
    resolveTransform(rect, textDirection),
  );
}