getViewportCenter method
Gets the center point of the current viewport in graph coordinates.
This is useful for determining where to place new nodes so they appear in the center of the visible area.
Returns Offset.zero if the screen size is zero.
Returns the center point of the viewport in graph/world coordinates.
Example:
// Get the viewport center to place a new node there
final center = controller.getViewportCenter();
final newNode = Node(
id: 'new-node',
type: 'process',
position: center, // Node will appear at viewport center
);
controller.addNode(newNode);
Implementation
Offset getViewportCenter() {
if (_screenSize.value == Size.zero) return Offset.zero;
// Get the screen center point
final screenCenter = Offset(
_screenSize.value.width / 2,
_screenSize.value.height / 2,
);
// Convert to graph coordinates
return screenToWorld(screenCenter);
}