1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
| class RenderFlex {
@override void performLayout() { final BoxConstraints constraints = this.constraints;
final _LayoutSizes sizes = _computeSizes( layoutChild: ChildLayoutHelper.layoutChild, constraints: constraints, );
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) { 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; } }
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); late final double leadingSpace; late final double betweenSpace; final bool flipMainAxis = !(_startIsTopLeft(direction, textDirection, verticalDirection) ?? true); switch (_mainAxisAlignment) { case MainAxisAlignment.start: leadingSpace = 0.0; betweenSpace = 0.0; case MainAxisAlignment.end: leadingSpace = remainingSpace; betweenSpace = 0.0; case MainAxisAlignment.center: leadingSpace = remainingSpace / 2.0; betweenSpace = 0.0; case MainAxisAlignment.spaceBetween: leadingSpace = 0.0; betweenSpace = childCount > 1 ? remainingSpace / (childCount - 1) : 0.0; case MainAxisAlignment.spaceAround: betweenSpace = childCount > 0 ? remainingSpace / childCount : 0.0; leadingSpace = betweenSpace / 2.0; case MainAxisAlignment.spaceEvenly: betweenSpace = childCount > 0 ? remainingSpace / (childCount + 1) : 0.0; leadingSpace = betweenSpace; }
double childMainPosition = flipMainAxis ? actualSize - leadingSpace : leadingSpace; RenderBox? child = firstChild; while (child != null) { 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); } switch (_direction) { case Axis.horizontal: childParentData.offset = Offset(childMainPosition, childCrossPosition); case Axis.vertical: childParentData.offset = Offset(childCrossPosition, childMainPosition); } if (flipMainAxis) { childMainPosition -= betweenSpace; } else { childMainPosition += _getMainSize(child.size) + betweenSpace; } child = childParentData.nextSibling; } }
_LayoutSizes _computeSizes({required BoxConstraints constraints, required ChildLayouter layoutChild}) { int totalFlex = 0; final double maxMainSize = _direction == Axis.horizontal ? constraints.maxWidth : constraints.maxHeight; final bool canFlex = maxMainSize < double.infinity;
double crossSize = 0.0; double allocatedSize = 0.0; RenderBox? child = firstChild; RenderBox? lastFlexChild; while (child != null) { final FlexParentData childParentData = child.parentData! as FlexParentData; final int flex = _getFlex(child); if (flex > 0) { totalFlex += flex; lastFlexChild = child; } else { final BoxConstraints innerConstraints; if (crossAxisAlignment == CrossAxisAlignment.stretch) { switch (_direction) { case Axis.horizontal: innerConstraints = BoxConstraints.tightFor(height: constraints.maxHeight); case Axis.vertical: innerConstraints = BoxConstraints.tightFor(width: constraints.maxWidth); } } else { switch (_direction) { case Axis.horizontal: innerConstraints = BoxConstraints(maxHeight: constraints.maxHeight); case Axis.vertical: innerConstraints = BoxConstraints(maxWidth: constraints.maxWidth); } } final Size childSize = layoutChild(child, innerConstraints); allocatedSize += _getMainSize(childSize); crossSize = math.max(crossSize, _getCrossSize(childSize)); } child = childParentData.nextSibling; }
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 = canFlex ? (child == lastFlexChild ? (freeSpace - allocatedFlexSpace) : spacePerFlex * flex) : double.infinity; late final double minChildExtent; switch (_getFit(child)) { case FlexFit.tight: assert(maxChildExtent < double.infinity); minChildExtent = maxChildExtent; case FlexFit.loose: minChildExtent = 0.0; } final BoxConstraints innerConstraints; if (crossAxisAlignment == CrossAxisAlignment.stretch) { switch (_direction) { case Axis.horizontal: innerConstraints = BoxConstraints( minWidth: minChildExtent, maxWidth: maxChildExtent, minHeight: constraints.maxHeight, maxHeight: constraints.maxHeight, ); case Axis.vertical: innerConstraints = BoxConstraints( minWidth: constraints.maxWidth, maxWidth: constraints.maxWidth, minHeight: minChildExtent, maxHeight: maxChildExtent, ); } } else { switch (_direction) { case Axis.horizontal: innerConstraints = BoxConstraints( minWidth: minChildExtent, maxWidth: maxChildExtent, maxHeight: constraints.maxHeight, ); case Axis.vertical: innerConstraints = BoxConstraints( maxWidth: constraints.maxWidth, 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 double idealSize = canFlex && mainAxisSize == MainAxisSize.max ? maxMainSize : allocatedSize; return _LayoutSizes( mainSize: idealSize, crossSize: crossSize, allocatedSize: allocatedSize, ); }
}
|