@@ -215,19 +215,19 @@ enum MainAxisAlignment {
215
215
/// after the first and last child.
216
216
spaceEvenly;
217
217
218
- (double leadingSpace, double betweenSpace) _distributeSpace (double freeSpace, int itemCount, bool flipped) {
218
+ (double leadingSpace, double betweenSpace) _distributeSpace (double freeSpace, int itemCount, bool flipped, double spacing ) {
219
219
assert (itemCount >= 0 );
220
220
return switch (this ) {
221
- MainAxisAlignment .start => flipped ? (freeSpace, 0.0 ) : (0.0 , 0.0 ),
221
+ MainAxisAlignment .start => flipped ? (freeSpace, spacing ) : (0.0 , spacing ),
222
222
223
- MainAxisAlignment .end => MainAxisAlignment .start._distributeSpace (freeSpace, itemCount, ! flipped),
224
- MainAxisAlignment .spaceBetween when itemCount < 2 => MainAxisAlignment .start._distributeSpace (freeSpace, itemCount, flipped),
225
- MainAxisAlignment .spaceAround when itemCount == 0 => MainAxisAlignment .start._distributeSpace (freeSpace, itemCount, flipped),
223
+ MainAxisAlignment .end => MainAxisAlignment .start._distributeSpace (freeSpace, itemCount, ! flipped, spacing ),
224
+ MainAxisAlignment .spaceBetween when itemCount < 2 => MainAxisAlignment .start._distributeSpace (freeSpace, itemCount, flipped, spacing ),
225
+ MainAxisAlignment .spaceAround when itemCount == 0 => MainAxisAlignment .start._distributeSpace (freeSpace, itemCount, flipped, spacing ),
226
226
227
- MainAxisAlignment .center => (freeSpace / 2.0 , 0.0 ),
228
- MainAxisAlignment .spaceBetween => (0.0 , freeSpace / (itemCount - 1 )),
229
- MainAxisAlignment .spaceAround => (freeSpace / itemCount / 2 , freeSpace / itemCount),
230
- MainAxisAlignment .spaceEvenly => (freeSpace / (itemCount + 1 ), freeSpace / (itemCount + 1 )),
227
+ MainAxisAlignment .center => (freeSpace / 2.0 , spacing ),
228
+ MainAxisAlignment .spaceBetween => (0.0 , freeSpace / (itemCount - 1 ) + spacing ),
229
+ MainAxisAlignment .spaceAround => (freeSpace / itemCount / 2 , freeSpace / itemCount + spacing ),
230
+ MainAxisAlignment .spaceEvenly => (freeSpace / (itemCount + 1 ), freeSpace / (itemCount + 1 ) + spacing ),
231
231
};
232
232
}
233
233
}
@@ -390,14 +390,17 @@ class RenderFlex extends RenderBox with ContainerRenderObjectMixin<RenderBox, Fl
390
390
VerticalDirection verticalDirection = VerticalDirection .down,
391
391
TextBaseline ? textBaseline,
392
392
Clip clipBehavior = Clip .none,
393
+ double spacing = 0.0 ,
393
394
}) : _direction = direction,
394
395
_mainAxisAlignment = mainAxisAlignment,
395
396
_mainAxisSize = mainAxisSize,
396
397
_crossAxisAlignment = crossAxisAlignment,
397
398
_textDirection = textDirection,
398
399
_verticalDirection = verticalDirection,
399
400
_textBaseline = textBaseline,
400
- _clipBehavior = clipBehavior {
401
+ _clipBehavior = clipBehavior,
402
+ _spacing = spacing,
403
+ assert (spacing >= 0.0 ) {
401
404
addAll (children);
402
405
}
403
406
@@ -588,6 +591,69 @@ class RenderFlex extends RenderBox with ContainerRenderObjectMixin<RenderBox, Fl
588
591
}
589
592
}
590
593
594
+ /// {@template flutter.rendering.RenderFlex.spacing}
595
+ /// How much space to place between children in the main axis.
596
+ ///
597
+ /// The spacing is only applied between children in the main axis.
598
+ ///
599
+ /// If the [spacing] is 10.0 and the [mainAxisAlignment] is
600
+ /// [MainAxisAlignment.start] , then the first child will be placed at the start
601
+ /// of the main axis, and the second child will be placed 10.0 pixels after
602
+ /// the first child in the main axis, and so on. The [spacing] is not applied
603
+ /// before the first child or after the last child.
604
+ ///
605
+ /// If the [spacing] is 10.0 and the [mainAxisAlignment] is [MainAxisAlignment.end] ,
606
+ /// then the last child will be placed at the end of the main axis, and the
607
+ /// second-to-last child will be placed 10.0 pixels before the last child in
608
+ /// the main axis, and so on. The [spacing] is not applied before the first
609
+ /// child or after the last child.
610
+ ///
611
+ /// If the [spacing] is 10.0 and the [mainAxisAlignment] is [MainAxisAlignment.center] ,
612
+ /// then the children will be placed in the center of the main axis with 10.0
613
+ /// pixels of space between the children. The [spacing] is not applied before the first
614
+ /// child or after the last child.
615
+ ///
616
+ /// If the [spacing] is 10.0 and the [mainAxisAlignment] is [MainAxisAlignment.spaceBetween] ,
617
+ /// then there will be a minimum of 10.0 pixels of space between each child in the
618
+ /// main axis. If the free space is 100.0 pixels between the two children,
619
+ /// then the minimum space between the children will be 10.0 pixels and the
620
+ /// remaining 90.0 pixels will be the free space between the children. The
621
+ /// [spacing] is not applied before the first child or after the last child.
622
+ ///
623
+ /// If the [spacing] is 10.0 and the [mainAxisAlignment] is [MainAxisAlignment.spaceAround] ,
624
+ /// then there will be a minimum of 10.0 pixels of space between each child in the
625
+ /// main axis, and the remaining free space will be placed between the children as
626
+ /// well as before the first child and after the last child. The [spacing] is
627
+ /// not applied before the first child or after the last child.
628
+ ///
629
+ /// If the [spacing] is 10.0 and the [mainAxisAlignment] is [MainAxisAlignment.spaceEvenly] ,
630
+ /// then there will be a minimum of 10.0 pixels of space between each child in the
631
+ /// main axis, and the remaining free space will be evenly placed between the
632
+ /// children as well as before the first child and after the last child. The
633
+ /// [spacing] is not applied before the first child or after the last child.
634
+ ///
635
+ /// When the [spacing] is non-zero, the layout size will be larger than
636
+ /// the sum of the children's layout sizes in the main axis.
637
+ ///
638
+ /// When the total children's layout sizes and total spacing between the
639
+ /// children is greater than the maximum constraints in the main axis, then
640
+ /// the children will overflow. For example, if there are two children and the
641
+ /// maximum constraint is 100.0 pixels, the children's layout sizes are 50.0
642
+ /// pixels each, and the spacing is 10.0 pixels, then the children will
643
+ /// overflow by 10.0 pixels.
644
+ ///
645
+ /// Defaults to 0.0.
646
+ /// {@endtemplate}
647
+ double get spacing => _spacing;
648
+ double _spacing;
649
+ set spacing (double value) {
650
+ if (_spacing == value) {
651
+ return ;
652
+ }
653
+ _spacing = value;
654
+ markNeedsLayout ();
655
+ }
656
+
591
657
@override
592
658
void setupParentData (RenderBox child) {
593
659
if (child.parentData is ! FlexParentData ) {
@@ -597,15 +663,15 @@ class RenderFlex extends RenderBox with ContainerRenderObjectMixin<RenderBox, Fl
597
663
598
664
double _getIntrinsicSize ({
599
665
required Axis sizingDirection,
600
- required double extent, // the extent in the direction that isn't the sizing direction
601
- required _ChildSizingFunction childSize, // a method to find the size in the sizing direction
666
+ required double extent, // The extent in the direction that isn't the sizing direction.
667
+ required _ChildSizingFunction childSize, // A method to find the size in the sizing direction.
602
668
}) {
603
669
if (_direction == sizingDirection) {
604
670
// INTRINSIC MAIN SIZE
605
671
// Intrinsic main size is the smallest size the flex container can take
606
672
// while maintaining the min/max-content contributions of its flex items.
607
673
double totalFlex = 0.0 ;
608
- double inflexibleSpace = 0.0 ;
674
+ double inflexibleSpace = spacing * (childCount - 1 ) ;
609
675
double maxFlexFractionSoFar = 0.0 ;
610
676
for (RenderBox ? child = firstChild; child != null ; child = childAfter (child)) {
611
677
final int flex = _getFlex (child);
@@ -825,7 +891,7 @@ class RenderFlex extends RenderBox with ContainerRenderObjectMixin<RenderBox, Fl
825
891
case Axis .vertical:
826
892
final double freeSpace = math.max (0.0 , sizes.mainAxisFreeSpace);
827
893
final bool flipMainAxis = _flipMainAxis;
828
- final (double leadingSpaceY, double spaceBetween) = mainAxisAlignment._distributeSpace (freeSpace, childCount, flipMainAxis);
894
+ final (double leadingSpaceY, double spaceBetween) = mainAxisAlignment._distributeSpace (freeSpace, childCount, flipMainAxis, spacing );
829
895
double y = flipMainAxis
830
896
? leadingSpaceY + (childCount - 1 ) * spaceBetween + (sizes.axisSize.mainAxisExtent - sizes.mainAxisFreeSpace)
831
897
: leadingSpaceY;
@@ -978,7 +1044,8 @@ class RenderFlex extends RenderBox with ContainerRenderObjectMixin<RenderBox, Fl
978
1044
int totalFlex = 0 ;
979
1045
RenderBox ? firstFlexChild;
980
1046
_AscentDescent accumulatedAscentDescent = _AscentDescent .none;
981
- _AxisSize accumulatedSize = _AxisSize .empty;
1047
+ // Initially, accumulatedSize is the sum of the spaces between children in the main axis.
1048
+ _AxisSize accumulatedSize = _AxisSize ._(Size (spacing * (childCount - 1 ), 0.0 ));
982
1049
for (RenderBox ? child = firstChild; child != null ; child = childAfter (child)) {
983
1050
final int flex;
984
1051
if (canFlex && (flex = _getFlex (child)) > 0 ) {
@@ -1064,7 +1131,7 @@ class RenderFlex extends RenderBox with ContainerRenderObjectMixin<RenderBox, Fl
1064
1131
final double remainingSpace = math.max (0.0 , sizes.mainAxisFreeSpace);
1065
1132
final bool flipMainAxis = _flipMainAxis;
1066
1133
final bool flipCrossAxis = _flipCrossAxis;
1067
- final (double leadingSpace, double betweenSpace) = mainAxisAlignment._distributeSpace (remainingSpace, childCount, flipMainAxis);
1134
+ final (double leadingSpace, double betweenSpace) = mainAxisAlignment._distributeSpace (remainingSpace, childCount, flipMainAxis, spacing );
1068
1135
final (_NextChild nextChild, RenderBox ? topLeftChild) = flipMainAxis ? (childBefore, lastChild) : (childAfter, firstChild);
1069
1136
final double ? baselineOffset = sizes.baselineOffset;
1070
1137
assert (baselineOffset == null || (crossAxisAlignment == CrossAxisAlignment .baseline && direction == Axis .horizontal));
@@ -1192,5 +1259,6 @@ class RenderFlex extends RenderBox with ContainerRenderObjectMixin<RenderBox, Fl
1192
1259
properties.add (EnumProperty <TextDirection >('textDirection' , textDirection, defaultValue: null ));
1193
1260
properties.add (EnumProperty <VerticalDirection >('verticalDirection' , verticalDirection, defaultValue: null ));
1194
1261
properties.add (EnumProperty <TextBaseline >('textBaseline' , textBaseline, defaultValue: null ));
1262
+ properties.add (DoubleProperty ('spacing' , spacing, defaultValue: null ));
1195
1263
}
1196
1264
}
0 commit comments