performLayout method
Hook for subclasses to perform layout within the given constraints.
Implementation
@override
Size performLayout(BoxConstraints constraints) {
final textWidget = widget as Text;
final width = constraints.maxWidth == BoxConstraints.infinity
? 999999
: constraints.maxWidth;
List<String> textLines;
if (!textWidget.wrap) {
textLines = [textWidget.data];
} else {
textLines = textWidget._wrapText(textWidget.data, width);
}
final limit = textWidget.maxLines != null
? min(textWidget.maxLines!, textLines.length)
: textLines.length;
var measuredWidth = 0;
final lineMeasurements = <int>[];
for (var i = 0; i < limit; i++) {
final lineW = measureStringWidth(textLines[i]);
lineMeasurements.add(lineW);
if (lineW > measuredWidth) {
measuredWidth = lineW;
}
}
final height = limit;
final resolvedSize = constraints.constrain(Size(measuredWidth, height));
// Now that final size is known, precompute render lines
_cachedLines = [];
final areaWidth = resolvedSize.width;
for (var i = 0; i < limit; i++) {
final line = textLines[i];
final lineWidth = lineMeasurements[i];
final startX = switch (textWidget.textAlign) {
TextAlign.left || TextAlign.justify => 0,
TextAlign.right => max(0, areaWidth - lineWidth),
TextAlign.center => max(0, (areaWidth - lineWidth) ~/ 2),
};
final availableWidth = areaWidth - startX;
final visibleChars = lineWidth > availableWidth
? textWidget._truncateToWidth(line, availableWidth)
: line;
_cachedLines.add(_RenderLine(visibleChars, startX));
}
return resolvedSize;
}