layout method

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

layout API.

Implementation

@override
PwBox layout(Context context, BoxConstraints constraints) {
  final String label = title ?? text ?? '';
  if (label.isNotEmpty) {
    context.registerHeading(level, label);
  }
  // Margin stays outside the rule. A border on the outer edge sits on the
  // next paragraph and reads as a strike-through that runs to the margin.
  final EdgeInsets inset =
      margin ??
      EdgeInsets.only(top: level <= 1 ? 10 : 8, bottom: level <= 1 ? 14 : 6);
  final EdgeInsets pad =
      padding ?? EdgeInsets.only(bottom: level <= 1 ? 4 : 0);
  final Widget body =
      child ??
      Text(
        text ?? '',
        style: context.theme.headerStyle(level).merge(textStyle),
      );
  final BoxConstraints inner = constraints.deflate(inset).deflate(pad);
  final PwBox box = body.layout(context, inner);
  final bool rule = level <= 1;
  final double wide = constraints.hasBoundedWidth
      ? constraints.maxWidth
      : box.size.width + inset.horizontal + pad.horizontal;
  return ProxyBox(
    PwSize(
      constraints.constrainWidth(wide),
      constraints.constrainHeight(
        box.size.height + inset.vertical + pad.vertical,
      ),
    ),
    child: box,
    childOffset: PwOffset(inset.left + pad.left, inset.top + pad.top),
    onPaint: rule
        ? (Context context, PwOffset offset) {
            final PdfCanvas? canvas = context.canvas;
            if (canvas == null) {
              return;
            }
            final double y =
                offset.dy +
                inset.top +
                pad.top +
                box.size.height +
                pad.bottom;
            final double x0 = offset.dx + inset.left;
            final double x1 = offset.dx + wide - inset.right;
            if (x1 <= x0) {
              return;
            }
            canvas.endText();
            canvas.setStrokeColor('90A4AE');
            canvas.setLineWidth(0.6);
            canvas.moveTo(x0, y);
            canvas.lineTo(x1, y);
            canvas.stroke();
          }
        : null,
    onNote: label.isEmpty
        ? null
        : (Context context, PwOffset offset) {
            context.placeHeading(label, offset.dy + inset.top);
          },
  );
}