draw method

  1. @override
void draw(
  1. Canvas canvas,
  2. Rect bounds
)
override

Draws the contents or children of this Drawable to the canvas, using the parentPaint to optionally override the child's paint.

The bounds specify the area to draw in.

Implementation

@override
void draw(Canvas canvas, Rect bounds) {
  if (!hasDrawableContent) {
    return;
  }

  path.fillType = style.pathFillType ?? PathFillType.nonZero;
  // if we have multiple clips to apply, need to wrap this in a loop.
  final Function innerDraw = () {
    if (transform != null) {
      canvas.save();
      canvas.transform(transform!);
    }
    if (style.blendMode != null) {
      canvas.saveLayer(null, Paint()..blendMode = style.blendMode!);
    }
    if (style.mask != null) {
      canvas.saveLayer(null, Paint());
    }
    if (style.fill?.color != null) {
      assert(style.fill!.style == PaintingStyle.fill);
      canvas.drawPath(path, style.fill!.toFlutterPaint());
    }

    if (style.stroke?.color != null &&
        (style.stroke!.strokeWidth == null ||
            (style.stroke!.strokeWidth != null &&
                style.stroke!.strokeWidth! > 0))) {
      assert(style.stroke!.style == PaintingStyle.stroke);
      if (style.dashArray != null &&
          !identical(style.dashArray, DrawableStyle.emptyDashArray)) {
        canvas.drawPath(
          dashPath(
            path,
            dashArray: style.dashArray!,
            dashOffset: style.dashOffset,
          ),
          style.stroke!.toFlutterPaint(),
        );
      } else {
        canvas.drawPath(path, style.stroke!.toFlutterPaint());
      }
    }

    if (style.mask != null) {
      canvas.saveLayer(null, _grayscaleDstInPaint);
      style.mask!.draw(canvas, bounds);
      canvas.restore();
      canvas.restore();
    }

    if (style.blendMode != null) {
      canvas.restore();
    }
    if (transform != null) {
      canvas.restore();
    }
  };

  if (style.clipPath?.isNotEmpty == true) {
    for (Path clip in style.clipPath!) {
      canvas.save();
      canvas.clipPath(clip);
      innerDraw();
      canvas.restore();
    }
  } else {
    innerDraw();
  }
}