layout method
layout API.
Implementation
@override
PwBox layout(Context context, BoxConstraints constraints) {
final Context ctx = textDirection == null
? context
: context.copyWith(textDirection: textDirection);
final TextAlign align = textAlign ?? TextAlign.start;
final double maxW = constraints.maxWidth.isFinite
? constraints.maxWidth
: pwMeasureText(ctx, text, style);
if (!softWrap) {
final double w = pwMeasureText(ctx, text, style);
final double h = PwResolvedStyle(ctx, style).lineHeight(ctx);
final List<BrokenLine> raw = pwWrapText(
ctx,
text,
w < 1 ? 1 : w,
style,
align: align,
);
final List<BrokenLine> lines = pwApplyOverflow(
ctx,
raw,
maxW,
style,
maxLines: maxLines,
overflow: overflow,
softWrap: false,
);
return _TextBox(
PwSize(constraints.constrainWidth(w), constraints.constrainHeight(h)),
lines,
style,
pwResolvedTextAlign(align, ctx.textDirection == TextDirection.rtl),
ctx.textDirection,
PwResolvedStyle(ctx, style).baseline(ctx),
);
}
final List<BrokenLine> raw = pwWrapText(
ctx,
text,
maxW,
style,
align: align,
);
final List<BrokenLine> lines = pwApplyOverflow(
ctx,
raw,
maxW,
style,
maxLines: maxLines,
overflow: overflow,
softWrap: true,
);
final double lh = PwResolvedStyle(ctx, style).lineHeight(ctx);
final int rows = lines.isEmpty ? 1 : lines.length;
// Content width (longest line), not the wrap budget. Claiming maxW under
// loose Row constraints made non-flex children (invoice meta / money
// amounts) steal the main axis so Expanded titles wrapped to one glyph
// and labels overlapped values.
var contentW = 0.0;
for (final BrokenLine line in lines) {
if (line.width > contentW) {
contentW = line.width;
}
}
final TextAlign resolvedAlign = pwResolvedTextAlign(
align,
ctx.textDirection == TextDirection.rtl,
);
final bool tightWidth = constraints.minWidth >= constraints.maxWidth;
// Center / justify need the full line box. start / end / left / right keep
// intrinsic width under loose constraints so Row meta / money amounts do
// not steal the main axis (Expanded still forces a tight width).
final bool fillWidth =
tightWidth ||
resolvedAlign == TextAlign.center ||
resolvedAlign == TextAlign.justify;
final double boxW = fillWidth
? constraints.constrainWidth(maxW)
: constraints.constrainWidth(contentW);
return _TextBox(
PwSize(boxW, constraints.constrainHeight(lh * rows)),
lines,
style,
resolvedAlign,
ctx.textDirection,
PwResolvedStyle(ctx, style).baseline(ctx),
);
}