RenderFlex baseline intrinsics (#145483)
Implements `RenderFlex` intrinsics calculation when children are baseline aligned in the cross axis.
This commit is contained in:
@@ -2046,7 +2046,9 @@ abstract class RenderBox extends RenderObject {
|
||||
final double? baselineOffset = _computeIntrinsics(_CachedLayoutCalculation.baseline, (constraints, baseline), _computeDryBaseline).offset;
|
||||
// This assert makes sure computeDryBaseline always gets called in debug mode,
|
||||
// in case the computeDryBaseline implementation invokes debugCannotComputeDryLayout.
|
||||
assert(baselineOffset == computeDryBaseline(constraints, baseline));
|
||||
// This check should be skipped when debugCheckintIntrinsics is true to avoid
|
||||
// slowing down the app significantly.
|
||||
assert(RenderObject.debugCheckingIntrinsics || baselineOffset == computeDryBaseline(constraints, baseline));
|
||||
return baselineOffset;
|
||||
}
|
||||
|
||||
@@ -2508,18 +2510,20 @@ abstract class RenderBox extends RenderObject {
|
||||
}
|
||||
}
|
||||
|
||||
testIntrinsicsForValues(getMinIntrinsicWidth, getMaxIntrinsicWidth, 'Width', double.infinity);
|
||||
testIntrinsicsForValues(getMinIntrinsicHeight, getMaxIntrinsicHeight, 'Height', double.infinity);
|
||||
if (constraints.hasBoundedWidth) {
|
||||
testIntrinsicsForValues(getMinIntrinsicWidth, getMaxIntrinsicWidth, 'Width', constraints.maxHeight);
|
||||
}
|
||||
if (constraints.hasBoundedHeight) {
|
||||
testIntrinsicsForValues(getMinIntrinsicHeight, getMaxIntrinsicHeight, 'Height', constraints.maxWidth);
|
||||
try {
|
||||
testIntrinsicsForValues(getMinIntrinsicWidth, getMaxIntrinsicWidth, 'Width', double.infinity);
|
||||
testIntrinsicsForValues(getMinIntrinsicHeight, getMaxIntrinsicHeight, 'Height', double.infinity);
|
||||
if (constraints.hasBoundedWidth) {
|
||||
testIntrinsicsForValues(getMinIntrinsicWidth, getMaxIntrinsicWidth, 'Width', constraints.maxHeight);
|
||||
}
|
||||
if (constraints.hasBoundedHeight) {
|
||||
testIntrinsicsForValues(getMinIntrinsicHeight, getMaxIntrinsicHeight, 'Height', constraints.maxWidth);
|
||||
}
|
||||
// TODO(ianh): Test that values are internally consistent in more ways than the above.
|
||||
} finally {
|
||||
RenderObject.debugCheckingIntrinsics = false;
|
||||
}
|
||||
|
||||
// TODO(ianh): Test that values are internally consistent in more ways than the above.
|
||||
|
||||
RenderObject.debugCheckingIntrinsics = false;
|
||||
if (failures.isNotEmpty) {
|
||||
// TODO(jacobr): consider nesting the failures object so it is collapsible.
|
||||
throw FlutterError.fromParts(<DiagnosticsNode>[
|
||||
|
||||
@@ -12,6 +12,82 @@ import 'layer.dart';
|
||||
import 'layout_helper.dart';
|
||||
import 'object.dart';
|
||||
|
||||
// A 2D vector that uses a [RenderFlex]'s main axis and cross axis as its first and second coordinate axes.
|
||||
// It represents the same vector as (double mainAxisExtent, double crossAxisExtent).
|
||||
extension type const _AxisSize._(Size _size) {
|
||||
_AxisSize({ required double mainAxisExtent, required double crossAxisExtent }) : this._(Size(mainAxisExtent, crossAxisExtent));
|
||||
_AxisSize.fromSize({ required Size size, required Axis direction }) : this._(_convert(size, direction));
|
||||
|
||||
static const _AxisSize empty = _AxisSize._(Size.zero);
|
||||
|
||||
static Size _convert(Size size, Axis direction) {
|
||||
return switch (direction) {
|
||||
Axis.horizontal => size,
|
||||
Axis.vertical => size.flipped,
|
||||
};
|
||||
}
|
||||
double get mainAxisExtent => _size.width;
|
||||
double get crossAxisExtent => _size.height;
|
||||
|
||||
Size toSize(Axis direction) => _convert(_size, direction);
|
||||
|
||||
_AxisSize applyConstraints(BoxConstraints constraints, Axis direction) {
|
||||
final BoxConstraints effectiveConstraints = switch (direction) {
|
||||
Axis.horizontal => constraints,
|
||||
Axis.vertical => constraints.flipped,
|
||||
};
|
||||
return _AxisSize._(effectiveConstraints.constrain(_size));
|
||||
}
|
||||
|
||||
_AxisSize operator +(_AxisSize other) => _AxisSize._(Size(_size.width + other._size.width, math.max(_size.height, other._size.height)));
|
||||
}
|
||||
|
||||
// The ascent and descent of a baseline-aligned child.
|
||||
//
|
||||
// Baseline-aligned children contributes to the cross axis extent of a [RenderFlex]
|
||||
// differently from chidren with other [CrossAxisAlignment]s.
|
||||
extension type const _AscentDescent._((double ascent, double descent)? ascentDescent) {
|
||||
factory _AscentDescent({ required double? baselineOffset, required double crossSize }) {
|
||||
return baselineOffset == null ? none : _AscentDescent._((baselineOffset, crossSize - baselineOffset));
|
||||
}
|
||||
static const _AscentDescent none = _AscentDescent._(null);
|
||||
|
||||
double? get baselineOffset => ascentDescent?.$1;
|
||||
|
||||
_AscentDescent operator +(_AscentDescent other) => switch ((this, other)) {
|
||||
(null, final _AscentDescent v) || (final _AscentDescent v, null) => v,
|
||||
((final double xAscent, final double xDescent),
|
||||
(final double yAscent, final double yDescent)) => _AscentDescent._((math.max(xAscent, yAscent), math.max(xDescent, yDescent))),
|
||||
};
|
||||
}
|
||||
|
||||
typedef _ChildSizingFunction = double Function(RenderBox child, double extent);
|
||||
typedef _NextChild = RenderBox? Function(RenderBox child);
|
||||
|
||||
class _LayoutSizes {
|
||||
_LayoutSizes({
|
||||
required this.axisSize,
|
||||
required this.baselineOffset,
|
||||
required this.mainAxisFreeSpace,
|
||||
required this.spacePerFlex,
|
||||
}) : assert(spacePerFlex?.isFinite ?? true);
|
||||
|
||||
// The final constrained _AxisSize of the RenderFlex.
|
||||
final _AxisSize axisSize;
|
||||
|
||||
// The free space along the main axis. If the value is positive, the free space
|
||||
// will be distributed according to the [MainAxisAliggnment] specified. A
|
||||
// negative value indicates the RenderFlex overflows along the main axis.
|
||||
final double mainAxisFreeSpace;
|
||||
|
||||
// Null if the RenderFlex is not baseline aligned, or none of its children has
|
||||
// a valid baseline of the given [TextBaseline] type.
|
||||
final double? baselineOffset;
|
||||
|
||||
// The allocated space for flex children.
|
||||
final double? spacePerFlex;
|
||||
}
|
||||
|
||||
/// How the child is inscribed into the available space.
|
||||
///
|
||||
/// See also:
|
||||
@@ -134,7 +210,23 @@ enum MainAxisAlignment {
|
||||
|
||||
/// Place the free space evenly between the children as well as before and
|
||||
/// after the first and last child.
|
||||
spaceEvenly,
|
||||
spaceEvenly;
|
||||
|
||||
(double leadingSpace, double betweenSpace) _distributeSpace(double freeSpace, int itemCount, bool flipped) {
|
||||
assert(itemCount >= 0);
|
||||
return switch (this) {
|
||||
MainAxisAlignment.start => flipped ? (freeSpace, 0.0) : (0.0, 0.0),
|
||||
|
||||
MainAxisAlignment.end => MainAxisAlignment.start._distributeSpace(freeSpace, itemCount, !flipped),
|
||||
MainAxisAlignment.spaceBetween when itemCount < 2 => MainAxisAlignment.start._distributeSpace(freeSpace, itemCount, flipped),
|
||||
MainAxisAlignment.spaceAround when itemCount == 0 => MainAxisAlignment.start._distributeSpace(freeSpace, itemCount, flipped),
|
||||
|
||||
MainAxisAlignment.center => (freeSpace / 2.0, 0.0),
|
||||
MainAxisAlignment.spaceBetween => (0.0, freeSpace / (itemCount - 1)),
|
||||
MainAxisAlignment.spaceAround => (freeSpace / itemCount / 2, freeSpace / itemCount),
|
||||
MainAxisAlignment.spaceEvenly => (freeSpace / (itemCount + 1), freeSpace / (itemCount + 1)),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/// How the children should be placed along the cross axis in a flex layout.
|
||||
@@ -216,29 +308,19 @@ enum CrossAxisAlignment {
|
||||
/// See also:
|
||||
///
|
||||
/// * [RenderBox.getDistanceToBaseline], which defines the baseline of a box.
|
||||
baseline,
|
||||
}
|
||||
baseline;
|
||||
|
||||
bool? _startIsTopLeft(Axis direction, TextDirection? textDirection, VerticalDirection? verticalDirection) {
|
||||
// If the relevant value of textDirection or verticalDirection is null, this returns null too.
|
||||
switch (direction) {
|
||||
case Axis.horizontal:
|
||||
return switch (textDirection) {
|
||||
TextDirection.ltr => true,
|
||||
TextDirection.rtl => false,
|
||||
null => null,
|
||||
};
|
||||
case Axis.vertical:
|
||||
return switch (verticalDirection) {
|
||||
VerticalDirection.down => true,
|
||||
VerticalDirection.up => false,
|
||||
null => null,
|
||||
};
|
||||
double _getChildCrossAxisOffset(double freeSpace, bool flipped) {
|
||||
// This method should not be used to position baseline-aligned children.
|
||||
return switch (this) {
|
||||
CrossAxisAlignment.stretch || CrossAxisAlignment.baseline => 0.0,
|
||||
CrossAxisAlignment.start => flipped ? freeSpace : 0.0,
|
||||
CrossAxisAlignment.center => freeSpace / 2,
|
||||
CrossAxisAlignment.end => CrossAxisAlignment.start._getChildCrossAxisOffset(freeSpace, !flipped),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
typedef _ChildSizingFunction = double Function(RenderBox child, double extent);
|
||||
|
||||
/// Displays its children in a one-dimensional array.
|
||||
///
|
||||
/// ## Layout algorithm
|
||||
@@ -449,6 +531,9 @@ class RenderFlex extends RenderBox with ContainerRenderObjectMixin<RenderBox, Fl
|
||||
}
|
||||
|
||||
bool get _debugHasNecessaryDirections {
|
||||
if (RenderObject.debugCheckingIntrinsics) {
|
||||
return true;
|
||||
}
|
||||
if (firstChild != null && lastChild != firstChild) {
|
||||
// i.e. there's more than one child
|
||||
switch (direction) {
|
||||
@@ -505,14 +590,14 @@ class RenderFlex extends RenderBox with ContainerRenderObjectMixin<RenderBox, Fl
|
||||
}
|
||||
}
|
||||
|
||||
bool get _canComputeIntrinsics => crossAxisAlignment != CrossAxisAlignment.baseline;
|
||||
|
||||
double _getIntrinsicSize({
|
||||
required Axis sizingDirection,
|
||||
required double extent, // the extent in the direction that isn't the sizing direction
|
||||
required _ChildSizingFunction childSize, // a method to find the size in the sizing direction
|
||||
}) {
|
||||
if (!_canComputeIntrinsics) {
|
||||
// TODO(LongCatIsLooong): use _computeSizes for intrinsics calculation and
|
||||
// add support for baseline alignment.
|
||||
if (_isBaselineAligned) {
|
||||
// Intrinsics cannot be calculated without a full layout for
|
||||
// baseline alignment. Throw an assertion and return 0.0 as documented
|
||||
// on [RenderBox.computeMinIntrinsicWidth].
|
||||
@@ -639,16 +724,26 @@ class RenderFlex extends RenderBox with ContainerRenderObjectMixin<RenderBox, Fl
|
||||
return defaultComputeDistanceToFirstActualBaseline(baseline);
|
||||
}
|
||||
|
||||
int _getFlex(RenderBox child) {
|
||||
static int _getFlex(RenderBox child) {
|
||||
final FlexParentData childParentData = child.parentData! as FlexParentData;
|
||||
return childParentData.flex ?? 0;
|
||||
}
|
||||
|
||||
FlexFit _getFit(RenderBox child) {
|
||||
static FlexFit _getFit(RenderBox child) {
|
||||
final FlexParentData childParentData = child.parentData! as FlexParentData;
|
||||
return childParentData.fit ?? FlexFit.tight;
|
||||
}
|
||||
|
||||
bool get _isBaselineAligned {
|
||||
return switch (crossAxisAlignment) {
|
||||
CrossAxisAlignment.baseline => switch (direction) {
|
||||
Axis.horizontal => true,
|
||||
Axis.vertical => false,
|
||||
},
|
||||
CrossAxisAlignment.start || CrossAxisAlignment.center || CrossAxisAlignment.end || CrossAxisAlignment.stretch => false,
|
||||
};
|
||||
}
|
||||
|
||||
double _getCrossSize(Size size) {
|
||||
return switch (_direction) {
|
||||
Axis.horizontal => size.height,
|
||||
@@ -663,15 +758,131 @@ class RenderFlex extends RenderBox with ContainerRenderObjectMixin<RenderBox, Fl
|
||||
};
|
||||
}
|
||||
|
||||
// flipMainAxis is used to decide whether to lay out
|
||||
// left-to-right/top-to-bottom (false), or right-to-left/bottom-to-top
|
||||
// (true). Returns false in cases when the layout direction does not matter
|
||||
// (for instance, there is no child).
|
||||
bool get _flipMainAxis => firstChild != null && switch (direction) {
|
||||
Axis.horizontal => switch (textDirection) {
|
||||
null || TextDirection.ltr => false,
|
||||
TextDirection.rtl => true,
|
||||
},
|
||||
Axis.vertical => switch (verticalDirection) {
|
||||
VerticalDirection.down => false,
|
||||
VerticalDirection.up => true,
|
||||
},
|
||||
};
|
||||
|
||||
bool get _flipCrossAxis => firstChild != null && switch (direction) {
|
||||
Axis.vertical => switch (textDirection) {
|
||||
null || TextDirection.ltr => false,
|
||||
TextDirection.rtl => true,
|
||||
},
|
||||
Axis.horizontal => switch (verticalDirection) {
|
||||
VerticalDirection.down => false,
|
||||
VerticalDirection.up => true,
|
||||
},
|
||||
};
|
||||
|
||||
BoxConstraints _constraintsForNonFlexChild(BoxConstraints constraints) {
|
||||
final bool fillCrossAxis = switch (crossAxisAlignment) {
|
||||
CrossAxisAlignment.stretch => true,
|
||||
CrossAxisAlignment.start ||
|
||||
CrossAxisAlignment.center ||
|
||||
CrossAxisAlignment.end ||
|
||||
CrossAxisAlignment.baseline => false,
|
||||
};
|
||||
return switch (_direction) {
|
||||
Axis.horizontal => fillCrossAxis
|
||||
? BoxConstraints.tightFor(height: constraints.maxHeight)
|
||||
: BoxConstraints(maxHeight: constraints.maxHeight),
|
||||
Axis.vertical => fillCrossAxis
|
||||
? BoxConstraints.tightFor(width: constraints.maxWidth)
|
||||
: BoxConstraints(maxWidth: constraints.maxWidth),
|
||||
};
|
||||
}
|
||||
|
||||
BoxConstraints _constraintsForFlexChild(RenderBox child, BoxConstraints constraints, double maxChildExtent) {
|
||||
assert(_getFlex(child) > 0.0);
|
||||
assert(maxChildExtent >= 0.0);
|
||||
final double minChildExtent = switch (_getFit(child)) {
|
||||
FlexFit.tight => maxChildExtent,
|
||||
FlexFit.loose => 0.0,
|
||||
};
|
||||
final bool fillCrossAxis = switch (crossAxisAlignment) {
|
||||
CrossAxisAlignment.stretch => true,
|
||||
CrossAxisAlignment.start ||
|
||||
CrossAxisAlignment.center ||
|
||||
CrossAxisAlignment.end ||
|
||||
CrossAxisAlignment.baseline => false,
|
||||
};
|
||||
return switch (_direction) {
|
||||
Axis.horizontal => BoxConstraints(
|
||||
minWidth: minChildExtent,
|
||||
maxWidth: maxChildExtent,
|
||||
minHeight: fillCrossAxis ? constraints.maxHeight : 0.0,
|
||||
maxHeight: constraints.maxHeight,
|
||||
),
|
||||
Axis.vertical => BoxConstraints(
|
||||
minWidth: fillCrossAxis ? constraints.maxWidth : 0.0,
|
||||
maxWidth: constraints.maxWidth,
|
||||
minHeight: minChildExtent,
|
||||
maxHeight: maxChildExtent,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
@override
|
||||
double? computeDryBaseline(BoxConstraints constraints, TextBaseline baseline) {
|
||||
final _LayoutSizes sizes = _computeSizes(
|
||||
constraints: constraints,
|
||||
layoutChild: ChildLayoutHelper.dryLayoutChild,
|
||||
getBaseline: ChildLayoutHelper.getDryBaseline,
|
||||
);
|
||||
|
||||
if (_isBaselineAligned) {
|
||||
return sizes.baselineOffset;
|
||||
}
|
||||
|
||||
final BoxConstraints nonFlexConstraints = _constraintsForNonFlexChild(constraints);
|
||||
BoxConstraints constraintsForChild(RenderBox child) {
|
||||
final double? spacePerFlex = sizes.spacePerFlex;
|
||||
final int flex;
|
||||
return spacePerFlex != null && (flex = _getFlex(child)) > 0
|
||||
? _constraintsForFlexChild(child, constraints, flex * spacePerFlex)
|
||||
: nonFlexConstraints;
|
||||
}
|
||||
|
||||
BaselineOffset baselineOffset = BaselineOffset.noBaseline;
|
||||
switch (direction) {
|
||||
case Axis.vertical:
|
||||
final double freeSpace = math.max(0.0, sizes.mainAxisFreeSpace);
|
||||
final bool flipMainAxis = _flipMainAxis;
|
||||
final (double leadingSpaceY, double spaceBetween) = mainAxisAlignment._distributeSpace(freeSpace, childCount, flipMainAxis);
|
||||
double y = leadingSpaceY;
|
||||
final (_NextChild nextChild, RenderBox? topLeftChild) = flipMainAxis ? (childBefore, lastChild) : (childAfter, firstChild);
|
||||
for (RenderBox? child = topLeftChild; child != null; child = nextChild(child)) {
|
||||
final BoxConstraints childConstraints = constraintsForChild(child);
|
||||
final Size childSize = child.getDryLayout(childConstraints);
|
||||
baselineOffset = baselineOffset.minOf(BaselineOffset(child.getDryBaseline(childConstraints, baseline)) + y);
|
||||
y += spaceBetween + childSize.height;
|
||||
}
|
||||
case Axis.horizontal:
|
||||
final bool flipCrossAxis = _flipCrossAxis;
|
||||
for (RenderBox? child = firstChild; child != null; child = childAfter(child)) {
|
||||
final BoxConstraints childConstraints = constraintsForChild(child);
|
||||
final BaselineOffset distance = BaselineOffset(child.getDryBaseline(childConstraints, baseline));
|
||||
final double freeCrossAxisSpace = sizes.axisSize.crossAxisExtent - child.getDryLayout(childConstraints).height;
|
||||
final BaselineOffset childBaseline = distance + crossAxisAlignment._getChildCrossAxisOffset(freeCrossAxisSpace, flipCrossAxis);
|
||||
baselineOffset = baselineOffset.minOf(childBaseline);
|
||||
}
|
||||
}
|
||||
return baselineOffset.offset;
|
||||
}
|
||||
|
||||
@override
|
||||
@protected
|
||||
Size computeDryLayout(covariant BoxConstraints constraints) {
|
||||
if (!_canComputeIntrinsics) {
|
||||
assert(debugCannotComputeDryLayout(
|
||||
reason: 'Dry layout cannot be computed for CrossAxisAlignment.baseline, which requires a full layout.',
|
||||
));
|
||||
return Size.zero;
|
||||
}
|
||||
FlutterError? constraintsError;
|
||||
assert(() {
|
||||
constraintsError = _debugCheckConstraints(
|
||||
@@ -685,15 +896,11 @@ class RenderFlex extends RenderBox with ContainerRenderObjectMixin<RenderBox, Fl
|
||||
return Size.zero;
|
||||
}
|
||||
|
||||
final _LayoutSizes sizes = _computeSizes(
|
||||
layoutChild: ChildLayoutHelper.dryLayoutChild,
|
||||
return _computeSizes(
|
||||
constraints: constraints,
|
||||
);
|
||||
|
||||
return constraints.constrain(switch (_direction) {
|
||||
Axis.horizontal => Size(sizes.mainSize, sizes.crossSize),
|
||||
Axis.vertical => Size(sizes.crossSize, sizes.mainSize),
|
||||
});
|
||||
layoutChild: ChildLayoutHelper.dryLayoutChild,
|
||||
getBaseline: ChildLayoutHelper.getDryBaseline,
|
||||
).axisSize.toSize(direction);
|
||||
}
|
||||
|
||||
FlutterError? _debugCheckConstraints({required BoxConstraints constraints, required bool reportParentConstraints}) {
|
||||
@@ -780,96 +987,87 @@ class RenderFlex extends RenderBox with ContainerRenderObjectMixin<RenderBox, Fl
|
||||
return result;
|
||||
}
|
||||
|
||||
_LayoutSizes _computeSizes({required BoxConstraints constraints, required ChildLayouter layoutChild}) {
|
||||
_LayoutSizes _computeSizes({
|
||||
required BoxConstraints constraints,
|
||||
required ChildLayouter layoutChild,
|
||||
required ChildBaselineGetter getBaseline,
|
||||
}) {
|
||||
assert(_debugHasNecessaryDirections);
|
||||
|
||||
// Determine used flex factor, size inflexible items, calculate free space.
|
||||
int totalFlex = 0;
|
||||
final double maxMainSize = _getMainSize(constraints.biggest);
|
||||
final bool canFlex = maxMainSize.isFinite;
|
||||
final BoxConstraints nonFlexChildConstraints = _constraintsForNonFlexChild(constraints);
|
||||
// Null indicates the children are not baseline aligned.
|
||||
final TextBaseline? textBaseline = _isBaselineAligned
|
||||
? (this.textBaseline ?? (throw FlutterError('To use CrossAxisAlignment.baseline, you must also specify which baseline to use using the "textBaseline" argument.')))
|
||||
: null;
|
||||
|
||||
double crossSize = 0.0;
|
||||
double allocatedSize = 0.0; // Sum of the sizes of the non-flexible children.
|
||||
RenderBox? child = firstChild;
|
||||
RenderBox? lastFlexChild;
|
||||
final bool stretched = switch (crossAxisAlignment) {
|
||||
CrossAxisAlignment.start => false,
|
||||
CrossAxisAlignment.center => false,
|
||||
CrossAxisAlignment.end => false,
|
||||
CrossAxisAlignment.baseline => false,
|
||||
CrossAxisAlignment.stretch => true,
|
||||
};
|
||||
while (child != null) {
|
||||
final FlexParentData childParentData = child.parentData! as FlexParentData;
|
||||
final int flex = _getFlex(child);
|
||||
if (flex > 0) {
|
||||
// The first pass lays out non-flex children and computes total flex.
|
||||
int totalFlex = 0;
|
||||
RenderBox? firstFlexChild;
|
||||
_AscentDescent accumulatedAscentDescent = _AscentDescent.none;
|
||||
_AxisSize accumulatedSize = _AxisSize.empty;
|
||||
for (RenderBox? child = firstChild; child != null; child = childAfter(child)) {
|
||||
final int flex;
|
||||
if (canFlex && (flex = _getFlex(child)) > 0) {
|
||||
totalFlex += flex;
|
||||
lastFlexChild = child;
|
||||
firstFlexChild ??= child;
|
||||
} else {
|
||||
final BoxConstraints innerConstraints = switch ((stretched, _direction)) {
|
||||
(true, Axis.horizontal) => BoxConstraints.tightFor(height: constraints.maxHeight),
|
||||
(true, Axis.vertical) => BoxConstraints.tightFor(width: constraints.maxWidth),
|
||||
(false, Axis.horizontal) => BoxConstraints(maxHeight: constraints.maxHeight),
|
||||
(false, Axis.vertical) => BoxConstraints(maxWidth: constraints.maxWidth),
|
||||
};
|
||||
final Size childSize = layoutChild(child, innerConstraints);
|
||||
allocatedSize += _getMainSize(childSize);
|
||||
crossSize = math.max(crossSize, _getCrossSize(childSize));
|
||||
}
|
||||
assert(child.parentData == childParentData);
|
||||
child = childParentData.nextSibling;
|
||||
}
|
||||
|
||||
// Distribute free space to flexible children.
|
||||
final double freeSpace = math.max(0.0, (canFlex ? maxMainSize : 0.0) - allocatedSize);
|
||||
double allocatedFlexSpace = 0.0;
|
||||
if (totalFlex > 0) {
|
||||
final double spacePerFlex = canFlex ? (freeSpace / totalFlex) : double.nan;
|
||||
child = firstChild;
|
||||
while (child != null) {
|
||||
final int flex = _getFlex(child);
|
||||
if (flex > 0) {
|
||||
final double maxChildExtent = switch (canFlex) {
|
||||
true when child == lastFlexChild => freeSpace - allocatedFlexSpace,
|
||||
true => spacePerFlex * flex,
|
||||
false => double.infinity,
|
||||
};
|
||||
final double minChildExtent = switch (_getFit(child)) {
|
||||
FlexFit.tight => maxChildExtent,
|
||||
FlexFit.loose => 0.0,
|
||||
};
|
||||
assert(minChildExtent.isFinite);
|
||||
final double minCrossSize = stretched ? _getCrossSize(constraints.biggest) : 0.0;
|
||||
final BoxConstraints innerConstraints = switch (_direction) {
|
||||
Axis.horizontal => constraints.copyWith(minHeight: minCrossSize, minWidth: minChildExtent, maxWidth: maxChildExtent),
|
||||
Axis.vertical => constraints.copyWith(minWidth: minCrossSize, minHeight: minChildExtent, maxHeight: maxChildExtent),
|
||||
};
|
||||
final Size childSize = layoutChild(child, innerConstraints);
|
||||
final double childMainSize = _getMainSize(childSize);
|
||||
assert(childMainSize <= maxChildExtent);
|
||||
allocatedSize += childMainSize;
|
||||
allocatedFlexSpace += maxChildExtent;
|
||||
crossSize = math.max(crossSize, _getCrossSize(childSize));
|
||||
}
|
||||
final FlexParentData childParentData = child.parentData! as FlexParentData;
|
||||
child = childParentData.nextSibling;
|
||||
final _AxisSize childSize = _AxisSize.fromSize(size: layoutChild(child, nonFlexChildConstraints), direction: direction);
|
||||
accumulatedSize += childSize;
|
||||
// Baseline-aligned children contributes to the cross axis extent separately.
|
||||
final double? baselineOffset = textBaseline == null ? null : getBaseline(child, nonFlexChildConstraints, textBaseline);
|
||||
accumulatedAscentDescent += _AscentDescent(baselineOffset: baselineOffset, crossSize: childSize.crossAxisExtent);
|
||||
}
|
||||
}
|
||||
|
||||
final double idealSize = switch (mainAxisSize) {
|
||||
MainAxisSize.max when canFlex => maxMainSize,
|
||||
MainAxisSize.max || MainAxisSize.min => allocatedSize,
|
||||
assert((totalFlex == 0) == (firstFlexChild == null));
|
||||
assert(firstFlexChild == null || canFlex); // If we are given infinite space there's no need for this extra step.
|
||||
|
||||
// The second pass distributes free space to flexible children.
|
||||
final double flexSpace = math.max(0.0, maxMainSize - accumulatedSize.mainAxisExtent);
|
||||
final double spacePerFlex = flexSpace / totalFlex;
|
||||
for (RenderBox? child = firstFlexChild; child != null && totalFlex > 0; child = childAfter(child)) {
|
||||
final int flex = _getFlex(child);
|
||||
if (flex == 0) {
|
||||
continue;
|
||||
}
|
||||
totalFlex -= flex;
|
||||
assert(spacePerFlex.isFinite);
|
||||
final double maxChildExtent = spacePerFlex * flex;
|
||||
assert(_getFit(child) == FlexFit.loose || maxChildExtent < double.infinity);
|
||||
final BoxConstraints childConstraints = _constraintsForFlexChild(child, constraints, maxChildExtent);
|
||||
final _AxisSize childSize = _AxisSize.fromSize(size: layoutChild(child, childConstraints), direction: direction);
|
||||
accumulatedSize += childSize;
|
||||
final double? baselineOffset = textBaseline == null ? null : getBaseline(child, childConstraints, textBaseline);
|
||||
accumulatedAscentDescent += _AscentDescent(baselineOffset: baselineOffset, crossSize: childSize.crossAxisExtent);
|
||||
}
|
||||
assert(totalFlex == 0);
|
||||
|
||||
// The overall height of baseline-aligned children contributes to the cross axis extent.
|
||||
accumulatedSize += switch (accumulatedAscentDescent) {
|
||||
null => _AxisSize.empty,
|
||||
(final double ascent, final double descent) => _AxisSize(mainAxisExtent: 0, crossAxisExtent: ascent + descent),
|
||||
};
|
||||
|
||||
final double idealMainSize = switch (mainAxisSize) {
|
||||
MainAxisSize.max when maxMainSize.isFinite => maxMainSize,
|
||||
MainAxisSize.max || MainAxisSize.min => accumulatedSize.mainAxisExtent,
|
||||
};
|
||||
|
||||
final _AxisSize constrainedSize = _AxisSize(mainAxisExtent: idealMainSize, crossAxisExtent: accumulatedSize.crossAxisExtent)
|
||||
.applyConstraints(constraints, direction);
|
||||
return _LayoutSizes(
|
||||
mainSize: idealSize,
|
||||
crossSize: crossSize,
|
||||
allocatedSize: allocatedSize,
|
||||
axisSize: constrainedSize,
|
||||
mainAxisFreeSpace: constrainedSize.mainAxisExtent - accumulatedSize.mainAxisExtent,
|
||||
baselineOffset: accumulatedAscentDescent.baselineOffset,
|
||||
spacePerFlex: firstFlexChild == null ? null : spacePerFlex,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void performLayout() {
|
||||
assert(_debugHasNecessaryDirections);
|
||||
final BoxConstraints constraints = this.constraints;
|
||||
assert(() {
|
||||
final FlutterError? constraintsError = _debugCheckConstraints(
|
||||
@@ -883,123 +1081,39 @@ class RenderFlex extends RenderBox with ContainerRenderObjectMixin<RenderBox, Fl
|
||||
}());
|
||||
|
||||
final _LayoutSizes sizes = _computeSizes(
|
||||
layoutChild: ChildLayoutHelper.layoutChild,
|
||||
constraints: constraints,
|
||||
layoutChild: ChildLayoutHelper.layoutChild,
|
||||
getBaseline: ChildLayoutHelper.getBaseline,
|
||||
);
|
||||
|
||||
final double allocatedSize = sizes.allocatedSize;
|
||||
double actualSize = sizes.mainSize;
|
||||
double crossSize = sizes.crossSize;
|
||||
double maxBaselineDistance = 0.0;
|
||||
if (crossAxisAlignment == CrossAxisAlignment.baseline) {
|
||||
RenderBox? child = firstChild;
|
||||
double maxSizeAboveBaseline = 0;
|
||||
double maxSizeBelowBaseline = 0;
|
||||
while (child != null) {
|
||||
assert(() {
|
||||
if (textBaseline == null) {
|
||||
throw FlutterError('To use CrossAxisAlignment.baseline, you must also specify which baseline to use using the "textBaseline" argument.');
|
||||
}
|
||||
return true;
|
||||
}());
|
||||
final double? distance = child.getDistanceToBaseline(textBaseline!, onlyReal: true);
|
||||
if (distance != null) {
|
||||
maxBaselineDistance = math.max(maxBaselineDistance, distance);
|
||||
maxSizeAboveBaseline = math.max(
|
||||
distance,
|
||||
maxSizeAboveBaseline,
|
||||
);
|
||||
maxSizeBelowBaseline = math.max(
|
||||
child.size.height - distance,
|
||||
maxSizeBelowBaseline,
|
||||
);
|
||||
crossSize = math.max(maxSizeAboveBaseline + maxSizeBelowBaseline, crossSize);
|
||||
}
|
||||
final FlexParentData childParentData = child.parentData! as FlexParentData;
|
||||
child = childParentData.nextSibling;
|
||||
}
|
||||
}
|
||||
final double crossAxisExtent = sizes.axisSize.crossAxisExtent;
|
||||
size = sizes.axisSize.toSize(direction);
|
||||
_overflow = math.max(0.0, -sizes.mainAxisFreeSpace);
|
||||
|
||||
// Align items along the main axis.
|
||||
switch (_direction) {
|
||||
case Axis.horizontal:
|
||||
size = constraints.constrain(Size(actualSize, crossSize));
|
||||
actualSize = size.width;
|
||||
crossSize = size.height;
|
||||
case Axis.vertical:
|
||||
size = constraints.constrain(Size(crossSize, actualSize));
|
||||
actualSize = size.height;
|
||||
crossSize = size.width;
|
||||
}
|
||||
final double actualSizeDelta = actualSize - allocatedSize;
|
||||
_overflow = math.max(0.0, -actualSizeDelta);
|
||||
final double remainingSpace = math.max(0.0, actualSizeDelta);
|
||||
final double betweenSpace = switch (_mainAxisAlignment) {
|
||||
MainAxisAlignment.start || MainAxisAlignment.end || MainAxisAlignment.center => 0.0,
|
||||
MainAxisAlignment.spaceBetween when childCount > 1 => remainingSpace / (childCount - 1),
|
||||
MainAxisAlignment.spaceAround when childCount > 0 => remainingSpace / childCount,
|
||||
MainAxisAlignment.spaceEvenly when childCount > 0 => remainingSpace / (childCount + 1),
|
||||
MainAxisAlignment.spaceBetween || MainAxisAlignment.spaceAround || MainAxisAlignment.spaceEvenly => 0.0,
|
||||
};
|
||||
final double leadingSpace = switch (_mainAxisAlignment) {
|
||||
MainAxisAlignment.start => 0.0,
|
||||
MainAxisAlignment.end => remainingSpace,
|
||||
MainAxisAlignment.center => remainingSpace / 2.0,
|
||||
MainAxisAlignment.spaceBetween => 0.0,
|
||||
MainAxisAlignment.spaceAround => betweenSpace / 2.0,
|
||||
MainAxisAlignment.spaceEvenly => betweenSpace,
|
||||
};
|
||||
final double remainingSpace = math.max(0.0, sizes.mainAxisFreeSpace);
|
||||
final bool flipMainAxis = _flipMainAxis;
|
||||
final bool flipCrossAxis = _flipCrossAxis;
|
||||
final (double leadingSpace, double betweenSpace) = mainAxisAlignment._distributeSpace(remainingSpace, childCount, flipMainAxis);
|
||||
final (_NextChild nextChild, RenderBox? topLeftChild) = flipMainAxis ? (childBefore, lastChild) : (childAfter, firstChild);
|
||||
final double? baselineOffset = sizes.baselineOffset;
|
||||
assert(baselineOffset == null || (crossAxisAlignment == CrossAxisAlignment.baseline && direction == Axis.horizontal));
|
||||
|
||||
// flipMainAxis is used to decide whether to lay out
|
||||
// left-to-right/top-to-bottom (false), or right-to-left/bottom-to-top
|
||||
// (true). The _startIsTopLeft will return null if there's only one child
|
||||
// and the relevant direction is null, in which case we arbitrarily decide
|
||||
// to flip, but that doesn't have any detectable effect.
|
||||
final bool flipMainAxis = !(_startIsTopLeft(direction, textDirection, verticalDirection) ?? true);
|
||||
|
||||
// Position elements
|
||||
double childMainPosition = flipMainAxis ? actualSize - leadingSpace : leadingSpace;
|
||||
RenderBox? child = firstChild;
|
||||
while (child != null) {
|
||||
// Position all chilren in visual order: starting from the top-left child and
|
||||
// work towards the child that's farthest away from the origin.
|
||||
double childMainPosition = leadingSpace;
|
||||
for (RenderBox? child = topLeftChild; child != null; child = nextChild(child)) {
|
||||
final double? childBaselineOffset;
|
||||
final bool baselineAlign = baselineOffset != null
|
||||
&& (childBaselineOffset = child.getDistanceToBaseline(textBaseline!, onlyReal: true)) != null;
|
||||
final double childCrossPosition = baselineAlign
|
||||
? baselineOffset - childBaselineOffset!
|
||||
: crossAxisAlignment._getChildCrossAxisOffset(crossAxisExtent - _getCrossSize(child.size), flipCrossAxis);
|
||||
final FlexParentData childParentData = child.parentData! as FlexParentData;
|
||||
final double childCrossPosition;
|
||||
switch (_crossAxisAlignment) {
|
||||
case CrossAxisAlignment.start:
|
||||
case CrossAxisAlignment.end:
|
||||
childCrossPosition = _startIsTopLeft(flipAxis(direction), textDirection, verticalDirection)
|
||||
== (_crossAxisAlignment == CrossAxisAlignment.start)
|
||||
? 0.0
|
||||
: crossSize - _getCrossSize(child.size);
|
||||
case CrossAxisAlignment.center:
|
||||
childCrossPosition = crossSize / 2.0 - _getCrossSize(child.size) / 2.0;
|
||||
case CrossAxisAlignment.stretch:
|
||||
childCrossPosition = 0.0;
|
||||
case CrossAxisAlignment.baseline:
|
||||
if (_direction == Axis.horizontal) {
|
||||
assert(textBaseline != null);
|
||||
final double? distance = child.getDistanceToBaseline(textBaseline!, onlyReal: true);
|
||||
if (distance != null) {
|
||||
childCrossPosition = maxBaselineDistance - distance;
|
||||
} else {
|
||||
childCrossPosition = 0.0;
|
||||
}
|
||||
} else {
|
||||
childCrossPosition = 0.0;
|
||||
}
|
||||
}
|
||||
if (flipMainAxis) {
|
||||
childMainPosition -= _getMainSize(child.size);
|
||||
}
|
||||
childParentData.offset = switch (_direction) {
|
||||
childParentData.offset = switch (direction) {
|
||||
Axis.horizontal => Offset(childMainPosition, childCrossPosition),
|
||||
Axis.vertical => Offset(childCrossPosition, childMainPosition),
|
||||
Axis.vertical => Offset(childCrossPosition, childMainPosition),
|
||||
};
|
||||
if (flipMainAxis) {
|
||||
childMainPosition -= betweenSpace;
|
||||
} else {
|
||||
childMainPosition += _getMainSize(child.size) + betweenSpace;
|
||||
}
|
||||
child = childParentData.nextSibling;
|
||||
childMainPosition += _getMainSize(child.size) + betweenSpace;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1109,15 +1223,3 @@ class RenderFlex extends RenderBox with ContainerRenderObjectMixin<RenderBox, Fl
|
||||
properties.add(EnumProperty<TextBaseline>('textBaseline', textBaseline, defaultValue: null));
|
||||
}
|
||||
}
|
||||
|
||||
class _LayoutSizes {
|
||||
const _LayoutSizes({
|
||||
required this.mainSize,
|
||||
required this.crossSize,
|
||||
required this.allocatedSize,
|
||||
});
|
||||
|
||||
final double mainSize;
|
||||
final double crossSize;
|
||||
final double allocatedSize;
|
||||
}
|
||||
|
||||
@@ -10,9 +10,18 @@ import 'box.dart';
|
||||
/// that the [RenderBox] would have if it were laid out with the given
|
||||
/// [BoxConstraints].
|
||||
///
|
||||
/// The methods of [ChildLayoutHelper] adhere to this signature.
|
||||
/// [ChildLayoutHelper.dryLayoutChild] and [ChildLayoutHelper.layoutChild] adhere
|
||||
/// to this signature.
|
||||
typedef ChildLayouter = Size Function(RenderBox child, BoxConstraints constraints);
|
||||
|
||||
/// Signature for a function that takes a [RenderBox] and returns the baseline
|
||||
/// offset this [RenderBox] would have if it were laid out with the given
|
||||
/// [BoxConstraints].
|
||||
///
|
||||
/// [ChildLayoutHelper.getDryBaseline] and [ChildLayoutHelper.getBaseline] adhere
|
||||
/// to this signature.
|
||||
typedef ChildBaselineGetter = double? Function(RenderBox child, BoxConstraints constraints, TextBaseline baseline);
|
||||
|
||||
/// A collection of static functions to layout a [RenderBox] child with the
|
||||
/// given set of [BoxConstraints].
|
||||
///
|
||||
@@ -52,4 +61,18 @@ abstract final class ChildLayoutHelper {
|
||||
child.layout(constraints, parentUsesSize: true);
|
||||
return child.size;
|
||||
}
|
||||
|
||||
/// Convenience function that calls [RenderBox.getDryBaseline].
|
||||
static double? getDryBaseline(RenderBox child, BoxConstraints constraints, TextBaseline baseline) {
|
||||
return child.getDryBaseline(constraints, baseline);
|
||||
}
|
||||
|
||||
/// Convenience function that calls [RenderBox.getDistanceToBaseline].
|
||||
///
|
||||
/// The given `child` must be already laid out with `constraints`.
|
||||
static double? getBaseline(RenderBox child, BoxConstraints constraints, TextBaseline baseline) {
|
||||
assert(!child.debugNeedsLayout);
|
||||
assert(child.constraints == constraints);
|
||||
return child.getDistanceToBaseline(baseline, onlyReal: true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -273,6 +273,11 @@ class RenderConstrainedBox extends RenderProxyBox {
|
||||
return height;
|
||||
}
|
||||
|
||||
@override
|
||||
double? computeDryBaseline(covariant BoxConstraints constraints, TextBaseline baseline) {
|
||||
return child?.getDryBaseline(_additionalConstraints.enforce(constraints), baseline);
|
||||
}
|
||||
|
||||
@override
|
||||
void performLayout() {
|
||||
final BoxConstraints constraints = this.constraints;
|
||||
@@ -842,7 +847,12 @@ class RenderIgnoreBaseline extends RenderProxyBox {
|
||||
}) : super(child);
|
||||
|
||||
@override
|
||||
double? computeDistanceToActualBaseline(TextBaseline baseline) {
|
||||
Null computeDistanceToActualBaseline(TextBaseline baseline) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@override
|
||||
Null computeDryBaseline(covariant BoxConstraints constraints, TextBaseline baseline) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ import 'box.dart';
|
||||
import 'debug.dart';
|
||||
import 'debug_overflow_indicator.dart';
|
||||
import 'layer.dart';
|
||||
import 'layout_helper.dart';
|
||||
import 'object.dart';
|
||||
import 'stack.dart' show RelativeRect;
|
||||
|
||||
@@ -1419,33 +1420,40 @@ class RenderBaseline extends RenderShiftedBox {
|
||||
markNeedsLayout();
|
||||
}
|
||||
|
||||
({Size size, double top}) _computeSizes(covariant BoxConstraints constraints, ChildLayouter layoutChild, ChildBaselineGetter getBaseline) {
|
||||
final RenderBox? child = this.child;
|
||||
if (child == null) {
|
||||
return (size: constraints.smallest, top: 0);
|
||||
}
|
||||
final BoxConstraints childConstraints = constraints.loosen();
|
||||
final Size childSize = layoutChild(child, childConstraints);
|
||||
final double childBaseline = getBaseline(child, childConstraints, baselineType) ?? childSize.height;
|
||||
final double top = baseline - childBaseline;
|
||||
return (size: constraints.constrain(Size(childSize.width, top + childSize.height)), top: top);
|
||||
}
|
||||
|
||||
@override
|
||||
@protected
|
||||
Size computeDryLayout(covariant BoxConstraints constraints) {
|
||||
if (child != null) {
|
||||
assert(debugCannotComputeDryLayout(
|
||||
reason: 'Baseline metrics are only available after a full layout.',
|
||||
));
|
||||
return Size.zero;
|
||||
return _computeSizes(constraints, ChildLayoutHelper.dryLayoutChild, ChildLayoutHelper.getDryBaseline).size;
|
||||
}
|
||||
|
||||
@override
|
||||
double? computeDryBaseline(covariant BoxConstraints constraints, TextBaseline baseline) {
|
||||
final RenderBox? child = this.child;
|
||||
final double? result1 = child?.getDryBaseline(constraints.loosen(), baseline);
|
||||
final double? result2 = child?.getDryBaseline(constraints.loosen(), baselineType);
|
||||
if (result1 == null || result2 == null) {
|
||||
return null;
|
||||
}
|
||||
return constraints.smallest;
|
||||
return this.baseline + result1 - result2;
|
||||
}
|
||||
|
||||
@override
|
||||
void performLayout() {
|
||||
if (child != null) {
|
||||
final BoxConstraints constraints = this.constraints;
|
||||
child!.layout(constraints.loosen(), parentUsesSize: true);
|
||||
final double childBaseline = child!.getDistanceToBaseline(baselineType)!;
|
||||
final double actualBaseline = baseline;
|
||||
final double top = actualBaseline - childBaseline;
|
||||
final BoxParentData childParentData = child!.parentData! as BoxParentData;
|
||||
childParentData.offset = Offset(0.0, top);
|
||||
final Size childSize = child!.size;
|
||||
size = constraints.constrain(Size(childSize.width, top + childSize.height));
|
||||
} else {
|
||||
size = constraints.smallest;
|
||||
}
|
||||
final (:Size size, :double top) = _computeSizes(constraints, ChildLayoutHelper.layoutChild, ChildLayoutHelper.getBaseline);
|
||||
this.size = size;
|
||||
(child?.parentData as BoxParentData?)?.offset = Offset(0.0, top);
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
@@ -53,6 +53,33 @@ void main() {
|
||||
expect(parent.size, equals(const Size(100.0, 110.0)));
|
||||
});
|
||||
|
||||
test('RenderBaseline different baseline types', () {
|
||||
final _RenderBaselineTester child = _RenderBaselineTester()
|
||||
..boxSize = const Size.square(100)
|
||||
..alphabeticBaselineOffset = 50
|
||||
..ideographicBaselineOffset = 60;
|
||||
final RenderBaseline renderBaseline = RenderBaseline(
|
||||
baseline: 1.0,
|
||||
baselineType: TextBaseline.alphabetic,
|
||||
child: child,
|
||||
);
|
||||
|
||||
layout(renderBaseline, phase: EnginePhase.paint);
|
||||
expect(renderBaseline.getDryBaseline(const BoxConstraints(), TextBaseline.alphabetic), 1.0);
|
||||
expect(renderBaseline.getDryBaseline(const BoxConstraints(), TextBaseline.ideographic), 11.0);
|
||||
|
||||
child
|
||||
..alphabeticBaselineOffset = null
|
||||
..ideographicBaselineOffset = null
|
||||
..markNeedsLayout(); // Clears baseline cache.
|
||||
|
||||
renderBaseline.markNeedsLayout();
|
||||
|
||||
pumpFrame(phase: EnginePhase.paint);
|
||||
expect(renderBaseline.getDryBaseline(const BoxConstraints(), TextBaseline.alphabetic), isNull);
|
||||
expect(renderBaseline.getDryBaseline(const BoxConstraints(), TextBaseline.ideographic), isNull);
|
||||
});
|
||||
|
||||
test('RenderFlex and RenderIgnoreBaseline (control test -- with baseline)', () {
|
||||
final RenderBox a, b;
|
||||
final RenderBox root = RenderFlex(
|
||||
@@ -105,3 +132,33 @@ void main() {
|
||||
expect(bPos.dy, 0.0);
|
||||
});
|
||||
}
|
||||
|
||||
class _RenderBaselineTester extends RenderBox {
|
||||
Size boxSize = Size.zero;
|
||||
double? alphabeticBaselineOffset;
|
||||
double? ideographicBaselineOffset;
|
||||
|
||||
@override
|
||||
double? computeDistanceToActualBaseline(TextBaseline baseline) {
|
||||
return switch (baseline) {
|
||||
TextBaseline.alphabetic => alphabeticBaselineOffset,
|
||||
TextBaseline.ideographic => ideographicBaselineOffset,
|
||||
};
|
||||
}
|
||||
|
||||
@override
|
||||
double? computeDryBaseline(covariant BoxConstraints constraints, TextBaseline baseline) {
|
||||
return switch (baseline) {
|
||||
TextBaseline.alphabetic => alphabeticBaselineOffset,
|
||||
TextBaseline.ideographic => ideographicBaselineOffset,
|
||||
};
|
||||
}
|
||||
|
||||
@override
|
||||
Size computeDryLayout(covariant BoxConstraints constraints) => constraints.constrain(boxSize);
|
||||
|
||||
@override
|
||||
void performLayout() {
|
||||
size = computeDryLayout(constraints);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,7 +32,9 @@ class RenderTestBox extends RenderBox {
|
||||
|
||||
@override
|
||||
double? computeDistanceToActualBaseline(TextBaseline baseline) {
|
||||
calls += 1;
|
||||
if (!RenderObject.debugCheckingIntrinsics) {
|
||||
calls += 1;
|
||||
}
|
||||
return boxSize.height / 2.0;
|
||||
}
|
||||
}
|
||||
@@ -42,7 +44,9 @@ class RenderDryBaselineTestBox extends RenderTestBox {
|
||||
|
||||
@override
|
||||
double? computeDryBaseline(covariant BoxConstraints constraints, TextBaseline baseline) {
|
||||
calls += 1;
|
||||
if (!RenderObject.debugCheckingIntrinsics) {
|
||||
calls += 1;
|
||||
}
|
||||
return baselineOverride ?? constraints.biggest.height / 2.0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:math' as math;
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/rendering.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
@@ -623,30 +625,192 @@ void main() {
|
||||
expect(box3.size, const Size(100.0, 100.0));
|
||||
});
|
||||
|
||||
test('Intrinsics throw if alignment is baseline', () {
|
||||
final RenderDecoratedBox box = RenderDecoratedBox(
|
||||
decoration: const BoxDecoration(),
|
||||
);
|
||||
test('children with no baselines are top-aligned', () {
|
||||
const BoxConstraints square = BoxConstraints.tightFor(width: 100.0, height: 100.0);
|
||||
final RenderConstrainedBox box1 = RenderConstrainedBox(additionalConstraints: square);
|
||||
final RenderConstrainedBox box2 = RenderConstrainedBox(additionalConstraints: square);
|
||||
final RenderFlex flex = RenderFlex(
|
||||
textDirection: TextDirection.ltr,
|
||||
children: <RenderBox>[box],
|
||||
children: <RenderBox>[box1, box2],
|
||||
crossAxisAlignment: CrossAxisAlignment.baseline,
|
||||
textBaseline: TextBaseline.alphabetic,
|
||||
verticalDirection: VerticalDirection.up,
|
||||
);
|
||||
layout(flex, constraints: const BoxConstraints(
|
||||
minWidth: 200.0, maxWidth: 200.0, minHeight: 200.0, maxHeight: 200.0,
|
||||
));
|
||||
layout(flex);
|
||||
|
||||
final Matcher cannotCalculateIntrinsics = throwsA(isAssertionError.having(
|
||||
(AssertionError e) => e.message,
|
||||
'message',
|
||||
'Intrinsics are not available for CrossAxisAlignment.baseline.',
|
||||
));
|
||||
// Not start-aligned.
|
||||
expect(box1.localToGlobal(Offset.zero).dy, 0.0);
|
||||
expect(box2.localToGlobal(Offset.zero).dy, 0.0);
|
||||
|
||||
expect(() => flex.getMaxIntrinsicHeight(100), cannotCalculateIntrinsics);
|
||||
expect(() => flex.getMinIntrinsicHeight(100), cannotCalculateIntrinsics);
|
||||
expect(() => flex.getMaxIntrinsicWidth(100), cannotCalculateIntrinsics);
|
||||
expect(() => flex.getMinIntrinsicWidth(100), cannotCalculateIntrinsics);
|
||||
flex.verticalDirection = VerticalDirection.down;
|
||||
pumpFrame();
|
||||
expect(box1.localToGlobal(Offset.zero).dy, 0.0);
|
||||
expect(box2.localToGlobal(Offset.zero).dy, 0.0);
|
||||
});
|
||||
|
||||
group('Intrinsics', () {
|
||||
test('main axis intrinsics with RenderAspectRatio 1', () {
|
||||
const BoxConstraints square = BoxConstraints.tightFor(width: 100.0, height: 100.0);
|
||||
final RenderConstrainedBox box1 = RenderConstrainedBox(additionalConstraints: square);
|
||||
final RenderConstrainedBox box2 = RenderConstrainedBox(additionalConstraints: square);
|
||||
final RenderAspectRatio box3 = RenderAspectRatio(aspectRatio: 1.0, child: RenderConstrainedBox(additionalConstraints: square));
|
||||
final RenderFlex flex = RenderFlex(
|
||||
textDirection: TextDirection.ltr,
|
||||
);
|
||||
flex.addAll(<RenderBox>[box1, box2, box3]);
|
||||
final FlexParentData box2ParentData = box2.parentData! as FlexParentData;
|
||||
box2ParentData.flex = 1;
|
||||
box2ParentData.fit = FlexFit.tight; // In intrinsics FlexFit.tight should have no effect.
|
||||
|
||||
expect(flex.getMinIntrinsicWidth(double.infinity), 300.0);
|
||||
expect(flex.getMaxIntrinsicWidth(double.infinity), 300.0);
|
||||
|
||||
expect(flex.getMinIntrinsicWidth(300.0), 200.0 + 300.0);
|
||||
expect(flex.getMaxIntrinsicWidth(300.0), 200.0 + 300.0);
|
||||
|
||||
expect(flex.getMinIntrinsicWidth(500.0), 200.0 + 500.0);
|
||||
expect(flex.getMaxIntrinsicWidth(500.0), 200.0 + 500.0);
|
||||
});
|
||||
|
||||
test('cross axis intrinsics, with ascending flex flow layout', () {
|
||||
const BoxConstraints square = BoxConstraints.tightFor(width: 5.0, height: 5.0);
|
||||
// 3 'A's separated by zero-width spaces. Max instrinsic width = 30, min intrinsic width = 10
|
||||
final TextSpan textSpan = TextSpan(text: List<String>.filled(3, 'A').join('\u200B') , style: const TextStyle(fontSize: 10));
|
||||
final RenderConstrainedBox box1 = RenderConstrainedBox(additionalConstraints: square);
|
||||
final RenderParagraph box2 = RenderParagraph(textSpan, textDirection: TextDirection.ltr);
|
||||
final RenderParagraph box3 = RenderParagraph(textSpan, textDirection: TextDirection.ltr);
|
||||
final RenderFlex flex = RenderFlex(textDirection: TextDirection.ltr);
|
||||
|
||||
flex.addAll(<RenderBox>[box1, box2, box3]);
|
||||
final FlexParentData box2ParentData = box2.parentData! as FlexParentData;
|
||||
box2ParentData.flex = 1;
|
||||
box2ParentData.fit = FlexFit.tight; // In intrinsics FlexFit.tight should have no effect.
|
||||
final FlexParentData box3ParentData = box3.parentData! as FlexParentData;
|
||||
box3ParentData.flex = 2;
|
||||
box3ParentData.fit = FlexFit.tight; // In intrinsics FlexFit.tight should have no effect.
|
||||
|
||||
expect(flex.getMinIntrinsicHeight(double.infinity), 10.0);
|
||||
expect(flex.getMaxIntrinsicHeight(double.infinity), 10.0);
|
||||
|
||||
// 95.0 is the max intrinsic width of the RenderFlex.
|
||||
// width distribution = 5, 30, 60.
|
||||
expect(flex.getMinIntrinsicHeight(95.0), 10.0);
|
||||
expect(flex.getMaxIntrinsicHeight(95.0), 10.0);
|
||||
|
||||
expect(flex.getMinIntrinsicHeight(94.0), 20.0);
|
||||
expect(flex.getMaxIntrinsicHeight(94.0), 20.0);
|
||||
|
||||
// width distribution = 5, 20, 40
|
||||
expect(flex.getMinIntrinsicHeight(65.0), 20.0);
|
||||
expect(flex.getMaxIntrinsicHeight(65.0), 20.0);
|
||||
|
||||
// width distribution = 5, 10, 20
|
||||
expect(flex.getMinIntrinsicHeight(35.0), 30.0);
|
||||
expect(flex.getMaxIntrinsicHeight(35.0), 30.0);
|
||||
});
|
||||
|
||||
test('cross axis intrinsics, with descending flex flow layout', () {
|
||||
const BoxConstraints square = BoxConstraints.tightFor(width: 5.0, height: 5.0);
|
||||
// 3 'A's separated by zero-width spaces. Max instrinsic width = 30, min intrinsic width = 10
|
||||
final TextSpan textSpan = TextSpan(text: List<String>.filled(3, 'A').join('\u200B') , style: const TextStyle(fontSize: 10));
|
||||
final RenderConstrainedBox box1 = RenderConstrainedBox(additionalConstraints: square);
|
||||
final RenderParagraph box2 = RenderParagraph(textSpan, textDirection: TextDirection.ltr);
|
||||
final RenderParagraph box3 = RenderParagraph(textSpan, textDirection: TextDirection.ltr);
|
||||
final RenderFlex flex = RenderFlex(textDirection: TextDirection.ltr);
|
||||
|
||||
flex.addAll(<RenderBox>[box1, box2, box3]);
|
||||
final FlexParentData box2ParentData = box2.parentData! as FlexParentData;
|
||||
box2ParentData.flex = 2;
|
||||
box2ParentData.fit = FlexFit.tight; // In intrinsics FlexFit.tight should have no effect.
|
||||
final FlexParentData box3ParentData = box3.parentData! as FlexParentData;
|
||||
box3ParentData.flex = 1;
|
||||
box3ParentData.fit = FlexFit.tight; // In intrinsics FlexFit.tight should have no effect.
|
||||
|
||||
// The setup is exactly the same as the previous test, but the flex factors
|
||||
// are swapped.
|
||||
expect(flex.getMinIntrinsicHeight(double.infinity), 10.0);
|
||||
expect(flex.getMaxIntrinsicHeight(double.infinity), 10.0);
|
||||
|
||||
// 95.0 is the max intrinsic width of the RenderFlex.
|
||||
expect(flex.getMinIntrinsicHeight(95.0), 10.0);
|
||||
expect(flex.getMaxIntrinsicHeight(95.0), 10.0);
|
||||
|
||||
// width distribution = 5, 40, 20.
|
||||
expect(flex.getMinIntrinsicHeight(65.0), 20.0);
|
||||
expect(flex.getMaxIntrinsicHeight(65.0), 20.0);
|
||||
|
||||
// width distribution = 5, 20, 10
|
||||
expect(flex.getMinIntrinsicHeight(35.0), 30.0);
|
||||
expect(flex.getMaxIntrinsicHeight(35.0), 30.0);
|
||||
});
|
||||
|
||||
test('baseline aligned flex flow computeDryLayout', () {
|
||||
// box1 has its baseline placed at the top of the box.
|
||||
final RenderFlowBaselineTestBox box1 = RenderFlowBaselineTestBox()
|
||||
..baselinePlacer = ((double height) => 0.0)
|
||||
..gridCount = 10;
|
||||
|
||||
// box2 has its baseline placed at the bottom of the box.
|
||||
final RenderFlowBaselineTestBox box2 = RenderFlowBaselineTestBox()
|
||||
..baselinePlacer = ((double height) => height)
|
||||
..gridCount = 10;
|
||||
|
||||
final RenderFlex flex = RenderFlex(
|
||||
textDirection: TextDirection.ltr,
|
||||
textBaseline: TextBaseline.alphabetic,
|
||||
crossAxisAlignment: CrossAxisAlignment.baseline,
|
||||
children: <RenderBox>[box1, box2],
|
||||
);
|
||||
final FlexParentData box1ParentData = box1.parentData! as FlexParentData;
|
||||
box1ParentData.flex = 2;
|
||||
box1ParentData.fit = FlexFit.tight;
|
||||
final FlexParentData box2ParentData = box2.parentData! as FlexParentData;
|
||||
box2ParentData.flex = 1;
|
||||
box2ParentData.fit = FlexFit.loose;
|
||||
|
||||
Size size = const Size(200, 100);
|
||||
// box 1 one line, box 2 two lines.
|
||||
expect(flex.getDryLayout(BoxConstraints.loose(size)), const Size(200.0, 30.0));
|
||||
expect(flex.getDryBaseline(BoxConstraints.loose(size), TextBaseline.alphabetic), 20.0);
|
||||
size = const Size(300, 100);
|
||||
// box 1 one line, box 2 one.
|
||||
expect(flex.getDryLayout(BoxConstraints.loose(size)), const Size(300.0, 20.0));
|
||||
expect(flex.getDryBaseline(BoxConstraints.loose(size), TextBaseline.alphabetic), 10.0);
|
||||
});
|
||||
|
||||
test('children with no baselines do not affect the baseline location', () {
|
||||
// box1 has its baseline placed at the bottom of the box.
|
||||
final RenderFlowBaselineTestBox box1 = RenderFlowBaselineTestBox()
|
||||
..baselinePlacer = ((double height) => height)
|
||||
..gridCount = 10;
|
||||
|
||||
// box2 has its baseline placed at the bottom of the box.
|
||||
final RenderFlowBaselineTestBox box2 = RenderFlowBaselineTestBox()
|
||||
..baselinePlacer = ((double height) => null)
|
||||
..gridCount = 10;
|
||||
|
||||
final RenderFlex flex = RenderFlex(
|
||||
textDirection: TextDirection.ltr,
|
||||
textBaseline: TextBaseline.alphabetic,
|
||||
crossAxisAlignment: CrossAxisAlignment.baseline,
|
||||
children: <RenderBox>[box1, box2],
|
||||
);
|
||||
final FlexParentData box1ParentData = box1.parentData! as FlexParentData;
|
||||
box1ParentData.flex = 2;
|
||||
box1ParentData.fit = FlexFit.tight;
|
||||
final FlexParentData box2ParentData = box2.parentData! as FlexParentData;
|
||||
box2ParentData.flex = 1;
|
||||
box2ParentData.fit = FlexFit.loose;
|
||||
|
||||
Size size = const Size(200, 100);
|
||||
// box 1 one line, box 2 two lines.
|
||||
expect(flex.getDryLayout(BoxConstraints.loose(size)), const Size(200.0, 20.0));
|
||||
expect(flex.getDryBaseline(BoxConstraints.loose(size), TextBaseline.alphabetic), 10.0);
|
||||
size = const Size(300, 100);
|
||||
// box 1 one line, box 2 one.
|
||||
expect(flex.getDryLayout(BoxConstraints.loose(size)), const Size(300.0, 10.0));
|
||||
expect(flex.getDryBaseline(BoxConstraints.loose(size), TextBaseline.alphabetic), 10.0);
|
||||
});
|
||||
});
|
||||
|
||||
test('Can call methods that check overflow even if overflow value is not set', () {
|
||||
@@ -666,3 +830,43 @@ void main() {
|
||||
expect(exceptions.first.message, isNot(contains('Null check operator')));
|
||||
});
|
||||
}
|
||||
|
||||
class RenderFlowBaselineTestBox extends RenderBox {
|
||||
static const Size gridSize = Size(10, 10);
|
||||
int gridCount = 0;
|
||||
|
||||
int lineGridCount(double width) {
|
||||
final int gridsPerLine = width >= gridCount * gridSize.width
|
||||
? gridCount
|
||||
: width ~/ gridSize.width;
|
||||
return math.max(1, gridsPerLine);
|
||||
}
|
||||
int lineCount(double width) => (gridCount / lineGridCount(width)).ceil();
|
||||
|
||||
double? Function(double height) baselinePlacer = (double height) => null;
|
||||
|
||||
@override
|
||||
double computeMinIntrinsicWidth(double height) => gridSize.width;
|
||||
@override
|
||||
double computeMaxIntrinsicWidth(double height) => gridSize.width * gridCount;
|
||||
@override
|
||||
double computeMinIntrinsicHeight(double width) => gridSize.height * lineCount(width);
|
||||
@override
|
||||
double computeMaxIntrinsicHeight(double width) => computeMinIntrinsicHeight(width);
|
||||
@override
|
||||
Size computeDryLayout(covariant BoxConstraints constraints) {
|
||||
return constraints.constrain(Size(
|
||||
gridSize.width * lineGridCount(constraints.maxWidth),
|
||||
gridSize.height * lineCount(constraints.maxWidth),
|
||||
));
|
||||
}
|
||||
@override
|
||||
double? computeDryBaseline(covariant BoxConstraints constraints, TextBaseline baseline) => baselinePlacer(getDryLayout(constraints).height);
|
||||
@override
|
||||
double? computeDistanceToActualBaseline(TextBaseline baseline) => baselinePlacer(size.height);
|
||||
|
||||
@override
|
||||
void performLayout() {
|
||||
size = computeDryLayout(constraints);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user