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.

Some special RenderObject subclasses (such as the one used by OverlayPortal.overlayChildLayoutBuilder) call applyPaintTransform in their performLayout implementation. To ensure such RenderObjects get the up-to-date paint transform, RenderObject subclasses should typically update the paint transform (as reported by applyPaintTransform) in this method instead of paint.

Implementation

@override
void performLayout() {
  _fragmentPositions.clear();
  _placeholderDimensions.clear();
  _scriptSpans.clear();

  // 1. Pre-layout of children (inline images): need real sizes
  //    to set PlaceholderDimensions of TextPainter.
  final childSizes = <Size>[];
  var child = firstChild;
  var placeholderIdx = 0;
  while (child != null) {
    child.layout(
      BoxConstraints.loose(Size(constraints.maxWidth, double.infinity)),
      parentUsesSize: true,
    );

    // Apply stretch logic to inline images
    var childSize = child.size;
    final inlineImages = collectInlineImages(_container);
    if (placeholderIdx < inlineImages.length) {
      final image = inlineImages[placeholderIdx];
      final originalWidth = image.width ?? 300.0;
      final availableWidth = constraints.maxWidth;

      // Apply stretch logic
      if (originalWidth > availableWidth && availableWidth > 0 && availableWidth != double.infinity) {
        final aspectRatio = (image.height ?? 300.0) / originalWidth;
        childSize = Size(availableWidth, availableWidth * aspectRatio);
      }
    }

    childSizes.add(childSize);
    child = (child.parentData as FluentInlineParentData).nextSibling;
    placeholderIdx++;
  }

  // 2. Build the TextSpan and populate _placeholderDimensions using the
  //    size of children (index 1:1 with visitation order).
  final textSpan = _buildTextSpanAndTrackPositions(_container, childSizes);

  _painter.textAlign = textAlign;
  _painter.text = textSpan;
  if (_placeholderDimensions.isNotEmpty) {
    _painter.setPlaceholderDimensions(_placeholderDimensions);
  }
  _painter.layout(maxWidth: constraints.maxWidth);
  _layoutMaxWidth = constraints.maxWidth;

  // Any layout change invalidates line-metrics and box caches.
  _lineMetricsDirty = true;
  _cachedSelectionBoxes = null;
  _cachedSpellBoxes = null;
  _cachedCommentBoxes = null;
  _cachedTextPicture?.dispose();
  _cachedTextPicture = null;

  // 3. Position children at offsets calculated by TextPainter.
  final placeholderBoxes = _painter.inlinePlaceholderBoxes ?? const [];
  child = firstChild;
  var i = 0;
  while (child != null && i < placeholderBoxes.length) {
    final box = placeholderBoxes[i];
    final parentData = child.parentData as FluentInlineParentData;
    parentData.offset = Offset(box.left, box.top);
    child = parentData.nextSibling;
    i++;
  }

  final width = _shrinkWrap ? _painter.width : constraints.maxWidth;
  size = constraints.constrain(Size(width, _painter.height));
}