performLayout method

  1. @override
void performLayout()
override

Performs the layout for this sliver.

It computes the sizes for each child based on the provided sizesValue list, lays out each child with the calculated width, and then sets the overall geometry of the sliver.

Implementation

@override
void performLayout() {
  // If there are no children, set geometry to zero and cache an empty state.
  if (firstChild == null) {
    geometry = SliverGeometry.zero;
    _childrenState = const [];
    return;
  }

  // Get the state of all children, including their calculated sizes.
  final childsState = _getChildsState();
  double currentXOffset = 0;
  double maxChildHeight = 0;

  // Layout each child sliver using the calculated size as the cross
  // axis extent.
  for (final childState in childsState) {
    childState.sliver.layout(
      // Modify constraints: set the cross axis extent to the calculated size.
      constraints.copyWith(crossAxisExtent: childState.size),
      parentUsesSize: true,
    );

    // Retrieve and update the parent's custom data with the
    // calculated offset.
    final parentData = childState.sliver.parentData;
    if (parentData is _SliverRowParentData) {
      parentData.paintOffset = Offset(currentXOffset, 0);
    }

    // Increase the offset by the child's size (or 0 if null).
    currentXOffset += childState.size ?? 0;
    maxChildHeight =
        max(maxChildHeight, childState.sliver.geometry?.paintExtent ?? 0);
  }

  // Determine the paint extent based on the available constraints.
  final paintExtent = min(constraints.remainingPaintExtent, maxChildHeight);

  // Set the geometry for this sliver so that scrolling and painting work
  // correctly.
  geometry = SliverGeometry(
    scrollExtent: maxChildHeight,
    paintExtent: paintExtent,
    layoutExtent: paintExtent,
    maxPaintExtent: maxChildHeight,
  );

  // Cache the calculated children state for use during painting
  // and hit testing.
  _childrenState = childsState;
}