animateToBounds method
void
animateToBounds(})
Animates the viewport to fit a bounding rectangle with padding.
The viewport will be adjusted to show all content within the bounds, with the specified padding on all sides.
Parameters:
bounds: The bounding rectangle in graph coordinatespadding: Padding around the bounds in screen pixels (default: 50)duration: Animation duration (default: 400ms)curve: Animation curve (default: easeInOut)
Example:
controller.animateToBounds(GraphRect(Rect.fromLTWH(0, 0, 500, 300)));
controller.animateToBounds(bounds, padding: 100);
Implementation
void animateToBounds(
GraphRect bounds, {
double padding = 50.0,
Duration duration = const Duration(milliseconds: 400),
Curve curve = Curves.easeInOut,
}) {
if (_screenSize.value == Size.zero || bounds.isEmpty) return;
final contentWidth = bounds.width;
final contentHeight = bounds.height;
// Calculate zoom to fit content with padding
final scaleX = (_screenSize.value.width - padding * 2) / contentWidth;
final scaleY = (_screenSize.value.height - padding * 2) / contentHeight;
final targetZoom = (scaleX < scaleY ? scaleX : scaleY).clamp(
_config.minZoom.value,
_config.maxZoom.value,
);
// Calculate center of bounds
final center = bounds.center;
animateToViewport(
GraphViewport(
x: _screenSize.value.width / 2 - center.dx * targetZoom,
y: _screenSize.value.height / 2 - center.dy * targetZoom,
zoom: targetZoom,
),
duration: duration,
curve: curve,
);
}