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) {
// Apply the canvas transform to convert graph coordinates to screen coordinates
canvas.save();
canvas.transform(transformationController.value.storage);
// Draw selection rectangle (in graph coordinates)
if (selectionRectangle != null) {
final paint = Paint()
..color = theme.connectionTheme.color.withValues(alpha: 0.2)
..style = PaintingStyle.fill;
final borderPaint = Paint()
..color = theme.connectionTheme.color
..style = PaintingStyle.stroke
..strokeWidth = 1.0;
canvas.drawRect(selectionRectangle!, paint);
canvas.drawRect(selectionRectangle!, borderPaint);
}
// Draw temporary connection (in graph coordinates)
if (temporaryConnection != null) {
final temp = temporaryConnection!;
final isStartFromOutput = temp.isStartFromOutput;
// Get the starting port (where drag began)
Port? startPort;
final startNode = controller.nodes[temp.startNodeId];
if (startNode != null) {
// Search in the appropriate port list based on port type
final portList = isStartFromOutput
? startNode.outputPorts
: startNode.inputPorts;
for (final port in portList) {
if (port.id == temp.startPortId) {
startPort = port;
break;
}
}
}
// Get the hovered port if available (where mouse is hovering)
Port? hoveredPort;
if (temp.targetNodeId != null && temp.targetPortId != null) {
final hoveredNode = controller.nodes[temp.targetNodeId!];
if (hoveredNode != null) {
// Hovered port is the opposite type of start port
final portList = isStartFromOutput
? hoveredNode.inputPorts
: hoveredNode.outputPorts;
for (final port in portList) {
if (port.id == temp.targetPortId) {
hoveredPort = port;
break;
}
}
}
}
// Determine actual source/target based on port direction:
// - If started from output: start port is SOURCE, hovered/mouse is TARGET
// - If started from input: start port is TARGET, hovered/mouse is SOURCE
final Port? sourcePort;
final Port? targetPort;
final Offset sourcePoint;
final Offset targetPoint;
final Rect? sourceNodeBounds;
final Rect? targetNodeBounds;
if (isStartFromOutput) {
// Output → Input: start is source, current point is target
sourcePort = startPort;
targetPort = hoveredPort;
sourcePoint = temp.startPoint;
targetPoint = temp.currentPoint;
sourceNodeBounds = temp.startNodeBounds;
targetNodeBounds = temp.targetNodeBounds;
} else {
// Input ← Output: start is target, current point is source
sourcePort = hoveredPort;
targetPort = startPort;
sourcePoint = temp.currentPoint;
targetPoint = temp.startPoint;
sourceNodeBounds = temp.targetNodeBounds;
targetNodeBounds = temp.startNodeBounds;
}
controller.connectionPainter.paintTemporaryConnection(
canvas,
sourcePoint,
targetPoint,
sourcePort: sourcePort,
targetPort: targetPort,
sourceNodeBounds: sourceNodeBounds,
targetNodeBounds: targetNodeBounds,
animationValue: animation?.value,
);
}
canvas.restore();
}