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) {
  // 获取画笔
  final paint = Paint();

  // 定义圆角半径
  final radius = Radius.circular(display?.dDayRadius ?? 10);

  // 行列布局,每行7天
  for (int i = 0; i < month.days.length; i++) {
    final day = month.days[i];
    final x = ((i + month.firstDayWeekDayIndex) % 7) * daySize.width;
    final y = ((i + month.firstDayWeekDayIndex) ~/ 7) *
        (daySize.height + xbCalendarDayRowGap);

    Color textColor;

    if (day.isSelected) {
      paint.color = display?.colorSelectedDateBg ?? Colors.blue; // 选中的日期
      textColor = display?.colorSelectedDateText ?? Colors.white;
      if (day.isToday) {
        textColor = display?.colorSelectedTodayText ?? Colors.limeAccent;
      }
    } else {
      if (day.isInRange) {
        paint.color = display?.colorInRangeDateBg ??
            (display?.colorSelectedDateBg ?? Colors.blue)
                .withAlpha(100); // 在范围内的日期
        textColor = display?.colorInRangeDateText ?? Colors.black;
      } else {
        paint.color = Colors.transparent; // 普通日期
        textColor = Colors.black;
      }
      if (day.isToday) {
        textColor = display?.colorTodayText ?? Colors.green;
      }
    }

    final textStyle = TextStyle(
      color: textColor.withAlpha(day.isEnable ? 255 : 50),
      fontSize: 14,
    );

    // 绘制日期方块
    final rect = Rect.fromLTWH(x, y, daySize.width, daySize.height);

    // 圆角处理,根据开始日期和结束日期的状态判断
    if (day.isSelectedStart && day.isSelectedEnd) {
      // 如果开始日期和结束日期是同一天,则四个角都切圆角
      final rrect = RRect.fromRectAndCorners(rect,
          topLeft: radius,
          topRight: radius,
          bottomLeft: radius,
          bottomRight: radius);
      canvas.drawRRect(rrect, paint);
    } else if (day.isSelectedStart) {
      // 如果是开始日期,则只切左上和左下角
      final rrect =
          RRect.fromRectAndCorners(rect, topLeft: radius, bottomLeft: radius);
      canvas.drawRRect(rrect, paint);
    } else if (day.isSelectedEnd) {
      // 如果是结束日期,则只切右上和右下角
      final rrect = RRect.fromRectAndCorners(rect,
          topRight: radius, bottomRight: radius);
      canvas.drawRRect(rrect, paint);
    } else {
      // 如果不是开始或结束日期,则绘制普通的矩形
      canvas.drawRect(rect, paint);
    }

    // 绘制日期文字
    final textPainter = TextPainter(
      text: TextSpan(text: day.dateTime.day.toString(), style: textStyle),
      textAlign: TextAlign.center,
      textDirection: TextDirection.ltr,
    );

    textPainter.layout(minWidth: 0, maxWidth: daySize.width);
    textPainter.paint(
      canvas,
      Offset(x + (daySize.width - textPainter.width) / 2,
          y + (daySize.height - textPainter.height) / 2),
    );

    // 绘制标记
    if (day.isMark) {
      Color markColor;
      if (day.isSelectedStart || day.isSelectedEnd) {
        markColor = display?.colorMarkSelected ?? Colors.red;
      } else if (day.isInRange) {
        markColor = display?.colorMarkInRange ?? Colors.red;
      } else {
        markColor = display?.colorMark ?? Colors.red;
      }
      final markPainter = TextPainter(
        text: TextSpan(
            text: "●",
            style: TextStyle(
                color: markColor, fontSize: display?.dMarkSize ?? 12)),
        textAlign: TextAlign.center,
        textDirection: TextDirection.ltr,
      );
      markPainter.layout(minWidth: 0, maxWidth: daySize.width);
      markPainter.paint(
        canvas,
        Offset(
            x + (daySize.width - markPainter.width) / 2,
            y +
                (daySize.height - markPainter.height) / 2 +
                (textStyle.fontSize ?? 0).toDouble() -
                2),
      );
    }
  }
}