paint method

  1. @override
void paint(
  1. Canvas canvas,
  2. Size size
)
override

Called whenever the object needs to paint. The given Canvas has its coordinate space configured such that the origin is at the top left of the box. The area of the box is the size of the size argument.

Paint operations should remain inside the given area. Graphical operations outside the bounds may be silently ignored, clipped, or not clipped. It may sometimes be difficult to guarantee that a certain operation is inside the bounds (e.g., drawing a rectangle whose size is determined by user inputs). In that case, consider calling Canvas.clipRect at the beginning of paint so everything that follows will be guaranteed to only draw within the clipped area.

Implementations should be wary of correctly pairing any calls to Canvas.save/Canvas.saveLayer and Canvas.restore, otherwise all subsequent painting on this canvas may be affected, with potentially hilarious but confusing results.

To paint text on a Canvas, use a TextPainter.

To paint an image on a Canvas:

  1. Obtain an ImageStream, for example by calling ImageProvider.resolve on an AssetImage or NetworkImage object.

  2. Whenever the ImageStream's underlying ImageInfo object changes (see ImageStream.addListener), create a new instance of your custom paint delegate, giving it the new ImageInfo object.

  3. In your delegate's paint method, call the Canvas.drawImage, Canvas.drawImageRect, or Canvas.drawImageNine methods to paint the ImageInfo.image object, applying the ImageInfo.scale value to obtain the correct rendering size.

Implementation

