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 = Paint()
    ..color = backgroundColor!
    ..style = PaintingStyle.fill;

  ///1、画刻度值
  void drawSectionText() {
    if (!showSectionText!) return;
    double yPosition = sectionTextMarginTop; //sectionTextMarginTop
    if (progressHeight! > 2 * indicatorRadius!) {
      yPosition += (progressHeight! + size.height) / 2;
    } else {
      yPosition += indicatorRadius! + size.height / 2;
    }
    double e = (max! - min!) / sectionCount!;

    for (var i = 0; i < sectionCount! + 1; i++) {
      String point = (e * i + min!).toStringAsFixed(sectionDecimal!);

      if (sectionTexts!.isNotEmpty) {
        Iterable<SectionTextModel> match = sectionTexts!.where((item) {
          return item.position == i;
        });

        if (match.isNotEmpty) {
          SectionTextModel matchModel = match.first;
          point = matchModel.text;
        } else if (i == value! * sectionCount! &&
            afterDragShowSectionText != null &&
            afterDragShowSectionText!) {
        } else {
          point = '';
        }
      }

      Size textSize = getTextWidth(text: point, fontsize: sectionTextSize);
      Color? textColor = sectionTextColor;
      if (sectionSelectTextColor != Colors.transparent &&
          i == value! * sectionCount!) {
        textColor = sectionSelectTextColor;
      }
      //为了计算文字和size的偏差
      double th = 0;
      if (textSize.height > size.height) {
        th = -(textSize.height - size.height) / 2;
      }
      canvas.drawParagraph(
          getParagraph(
              text: point,
              fontsize: sectionTextSize,
              textColor: textColor,
              textSize: textSize),
          Offset(i * size.width / sectionCount! - textSize.width / 2,
              yPosition + th));
    }
  }

  canvas.drawPath(drawPath(progressHeight!, size.width, size.height, radius),
      paint); //画背景
  //下面是画矩形
  // Size newSize = Size(size.width - indicatorRadius, size.height);
  // canvas.drawRect(Offset.zero & newSize, paint);

  paint.color = progressColor!;

  // 画进度条
  void drawBar(double x, double? progress) {
    if (x <= 0.0) return;
    //如果是分段,而且有自定义的刻度值
    if (sectionCount! > 1 && sectionTexts!.length > 1) {
      for (var item in sectionTexts!) {
        if (progress! * sectionCount! >= item.position) {
          paint.color = indicatorColor = sectionColor = item.progressColor;
        }
      }
    }

    canvas.drawPath(drawPath(progressHeight!, x, size.height, radius), paint);
    // canvas.drawRect(Offset(x, 0.0) & Size(width, size.height), paint);
  }

  //画间隔
  void drawInterval() {
    if (sectionCount! <= 1) return;
    for (var i = 0; i < sectionCount! + 1; i++) {
      paint.color =
          i > value! * sectionCount! ? sectionUnSelectColor! : sectionColor!;

      canvas.drawCircle(
          Offset(i * size.width / sectionCount!, size.height / 2),
          sectionRadius!,
          paint);
    }
  }

  // 画当前显示的值的指示器
  void drawIndicator() {
    if (indicatorRadius! <= 0.0) return;
    indicatorColor ??= progressColor;
    Paint indicatorPaint = Paint()
      ..style = PaintingStyle.fill
      ..color = indicatorColor!;
    canvas.drawCircle(Offset(value! * size.width, size.height / 2),
        indicatorRadius!, indicatorPaint);
  }

  //画顶部的指示器
  void drawTopBubble() {
    if (hideBubble! || !alwaysShowBubble!) return;
    paint.color = bubbleColor!;
    double bubbleInCenterY = 0.0;
    double? newBubbleHeight;
    if (bubbleInCenter!) {
      bubbleMargin = 0.0;
      bubbleInCenterY = bubbleHeight! - bubbleRadius!;
      newBubbleHeight = bubbleHeight;
    } else {
      bubbleInCenterY = indicatorRadius! - size.height / 2 + bubbleMargin!;
      newBubbleHeight = bubbleHeight! + bubbleInCenterY;
    }
    //计算bubble的坐标值,画出bubble,
    double x = bubbleRadius! /
        (newBubbleHeight! - bubbleRadius!) *
        math.sqrt((math.pow(newBubbleHeight - bubbleRadius!, 2) -
            math.pow(bubbleRadius!, 2)));
    double y = math.sqrt(math.pow(bubbleRadius!, 2) - x * x);
    Path bubblePath = Path()
      ..moveTo(value! * size.width,
          bubbleInCenter! ? bubbleInCenterY : -bubbleInCenterY)
      ..lineTo(
          value! * size.width + x,
          bubbleInCenter!
              ? -newBubbleHeight + bubbleRadius! + y + bubbleInCenterY
              : -newBubbleHeight + bubbleRadius! + y)
      ..arcToPoint(
          Offset(
              value! * size.width - x,
              bubbleInCenter!
                  ? -newBubbleHeight + bubbleRadius! + y + bubbleInCenterY
                  : -newBubbleHeight + bubbleRadius! + y),
          radius: Radius.circular(bubbleRadius!),
          clockwise: false,
          largeArc: true)
      ..close();
    canvas.drawPath(bubblePath, paint);

    double realValue = (max! - min!) * value! + min!;
    int rv = realValue.ceil();
    String text = '$rv';
    double? fontSize = bubbleTextSize;

    //此���主要是拿到text的宽高,然后给个约束,同时把text准确的放到某个位置上
    Size textSize = getTextWidth(text: text, fontsize: fontSize);

    canvas.drawParagraph(
        getParagraph(
            text: text,
            fontsize: fontSize,
            textColor: bubbleTextColor,
            textSize: textSize),
        Offset(
            value! * size.width - textSize.width / 2,
            bubbleInCenter!
                ? -newBubbleHeight +
                    bubbleRadius! -
                    textSize.height / 2 +
                    bubbleInCenterY
                : -newBubbleHeight + bubbleRadius! - textSize.height / 2));
  }

  drawSectionText(); // draw section text 画刻度值

  drawBar(value!.clamp(0.0, 1.0) * size.width, value); //画进度
  drawInterval(); //画间隔

  drawIndicator(); //draw indicator
  drawTopBubble(); //draw top bubble
}