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) {
  if (data.isEmpty) return;

  // Validate size
  if (!size.width.isFinite ||
      !size.height.isFinite ||
      size.width <= 0 ||
      size.height <= 0) {
    return;
  }

  // Sort data by value (largest to smallest for pyramid)
  final sortedData = List<PieData>.from(data)
    ..sort((a, b) => b.value.compareTo(a.value));

  final total = sortedData.fold<double>(0, (sum, item) => sum + item.value);

  // Validate total
  if (!total.isFinite || total <= 0) {
    return;
  }

  const padding = 40.0;
  final chartWidth = size.width - padding * 2;
  final chartHeight = size.height - padding * 2;
  final centerX = size.width / 2;

  // Validate dimensions
  if (!chartWidth.isFinite ||
      !chartHeight.isFinite ||
      chartWidth <= 0 ||
      chartHeight <= 0) {
    return;
  }

  // Calculate cumulative heights
  double cumulativeHeight = 0.0;

  for (int i = 0; i < sortedData.length; i++) {
    final segment = sortedData[i];

    // Validate segment value
    if (!segment.value.isFinite || segment.value < 0) {
      continue;
    }

    final percentage = segment.value / total;
    final segmentHeight = chartHeight * percentage * animationProgress;

    // Validate calculated dimensions
    if (!percentage.isFinite ||
        !segmentHeight.isFinite ||
        segmentHeight <= 0) {
      continue;
    }

    // Find original index in unsorted data
    final originalIndex = data.indexOf(segment);
    final segmentIndex = originalIndex >= 0 ? originalIndex : i;

    // Check if this segment is selected
    final isSelected = selectedSegment != null &&
        selectedSegment!.isHit &&
        selectedSegment!.elementIndex == segmentIndex;

    // Calculate width at this level (pyramid tapers)
    final baseWidth = chartWidth;
    final topWidth = chartWidth * 0.3; // Top is 30% of base
    final currentY = cumulativeHeight;
    final nextY = cumulativeHeight + segmentHeight;

    // Calculate width at current and next level
    final progress = currentY / chartHeight;
    final nextProgress = nextY / chartHeight;
    final currentWidth = baseWidth - (baseWidth - topWidth) * progress;
    final nextWidth = baseWidth - (baseWidth - topWidth) * nextProgress;

    // Draw trapezoid segment
    final path = Path()
      ..moveTo(centerX - currentWidth / 2, padding + currentY)
      ..lineTo(centerX + currentWidth / 2, padding + currentY)
      ..lineTo(centerX + nextWidth / 2, padding + nextY)
      ..lineTo(centerX - nextWidth / 2, padding + nextY)
      ..close();

    // Gradient fill
    final gradient = LinearGradient(
      begin: Alignment.topLeft,
      end: Alignment.bottomRight,
      colors: [
        segment.color,
        segment.color.withValues(alpha: 0.7),
      ],
    );

    final paint = Paint()
      ..shader = gradient.createShader(path.getBounds())
      ..style = PaintingStyle.fill;

    canvas.drawPath(path, paint);

    // Highlight selected segment
    if (isSelected) {
      final highlightPaint = Paint()
        ..color = Colors.white.withValues(alpha: 0.2)
        ..style = PaintingStyle.fill;
      canvas.drawPath(path, highlightPaint);
    }

    // Border - thicker and more visible for selected
    final borderPaint = Paint()
      ..color =
          isSelected ? Colors.white : segment.color.withValues(alpha: 0.5)
      ..style = PaintingStyle.stroke
      ..strokeWidth = isSelected ? 4.0 : 2.0;
    canvas.drawPath(path, borderPaint);

    // Label
    final labelY = padding + currentY + segmentHeight / 2;
    final textStyle = TextStyle(
      color: theme.textColor,
      fontSize: 12,
      fontWeight: FontWeight.w600,
    );

    final textPainter = TextPainter(
      text: TextSpan(
        text: '${segment.label}\n${(percentage * 100).toStringAsFixed(1)}%',
        style: textStyle,
      ),
      textAlign: TextAlign.center,
      textDirection: TextDirection.ltr,
    );
    textPainter.layout();

    if (textPainter.width < nextWidth * 0.9) {
      textPainter.paint(
        canvas,
        Offset(
          centerX - textPainter.width / 2,
          labelY - textPainter.height / 2,
        ),
      );
    }

    cumulativeHeight += segmentHeight;
  }
}