@override
void paint(Canvas canvas, Size size) {
  double _w = w ?? size.width;
  double _h = h ?? size.height;
  double _triangleLength = triangleLength ?? 0;

  assert(() {
    CcBubblePainter.log('paint point offset: $trianglePointOffset');
    CcBubblePainter.log('paint size.width: ${size.width}, size.height: ${size.height}, w: $w, h: $h, _w:$_w, _h:$_h');
    return true;
  }());

  if (isTriangleOccupiedSpace) {
    // isTriangleOccupiedSpace = true, we need to decrease the length we setted in margin ~~~
    if (w == null) {
      if (triangleDirection == CcBubbleArrowDirection.left || triangleDirection == CcBubbleArrowDirection.right) {
        _w = size.width - (trianglePointOffset?.dx ?? 0).abs();
      }
    }
    if (h == null) {
      if (triangleDirection == CcBubbleArrowDirection.top || triangleDirection == CcBubbleArrowDirection.bottom) {
        _h = size.height - (trianglePointOffset?.dy ?? 0).abs();
      }
    }
  }

  assert(() {
    CcBubblePainter.log('paint size.width: ${size.width}, size.height: ${size.height}, w: $w, h: $h, _w:$_w, _h:$_h');
    return true;
  }());

  double _radius = radius ?? 0;

  Path pointsPath = Path();

  double leftCircleCenterX = x + _radius;
  double rightCircleCenterX = x + _w - _radius;
  double topCircleCenterY = y + _radius;
  double bottomCircleCenterY = y + _h - _radius;

  // make triangle is in center or by custom
  double _triangleTranslation = 0;
  if (triangleTranslation == null) {
    if (triangleDirection == CcBubbleArrowDirection.left || triangleDirection == CcBubbleArrowDirection.right) {
      _triangleTranslation = _h / 2 - _triangleLength / 2 - _radius;
    } else if (triangleDirection == CcBubbleArrowDirection.top || triangleDirection == CcBubbleArrowDirection.bottom) {
      _triangleTranslation = _w / 2 - _triangleLength / 2 - _radius;
    }
  } else {
    _triangleTranslation = triangleTranslation!;
  }

  void _drawTriangle(_CcBubblePaintingBorder whichSide, Offset startPoint, Offset overPoint) {
    _applyTriangleArrow(
        pointsPath, whichSide, triangleDirection, startPoint, overPoint, _triangleTranslation, trianglePointOffset, _triangleLength);
  }

  // start from original point
  pointsPath.moveTo(leftCircleCenterX, y);

  Offset fromPoint;
  Offset toPoint;
  Rect rectRadius;

  // top line
  fromPoint = Offset(leftCircleCenterX, y);
  toPoint = Offset(rightCircleCenterX, y);

  _drawTriangle(_CcBubblePaintingBorder.top, fromPoint, toPoint);
  pointsPath.lineTo(toPoint.dx, toPoint.dy);

  fromPoint = toPoint;
  toPoint = Offset(x + _w, topCircleCenterY);

  if (triangleDirection == CcBubbleArrowDirection.topRight) {
    // top right triangle
    _drawTriangle(_CcBubblePaintingBorder.topRight, fromPoint, toPoint);
    pointsPath.lineTo(toPoint.dx, toPoint.dy);
  } else if (radius != 0) {
    // top right arc
    rectRadius = Rect.fromCircle(center: Offset(rightCircleCenterX, topCircleCenterY), radius: _radius);
    pointsPath.arcTo(rectRadius, math.pi * -0.5, math.pi * 0.5, false);
  }

  // right line
  fromPoint = toPoint;
  toPoint = Offset(x + _w, bottomCircleCenterY);

  _drawTriangle(_CcBubblePaintingBorder.right, fromPoint, toPoint);
  pointsPath.lineTo(toPoint.dx, toPoint.dy);

  fromPoint = toPoint;
  toPoint = Offset(rightCircleCenterX, y + _h);

  if (triangleDirection == CcBubbleArrowDirection.bottomRight) {
    // bottom right triangle
    _drawTriangle(_CcBubblePaintingBorder.bottomRight, fromPoint, toPoint);
    pointsPath.lineTo(toPoint.dx, toPoint.dy);
  } else if (radius != 0) {
    // bottom right arc
    rectRadius = Rect.fromCircle(center: Offset(rightCircleCenterX, bottomCircleCenterY), radius: _radius);
    pointsPath.arcTo(rectRadius, math.pi * 0, math.pi * 0.5, false);
  }

  // bottom line
  fromPoint = toPoint;
  toPoint = Offset(leftCircleCenterX, y + _h);

  _drawTriangle(_CcBubblePaintingBorder.bottom, fromPoint, toPoint);
  pointsPath.lineTo(toPoint.dx, toPoint.dy);

  fromPoint = toPoint;
  toPoint = Offset(x, bottomCircleCenterY);

  if (triangleDirection == CcBubbleArrowDirection.bottomLeft) {
    // bottom left triangle
    _drawTriangle(_CcBubblePaintingBorder.bottomLeft, fromPoint, toPoint);
    pointsPath.lineTo(toPoint.dx, toPoint.dy);
  } else if (radius != 0) {
    // bottom left arc
    rectRadius = Rect.fromCircle(center: Offset(leftCircleCenterX, bottomCircleCenterY), radius: _radius);
    pointsPath.arcTo(rectRadius, math.pi * 0.5, math.pi * 0.5, false);
  }

  // left line
  fromPoint = toPoint;
  toPoint = Offset(x, topCircleCenterY);

  _drawTriangle(_CcBubblePaintingBorder.left, fromPoint, toPoint);
  pointsPath.lineTo(toPoint.dx, toPoint.dy);

  fromPoint = toPoint;
  toPoint = Offset(leftCircleCenterX, y);

  if (triangleDirection == CcBubbleArrowDirection.topLeft) {
    // top left triangle
    _drawTriangle(_CcBubblePaintingBorder.topLeft, fromPoint, toPoint);
    pointsPath.lineTo(toPoint.dx, toPoint.dy);
  } else if (radius != 0) {
    // top left arc
    rectRadius = Rect.fromCircle(center: Offset(leftCircleCenterX, topCircleCenterY), radius: _radius);
    pointsPath.arcTo(rectRadius, math.pi * -1, math.pi * 0.5, false);
  }

  // end to original point
  pointsPath.moveTo(toPoint.dx, toPoint.dy);
  pointsPath.close();

  // draw
  // canvas.drawShadow(pointsPath, Colors.black, 10.0, true);
  // canvas.drawRRect(
  //   const BorderRadius.all(Radius.circular(10)).resolve(TextDirection.ltr).toRRect(const Rect.fromLTRB(30, 30, 50, 50)),
  //   const BoxShadow(color: Colors.black, offset: Offset.zero, blurRadius: 10, spreadRadius: 20,).toPaint(),
  // );

  // 1. first draw shadow
  if (shadowColor != null) {
    BoxShadow boxShadow = BoxShadow(
      color: shadowColor ?? Colors.grey,
      offset: shadowOffset ?? Offset.zero,
      blurRadius: shadowBlurRadius ?? 10.0,
      spreadRadius: shadowSpreadRadius ?? 0.0,
    );
    Paint boxShadowPaint = boxShadow.toPaint();
    canvas.drawPath(pointsPath, boxShadowPaint);
  }

  // 2. second file the path
  canvas.drawPath(
    pointsPath,
    Paint()
      ..color = color ?? Colors.white
      ..style = PaintingStyle.fill
      ..strokeCap = StrokeCap.round
      ..strokeWidth = 1,
  );
}