performLayout method

  1. @override
void performLayout()
override

Do the work of computing the layout for this render object.

Do not call this function directly: call layout instead. This function is called by layout when there is actually work to be done by this render object during layout. The layout constraints provided by your parent are available via the constraints getter.

If sizedByParent is true, then this function should not actually change the dimensions of this render object. Instead, that work should be done by performResize. If sizedByParent is false, then this function should both change the dimensions of this render object and instruct its children to layout.

In implementing this function, you must call layout on each of your children, passing true for parentUsesSize if your layout information is dependent on your child's layout information. Passing true for parentUsesSize ensures that this render object will undergo layout if the child undergoes layout. Otherwise, the child can change its layout information without informing this render object.

Implementation

@override
void performLayout() {
  _hasVisualOverflow = false;
  if (childCount == 0) {
    size = constraints.smallest;
    assert(size.isFinite);
    return;
  }

  Rect bounds = Rect.zero;

  final Size maxSize = constraints.biggest;
  size = Size.zero;
  delegate._setAvailableSize(maxSize);

  RenderBox? child = firstChild;
  int index = 0;
  Rect previousChildRect = Rect.zero;
  int iteration = -1;
  while (child != null) {
    if (_fillGaps) {
      iteration = -1;
    }

    final ScatterParentData childParentData =
        child.parentData as ScatterParentData;
    childParentData.index = index;

    child.layout(constraints, parentUsesSize: true);

    final Size childSize = child.size;
    childParentData.width = childSize.width;
    childParentData.height = childSize.height;

    // Place the child following the placement strategy
    // until it does not overlap any previous child.
    final int max =
        _fillGaps ? _maxChildIteration * (index + 1) : _maxChildIteration;
    final int startIteration = iteration;
    do {
      assert(() {
        if (iteration - startIteration >= max) {
          throw FlutterError('Too much iterations for one child.\n'
              'It may be impossible to place another child with this delegate '
              'or consider to increase to maxChildIteration');
        }
        return true;
      }());
      final childOffset = delegate.getChildOffsetForIteration(
        ++iteration,
        ScatterContext._(
          childSize,
          previousChildRect,
          bounds,
          alignment,
        ),
      );
      childParentData.offset = childOffset;
    } while (_overlaps(childParentData));

    previousChildRect = childParentData.rect;
    bounds = bounds.expandToInclude(previousChildRect);

    child = childParentData.nextSibling;
    index++;
  }

  size = constraints
      .tighten(width: bounds.width, height: bounds.height)
      .smallest;

  _hasVisualOverflow =
      size.width < bounds.width || size.height < bounds.height;

  // Center the scatter.
  Offset boundsCenter = bounds.center;
  Offset scatterCenter = size.center(Offset.zero);
  Offset translation = scatterCenter - boundsCenter;

  // Move the whole scatter to the center.
  child = firstChild;
  while (child != null) {
    final ScatterParentData childParentData =
        child.parentData as ScatterParentData;
    childParentData.offset += translation;
    child = childParentData.nextSibling;
  }
}