layout method

  1. @override
PwBox layout(
  1. Context context,
  2. BoxConstraints constraints
)
override

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;
  // Soft-wrapped text must keep the full max width so start/end/center/
  // justify can pin glyphs to the correct edge (RTL headers and bullets).
  return _TextBox(
    PwSize(
      constraints.constrainWidth(maxW),
      constraints.constrainHeight(lh * rows),
    ),
    lines,
    style,
    pwResolvedTextAlign(align, ctx.textDirection == TextDirection.rtl),
    ctx.textDirection,
    PwResolvedStyle(ctx, style).baseline(ctx),
  );
}