layout method

  1. @override
void layout(
  1. Context context,
  2. BoxConstraints constraints, {
  3. bool parentUsesSize = false,
})
override

First widget pass to calculate the children layout and bounding box

Implementation

@override
void layout(Context context, BoxConstraints constraints,
    {bool parentUsesSize = false}) {
  final childCount = children.length;

  var hasNonPositionedChildren = false;

  if (childCount == 0) {
    box = PdfRect.fromPoints(PdfPoint.zero, constraints.biggest);
    return;
  }

  var width = constraints.minWidth;
  var height = constraints.minHeight;

  BoxConstraints? nonPositionedConstraints;

  switch (fit) {
    case StackFit.loose:
      nonPositionedConstraints = constraints.loosen();
      break;
    case StackFit.expand:
      nonPositionedConstraints = BoxConstraints.tight(constraints.biggest);
      break;
    case StackFit.passthrough:
      nonPositionedConstraints = constraints;
      break;
  }

  for (final child in children) {
    if (child is! Positioned) {
      hasNonPositionedChildren = true;

      child.layout(context, nonPositionedConstraints, parentUsesSize: true);
      assert(child.box != null);

      final childSize = child.box!;
      width = math.max(width, childSize.width);
      height = math.max(height, childSize.height);
    }
  }

  if (hasNonPositionedChildren) {
    box = PdfRect.fromPoints(PdfPoint.zero, PdfPoint(width, height));
    assert(box!.width == constraints.constrainWidth(width));
    assert(box!.height == constraints.constrainHeight(height));
  } else {
    box = PdfRect.fromPoints(PdfPoint.zero, constraints.biggest);
  }

  for (final child in children) {
    if (child is! Positioned) {
      child.box = PdfRect.fromPoints(
          alignment.inscribe(child.box!.size, box!).offset, child.box!.size);
    } else {
      final positioned = child;
      var childConstraints = const BoxConstraints();

      if (positioned.left != null && positioned.right != null) {
        childConstraints = childConstraints.tighten(
            width: box!.width - positioned.right! - positioned.left!);
      } else if (positioned.width != null) {
        childConstraints = childConstraints.tighten(width: positioned.width);
      }

      if (positioned.top != null && positioned.bottom != null) {
        childConstraints = childConstraints.tighten(
            height: box!.height - positioned.bottom! - positioned.top!);
      } else if (positioned.height != null) {
        childConstraints =
            childConstraints.tighten(height: positioned.height);
      }

      positioned.layout(context, childConstraints, parentUsesSize: true);
      assert(positioned.box != null);

      double? x;
      if (positioned.left != null) {
        x = positioned.left;
      } else if (positioned.right != null) {
        x = box!.width - positioned.right! - positioned.width!;
      } else {
        x = alignment.inscribe(positioned.box!.size, box!).x;
      }

      double? y;
      if (positioned.bottom != null) {
        y = positioned.bottom;
      } else if (positioned.top != null) {
        y = box!.height - positioned.top! - positioned.height!;
      } else {
        y = alignment.inscribe(positioned.box!.size, box!).y;
      }

      positioned.box =
          PdfRect.fromPoints(PdfPoint(x!, y!), positioned.box!.size);
    }
  }
}