performBoxLayout method

  1. @override
void performBoxLayout(
  1. BoxConstraints constraints
)
override

Perform layout with typed box constraints

Subclasses override this instead of performLayout. The default implementation sizes to the maxima, treating an unbounded axis as the corresponding lower bound.

Implementation

@override
void performBoxLayout(BoxConstraints constraints) {
  final child = this.child;

  if (child == null) {
    super.performBoxLayout(constraints);
    return;
  }

  // Align fills bounded axes but lets its child choose a natural size within
  // those bounds, leaving an extent for the requested alignment to position.
  child.layout(
    BoxConstraints.loose(
      maxWidth: constraints.maxWidth,
      maxHeight: constraints.maxHeight,
    ),
  );
  final selfWidth =
      constraints.maxWidth ?? constraints.constrainWidth(child.width);
  final selfHeight =
      constraints.maxHeight ?? constraints.constrainHeight(child.height);
  size = Size(selfWidth, selfHeight);

  final childSize = child.size;
  final availableWidth = size.width - childSize.width;
  final availableHeight = size.height - childSize.height;

  final offsetX = ((alignment.x + 1) * availableWidth / 2).round();
  final offsetY = ((alignment.y + 1) * availableHeight / 2).round();

  positionChild(child, offsetX, offsetY);
}