layoutPositionedChild method Null safety

bool layoutPositionedChild(
  1. RenderBox child,
  2. StackParentData childParentData,
  3. Size size,
  4. Alignment alignment
)

Lays out the positioned child according to alignment within a Stack of size.

Returns true when the child has visual overflow.

Implementation

static bool layoutPositionedChild(RenderBox child, StackParentData childParentData, Size size, Alignment alignment) {
  assert(childParentData.isPositioned);
  assert(child.parentData == childParentData);

  bool hasVisualOverflow = false;
  BoxConstraints childConstraints = const BoxConstraints();

  if (childParentData.left != null && childParentData.right != null)
    childConstraints = childConstraints.tighten(width: size.width - childParentData.right! - childParentData.left!);
  else if (childParentData.width != null)
    childConstraints = childConstraints.tighten(width: childParentData.width);

  if (childParentData.top != null && childParentData.bottom != null)
    childConstraints = childConstraints.tighten(height: size.height - childParentData.bottom! - childParentData.top!);
  else if (childParentData.height != null)
    childConstraints = childConstraints.tighten(height: childParentData.height);

  child.layout(childConstraints, parentUsesSize: true);

  late final double x;
  if (childParentData.left != null) {
    x = childParentData.left!;
  } else if (childParentData.right != null) {
    x = size.width - childParentData.right! - child.size.width;
  } else {
    x = alignment.alongOffset(size - child.size as Offset).dx;
  }

  if (x < 0.0 || x + child.size.width > size.width)
    hasVisualOverflow = true;

  late final double y;
  if (childParentData.top != null) {
    y = childParentData.top!;
  } else if (childParentData.bottom != null) {
    y = size.height - childParentData.bottom! - child.size.height;
  } else {
    y = alignment.alongOffset(size - child.size as Offset).dy;
  }

  if (y < 0.0 || y + child.size.height > size.height)
    hasVisualOverflow = true;

  childParentData.offset = Offset(x, y);

  return hasVisualOverflow;
}