fitToView method
void
fitToView()
Adjusts the viewport to fit all nodes in the view with padding.
Calculates the optimal zoom level and pan position to show all nodes in the graph with 50 pixels of padding on all sides.
Does nothing if there are no nodes or if the screen size is zero.
Example:
// After loading a graph, fit it to view
controller.loadGraph(savedGraph);
controller.fitToView();
Implementation
void fitToView() {
if (_nodes.isEmpty || _screenSize.value == Size.zero) return;
final bounds = nodesBounds;
if (bounds == Rect.zero) return;
final contentWidth = bounds.width;
final contentHeight = bounds.height;
final padding = 50.0;
final scaleX = (_screenSize.value.width - padding * 2) / contentWidth;
final scaleY = (_screenSize.value.height - padding * 2) / contentHeight;
final zoom = (scaleX < scaleY ? scaleX : scaleY).clamp(
_config.minZoom.value,
_config.maxZoom.value,
);
final centerX = bounds.left + contentWidth / 2;
final centerY = bounds.top + contentHeight / 2;
setViewport(
GraphViewport(
x: _screenSize.value.width / 2 - centerX * zoom,
y: _screenSize.value.height / 2 - centerY * zoom,
zoom: zoom,
),
);
}