addControlPoint method
Adds a control point to a connection at the specified position.
Control points are intermediate waypoints that define the path of editable connections. The new control point is inserted at the given index in the control points list.
Automatically invalidates the connection's cached path to trigger a repaint.
Does nothing if the connection doesn't exist.
Parameters:
connectionId: The ID of the connection to modifyposition: The position of the new control point in graph coordinatesindex: The index where the control point should be inserted. If null, appends to the end of the control points list.
Example:
// Add a control point at the end
controller.addControlPoint('conn1', Offset(150, 200));
// Insert a control point at a specific index
controller.addControlPoint('conn1', Offset(100, 150), index: 0);
Implementation
void addControlPoint(String connectionId, Offset position, {int? index}) {
final connection = _connections.firstWhere(
(c) => c.id == connectionId,
orElse: () => throw ArgumentError('Connection $connectionId not found'),
);
runInAction(() {
final controlPoints = List<Offset>.from(connection.controlPoints);
if (index != null && index >= 0 && index <= controlPoints.length) {
controlPoints.insert(index, position);
} else {
controlPoints.add(position);
}
connection.controlPoints = controlPoints;
});
// Invalidate cached path
_connectionPainter?.pathCache.removeConnection(connectionId);
}