paint method
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:
-
Obtain an ImageStream, for example by calling ImageProvider.resolve on an AssetImage or NetworkImage object.
-
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.
-
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) {
// Use theme padding
final leftPadding = theme.padding.left;
final rightPadding = theme.padding.right;
final topPadding = theme.padding.top;
final bottomPadding = theme.padding.bottom;
final chartSize = Size(size.width - leftPadding - rightPadding, size.height - topPadding - bottomPadding);
final chartOffset = Offset(leftPadding, topPadding);
if (dataSets.isEmpty) return;
// Validate chart size
if (!chartSize.width.isFinite || !chartSize.height.isFinite || chartSize.width <= 0 || chartSize.height <= 0) {
return;
}
// Calculate bounds
double minX = .infinity;
double maxX = double.negativeInfinity;
double minY = .infinity;
double maxY = double.negativeInfinity;
for (final dataSet in dataSets) {
final point = dataSet.dataPoint;
if (point.x < minX) minX = point.x;
if (point.x > maxX) maxX = point.x;
if (point.y < minY) minY = point.y;
if (point.y > maxY) maxY = point.y;
}
// Validate bounds
if (!minX.isFinite || !maxX.isFinite || !minY.isFinite || !maxY.isFinite || minX == .infinity) {
return;
}
// Add padding to bounds
final xRange = maxX - minX;
final yRange = maxY - minY;
minX -= xRange * 0.1;
maxX += xRange * 0.1;
minY -= yRange * 0.1;
maxY += yRange * 0.1;
// Save canvas state
canvas.save();
canvas.translate(chartOffset.dx, chartOffset.dy);
// Draw grid and axes (background)
drawGrid(canvas, chartSize, minX, maxX, minY, maxY);
drawAxes(canvas, chartSize, minX, maxX, minY, maxY);
// Draw scatter points with Dribbble-like glow styling
for (int datasetIndex = 0; datasetIndex < dataSets.length; datasetIndex++) {
final dataSet = dataSets[datasetIndex];
final color = dataSet.color;
final point = dataSet.dataPoint;
// Validate point data
if (!point.x.isFinite || !point.y.isFinite) {
continue;
}
final canvasPoint = pointToCanvas(point, chartSize, minX, maxX, minY, maxY);
// Validate canvas point
if (!canvasPoint.dx.isFinite || !canvasPoint.dy.isFinite) {
continue;
}
// Check if this point is selected or hovered
final isSelected = selectedPoint?.datasetIndex == datasetIndex && selectedPoint?.elementIndex == 0;
final isHovered = hoveredPoint?.datasetIndex == datasetIndex && hoveredPoint?.elementIndex == 0;
// Determine point size and color based on state
final currentSize = isSelected || isHovered ? pointSize * 1.5 : pointSize;
final currentColor = isSelected || isHovered ? color.withValues(alpha: 1.0) : color.withValues(alpha: 0.8);
// Draw point with animation
final animatedSize = currentSize * animationProgress;
if (animatedSize > 0 && animatedSize.isFinite) {
// Outer glow
if (isSelected || isHovered) {
final outerPaint = Paint()
..color = currentColor.withValues(alpha: 0.3)
..style = PaintingStyle.fill;
canvas.drawCircle(canvasPoint, animatedSize * 1.8, outerPaint);
}
// Main dot
final fillPaint = Paint()
..color = currentColor
..style = PaintingStyle.fill;
canvas.drawCircle(canvasPoint, animatedSize, fillPaint);
// Inner highlight
final highlightPaint = Paint()
..color = Colors.white.withValues(alpha: 0.85)
..style = PaintingStyle.fill;
canvas.drawCircle(canvasPoint, animatedSize * 0.4, highlightPaint);
// Border (white if selected, card-colored otherwise)
final borderPaint = Paint()
..color = isSelected ? Colors.white : theme.backgroundColor
..style = PaintingStyle.stroke
..strokeWidth = isSelected ? 2.5 : 1.8;
canvas.drawCircle(canvasPoint, animatedSize, borderPaint);
}
}
// Optional crosshair on hovered/selected point
final interaction = hoveredPoint ?? selectedPoint;
if (interaction != null && interaction.isHit && interaction.point != null) {
final crosshairPos = pointToCanvas(interaction.point!, chartSize, minX, maxX, minY, maxY);
drawCrosshair(canvas, chartSize, position: crosshairPos);
}
canvas.restore();
// Axis labels above everything for clarity
canvas.save();
canvas.translate(chartOffset.dx, chartOffset.dy);
drawAxisLabels(canvas, chartSize, minX, maxX, minY, maxY, dataSets: dataSets);
canvas.restore();
}