layout method

  1. @override
void layout(
  1. BoxConstraints constraints
)
override

Implementation

@override
void layout(BoxConstraints constraints) {
  super.layout(constraints);
  if (children.isEmpty) return;

  final child = children.first;
  // Let the child have unconstrained height so it can be as tall as it wants.
  // For the cross-axis (width), use tight constraints when bounded so the
  // child fills the full viewport width (matches Flutter's behavior).
  final childConstraints = BoxConstraints(
    minWidth: constraints.hasBoundedWidth
        ? constraints.maxWidth
        : constraints.minWidth,
    maxWidth: constraints.maxWidth,
    minHeight: 0,
    maxHeight: double.infinity,
  );
  child.layout(childConstraints);

  // Invalidate paint cache if child constraints or size changed.
  if (_cachedChildConstraints != childConstraints ||
      _cachedChildSize != child.size) {
    _cachedChildPaintLines = null;
    _cachedChildConstraints = childConstraints;
    _cachedChildSize = child.size;
  }

  // Our size is constrained to the parent.
  size = constraints.constrain(
    Size(
      constraints.hasBoundedWidth ? constraints.maxWidth : child.size.width,
      constraints.maxHeight,
    ),
  );

  // Update controller metrics.
  final viewportHeight = size.height.round();
  final contentHeight = child.size.height.round();
  if (_controller is WidgetScrollController) {
    (_controller as WidgetScrollController).updateMetrics(
      viewportExtent: viewportHeight,
      contentExtent: contentHeight,
    );
  } else if (_controller is ListViewController) {
    final lvc = _controller as ListViewController;
    lvc.setViewportHeight(viewportHeight);
    lvc.setContentHeight(contentHeight);
  }
  // For ViewportController or other custom controllers, the caller is
  // responsible for updating metrics.
}