getLargestNodeForWrappingStack function
If the parent
is wrapping on an axis, one of its siblings
is going to be
laid out without Positioned or Align, marking the child as the best
candidate for the Stack to figure out its own size.
To do this, we need to find the largest child on the axis that the parent is wrapping on. If the parent is wrapping on both axes, we need to find the largest child by multiplying its width and height as a sort of best approximation.
Implementation
BaseNode? getLargestNodeForWrappingStack(
BaseNode parent, List<BaseNode> sibling) {
if (sibling.isEmpty) return null;
// Only horizontal wrap.
if (parent.isHorizontalWrap && !parent.isVerticalWrap) {
return sibling.reduce(
(a, b) => a.outerBoxLocal.width > b.outerBoxLocal.width ? a : b);
}
// Only vertical wrap, compare heights.
if (parent.isVerticalWrap && !parent.isHorizontalWrap) {
return sibling.reduce(
(a, b) => a.outerBoxLocal.height > b.outerBoxLocal.height ? a : b);
}
// Both wrap.
return sibling.reduce((a, b) =>
a.outerBoxLocal.height * a.outerBoxLocal.width >
b.outerBoxLocal.height * b.outerBoxLocal.width
? a
: b);
}