/
flow.dart
1625 lines (1437 loc) · 60.3 KB
/
flow.dart
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2020-present Alibaba Inc. All rights reserved.
* Author: Kraken Team.
*/
import 'dart:math' as math;
import 'package:flutter/foundation.dart';
import 'package:flutter/rendering.dart';
import 'package:kraken/css.dart';
import 'package:kraken/module.dart';
import 'package:kraken/rendering.dart';
/// Infos of each run (line box) in flow layout
/// https://www.w3.org/TR/css-inline-3/#line-boxes
class _RunMetrics {
_RunMetrics(
this.mainAxisExtent,
this.crossAxisExtent,
this.baselineExtent,
this.runChildren,
);
// Main size extent of the run
final double mainAxisExtent;
// Cross size extent of the run
final double crossAxisExtent;
// Max extent above each flex items in the run
final double baselineExtent;
// All the children RenderBox of layout in the run
final Map<int?, RenderBox> runChildren;
}
/// Impl flow layout algorithm.
class RenderFlowLayout extends RenderLayoutBox {
RenderFlowLayout({
List<RenderBox>? children,
required RenderStyle renderStyle,
}) : super(
renderStyle: renderStyle,
) {
addAll(children);
}
/// The direction to use as the main axis.
///
/// For example, if [direction] is [Axis.horizontal], the default, the
/// children are placed adjacent to one another in a horizontal run until the
/// available horizontal space is consumed, at which point a subsequent
/// children are placed in a new run vertically adjacent to the previous run.
Axis get direction => _direction;
Axis _direction = Axis.horizontal;
set direction(Axis value) {
if (_direction == value) return;
_direction = value;
markNeedsLayout();
}
/// How the runs themselves should be placed in the cross axis.
///
/// For example, if [runAlignment] is [MainAxisAlignment.center], the runs are
/// grouped together in the center of the overall [RenderWrap] in the cross
/// axis.
///
/// Defaults to [MainAxisAlignment.start].
///
MainAxisAlignment get runAlignment => _runAlignment;
MainAxisAlignment _runAlignment = MainAxisAlignment.start;
set runAlignment(MainAxisAlignment value) {
if (_runAlignment == value) return;
_runAlignment = value;
markNeedsLayout();
}
/// If there is additional free space in the overall [RenderWrap] (e.g.,
/// The distance by which the child's top edge is inset from the top of the stack.
double? top;
/// The distance by which the child's right edge is inset from the right of the stack.
double? right;
/// The distance by which the child's bottom edge is inset from the bottom of the stack.
double? bottom;
/// The distance by which the child's left edge is inset from the left of the stack.
double? left;
/// How the children within a run should be aligned relative to each other in
/// the cross axis.
///
/// For example, if this is set to [CrossAxisAlignment.end], and the
/// [direction] is [Axis.horizontal], then the children within each
/// run will have their bottom edges aligned to the bottom edge of the run.
///
/// Defaults to [CrossAxisAlignment.end].
///
CrossAxisAlignment get crossAxisAlignment => _crossAxisAlignment;
CrossAxisAlignment _crossAxisAlignment = CrossAxisAlignment.end;
set crossAxisAlignment(CrossAxisAlignment value) {
if (_crossAxisAlignment == value) return;
_crossAxisAlignment = value;
markNeedsLayout();
}
/// Determines the order to lay children out horizontally and how to interpret
/// `start` and `end` in the horizontal direction.
///
/// If the [direction] is [Axis.horizontal], this controls the order in which
/// children are positioned (left-to-right or right-to-left), and the meaning
/// of the textAlign style's [TextAlign.start] and
/// [TextAlign.end] values.
///
/// If the [direction] is [Axis.horizontal], and either the
/// textAlign style is either [TextAlign.start] or [TextAlign.end], or
/// there's more than one child, then the [textDirection] must not be null.
///
/// If the [direction] is [Axis.vertical], this controls the order in
/// which runs are positioned, the meaning of the [runAlignment] property's
/// [TextAlign.start] and [TextAlign.end] values, as well as the
/// [crossAxisAlignment] property's [CrossAxisAlignment.start] and
/// [CrossAxisAlignment.end] values.
///
/// If the [direction] is [Axis.vertical], and either the
/// [runAlignment] is either [MainAxisAlignment.start] or [MainAxisAlignment.end], the
/// [crossAxisAlignment] is either [CrossAxisAlignment.start] or
/// [CrossAxisAlignment.end], or there's more than one child, then the
/// [textDirection] must not be null.
TextDirection get textDirection => _textDirection;
TextDirection _textDirection = TextDirection.ltr;
set textDirection(TextDirection value) {
if (_textDirection != value) {
_textDirection = value;
markNeedsLayout();
}
}
/// Determines the order to lay children out vertically and how to interpret
/// `start` and `end` in the vertical direction.
///
/// If the [direction] is [Axis.vertical], this controls which order children
/// are painted in (down or up), the meaning of the textAlign style's
/// [TextAlign.start] and [TextAlign.end] values.
///
/// If the [direction] is [Axis.vertical], and either the textAlign
/// is either [TextAlign.start] or [TextAlign.end], or there's
/// more than one child, then the [verticalDirection] must not be null.
///
/// If the [direction] is [Axis.horizontal], this controls the order in which
/// runs are positioned, the meaning of the [runAlignment] property's
/// [MainAxisAlignment.start] and [MainAxisAlignment.end] values, as well as the
/// [crossAxisAlignment] property's [CrossAxisAlignment.start] and
/// [CrossAxisAlignment.end] values.
///
/// If the [direction] is [Axis.horizontal], and either the
/// [runAlignment] is either [MainAxisAlignment.start] or [MainAxisAlignment.end], the
/// [crossAxisAlignment] is either [CrossAxisAlignment.start] or
/// [CrossAxisAlignment.end], or there's more than one child, then the
/// [verticalDirection] must not be null.
VerticalDirection get verticalDirection => _verticalDirection;
VerticalDirection _verticalDirection = VerticalDirection.down;
set verticalDirection(VerticalDirection value) {
if (_verticalDirection != value) {
_verticalDirection = value;
markNeedsLayout();
}
}
/// Line boxes of flow layout
List<_RunMetrics> lineBoxMetrics = <_RunMetrics>[];
@override
void dispose() {
super.dispose();
lineBoxMetrics.clear();
}
@override
void setupParentData(RenderBox child) {
if (child.parentData is! RenderLayoutParentData) {
child.parentData = RenderLayoutParentData();
}
if (child is RenderBoxModel) {
child.parentData = CSSPositionedLayout.getPositionParentData(
child, child.parentData as RenderLayoutParentData);
}
}
double _computeIntrinsicHeightForWidth(double width) {
assert(direction == Axis.horizontal);
double height = 0.0;
double runWidth = 0.0;
double runHeight = 0.0;
int childCount = 0;
RenderBox? child = firstChild;
while (child != null) {
final double childWidth = child.getMaxIntrinsicWidth(double.infinity);
final double childHeight = child.getMaxIntrinsicHeight(childWidth);
if (runWidth + childWidth > width) {
height += runHeight;
runWidth = 0.0;
runHeight = 0.0;
childCount = 0;
}
runWidth += childWidth;
runHeight = math.max(runHeight, childHeight);
childCount += 1;
child = childAfter(child);
}
if (childCount > 0) height += runHeight;
return height;
}
double _computeIntrinsicWidthForHeight(double height) {
assert(direction == Axis.vertical);
double width = 0.0;
double runHeight = 0.0;
double runWidth = 0.0;
int childCount = 0;
RenderBox? child = firstChild;
while (child != null) {
final double childHeight = child.getMaxIntrinsicHeight(double.infinity);
final double childWidth = child.getMaxIntrinsicWidth(childHeight);
if (runHeight + childHeight > height) {
width += runWidth;
runHeight = 0.0;
runWidth = 0.0;
childCount = 0;
}
runHeight += childHeight;
runWidth = math.max(runWidth, childWidth);
childCount += 1;
child = childAfter(child);
}
if (childCount > 0) width += runWidth;
return width;
}
@override
double computeMinIntrinsicWidth(double height) {
switch (direction) {
case Axis.horizontal:
double width = 0.0;
RenderBox? child = firstChild;
while (child != null) {
width = math.max(width, child.getMinIntrinsicWidth(double.infinity));
child = childAfter(child);
}
return width;
case Axis.vertical:
return _computeIntrinsicWidthForHeight(height);
}
}
@override
double computeMaxIntrinsicWidth(double height) {
switch (direction) {
case Axis.horizontal:
double width = 0.0;
RenderBox? child = firstChild;
while (child != null) {
width += child.getMaxIntrinsicWidth(double.infinity);
child = childAfter(child);
}
return width;
case Axis.vertical:
return _computeIntrinsicWidthForHeight(height);
}
}
@override
double computeMinIntrinsicHeight(double width) {
switch (direction) {
case Axis.horizontal:
return _computeIntrinsicHeightForWidth(width);
case Axis.vertical:
double height = 0.0;
RenderBox? child = firstChild;
while (child != null) {
height =
math.max(height, child.getMinIntrinsicHeight(double.infinity));
child = childAfter(child);
}
return height;
}
}
/// Get current offset.
Offset get offset => (parentData as BoxParentData).offset;
@override
double computeMaxIntrinsicHeight(double width) {
switch (direction) {
case Axis.horizontal:
return _computeIntrinsicHeightForWidth(width);
case Axis.vertical:
double height = 0.0;
RenderBox? child = firstChild;
while (child != null) {
height += child.getMaxIntrinsicHeight(double.infinity);
child = childAfter(child);
}
return height;
}
}
double _getMainAxisExtent(RenderBox child) {
double marginHorizontal = 0;
double marginVertical = 0;
if (child is RenderBoxModel) {
marginHorizontal = child.renderStyle.marginLeft.computedValue +
child.renderStyle.marginRight.computedValue;
marginVertical = _getChildMarginTop(child) + _getChildMarginBottom(child);
}
Size childSize = _getChildSize(child) ?? Size.zero;
switch (direction) {
case Axis.horizontal:
return childSize.width + marginHorizontal;
case Axis.vertical:
return childSize.height + marginVertical;
}
}
double _getCrossAxisExtent(RenderBox child) {
bool isLineHeightValid = _isLineHeightValid(child);
double? lineHeight = isLineHeightValid ? _getLineHeight(child) : 0;
double marginVertical = 0;
double marginHorizontal = 0;
if (child is RenderBoxModel) {
marginHorizontal = child.renderStyle.marginLeft.computedValue +
child.renderStyle.marginRight.computedValue;
marginVertical = _getChildMarginTop(child) + _getChildMarginBottom(child);
}
Size childSize = _getChildSize(child) ?? Size.zero;
switch (direction) {
case Axis.horizontal:
return lineHeight != null
? math.max(lineHeight, childSize.height) + marginVertical
: childSize.height + marginVertical;
case Axis.vertical:
return childSize.width + marginHorizontal;
}
}
Offset _getOffset(double mainAxisOffset, double crossAxisOffset) {
switch (direction) {
case Axis.horizontal:
return Offset(mainAxisOffset, crossAxisOffset);
case Axis.vertical:
return Offset(crossAxisOffset, mainAxisOffset);
}
}
double _getChildCrossAxisOffset(bool flipCrossAxis, double runCrossAxisExtent,
double childCrossAxisExtent) {
final double freeSpace = runCrossAxisExtent - childCrossAxisExtent;
switch (crossAxisAlignment) {
case CrossAxisAlignment.start:
return flipCrossAxis ? freeSpace : 0.0;
case CrossAxisAlignment.end:
return flipCrossAxis ? 0.0 : freeSpace;
case CrossAxisAlignment.center:
return freeSpace / 2.0;
case CrossAxisAlignment.baseline:
return 0.0;
case CrossAxisAlignment.stretch:
return 0.0;
}
}
double? _getLineHeight(RenderBox child) {
CSSLengthValue? lineHeight;
if (child is RenderTextBox) {
lineHeight = renderStyle.lineHeight;
} else if (child is RenderBoxModel) {
lineHeight = child.renderStyle.lineHeight;
} else if (child is RenderPositionPlaceholder) {
lineHeight = child.positioned!.renderStyle.lineHeight;
}
if (lineHeight != null && lineHeight.type != CSSLengthType.NORMAL) {
return lineHeight.computedValue;
}
return null;
}
@override
void performLayout() {
if (kProfileMode) {
childLayoutDuration = 0;
PerformanceTiming.instance()
.mark(PERF_FLOW_LAYOUT_START, uniqueId: hashCode);
}
_doPerformLayout();
if (needsRelayout) {
_doPerformLayout();
needsRelayout = false;
}
if (kProfileMode) {
DateTime flowLayoutEndTime = DateTime.now();
int amendEndTime =
flowLayoutEndTime.microsecondsSinceEpoch - childLayoutDuration;
PerformanceTiming.instance().mark(PERF_FLOW_LAYOUT_END,
uniqueId: hashCode, startTime: amendEndTime);
}
}
void _doPerformLayout() {
beforeLayout();
RenderBox? child = firstChild;
// Layout positioned element
while (child != null) {
final RenderLayoutParentData childParentData =
child.parentData as RenderLayoutParentData;
if (childParentData.isPositioned) {
CSSPositionedLayout.layoutPositionedChild(
this, child as RenderBoxModel);
}
child = childParentData.nextSibling;
}
// Layout non positioned element
_layoutChildren();
// Set offset of positioned and sticky element
child = firstChild;
while (child != null) {
final RenderLayoutParentData childParentData =
child.parentData as RenderLayoutParentData;
if (child is RenderBoxModel && childParentData.isPositioned) {
CSSPositionedLayout.applyPositionedChildOffset(this, child);
extendMaxScrollableSize(child);
ensureBoxSizeLargerThanScrollableSize();
} else if (child is RenderBoxModel &&
CSSPositionedLayout.isSticky(child)) {
RenderBoxModel scrollContainer = child.findScrollContainer()!;
// Sticky offset depends on the layout of scroll container, delay the calculation of
// sticky offset to the layout stage of scroll container if its not layouted yet
// due to the layout order of Flutter renderObject tree is from down to up.
if (scrollContainer.hasSize) {
CSSPositionedLayout.applyStickyChildOffset(scrollContainer, child);
}
}
child = childParentData.nextSibling;
}
bool isScrollContainer =
(renderStyle.effectiveOverflowX != CSSOverflowType.visible ||
renderStyle.effectiveOverflowY != CSSOverflowType.visible);
if (isScrollContainer) {
// Find all the sticky children when scroll container is layouted
stickyChildren = findStickyChildren();
// Calculate the offset of its sticky children
for (RenderBoxModel stickyChild in stickyChildren) {
CSSPositionedLayout.applyStickyChildOffset(this, stickyChild);
}
}
didLayout();
}
void _layoutChildren() {
RenderBox? child = firstChild;
// If no child exists, stop layout.
if (childCount == 0) {
Size layoutSize = getLayoutSize(
logicalContentWidth: logicalContentWidth,
logicalContentHeight: logicalContentHeight,
contentWidth: 0,
contentHeight: 0,
);
double constraintWidth = layoutSize.width;
double constraintHeight = layoutSize.height;
setMaxScrollableSize(constraintWidth, constraintHeight);
size = getBoxSize(Size(
constraintWidth,
constraintHeight,
));
return;
}
double mainAxisLimit = 0.0;
bool flipMainAxis = false;
bool flipCrossAxis = false;
// Use scrolling container to calculate flex line limit for scrolling content box
RenderBoxModel? containerBox =
isScrollingContentBox ? parent as RenderBoxModel? : this;
switch (direction) {
case Axis.horizontal:
mainAxisLimit = renderStyle.contentMaxConstraintsWidth;
if (textDirection == TextDirection.rtl) flipMainAxis = true;
if (verticalDirection == VerticalDirection.up) flipCrossAxis = true;
break;
case Axis.vertical:
mainAxisLimit = containerBox!.contentConstraints!.maxHeight;
if (verticalDirection == VerticalDirection.up) flipMainAxis = true;
if (textDirection == TextDirection.rtl) flipCrossAxis = true;
break;
}
List<_RunMetrics> runMetrics = <_RunMetrics>[];
double mainAxisExtent = 0.0;
double crossAxisExtent = 0.0;
double runMainAxisExtent = 0.0;
double runCrossAxisExtent = 0.0;
RenderBox? preChild;
double maxSizeAboveBaseline = 0;
double maxSizeBelowBaseline = 0;
Map<int?, RenderBox> runChildren = {};
lineBoxMetrics = runMetrics;
WhiteSpace? whiteSpace = renderStyle.whiteSpace;
while (child != null) {
final RenderLayoutParentData childParentData =
child.parentData as RenderLayoutParentData;
if (childParentData.isPositioned) {
child = childParentData.nextSibling;
continue;
}
int? childNodeId;
if (child is RenderTextBox) {
childNodeId = child.hashCode;
} else if (child is RenderBoxModel) {
childNodeId = child.hashCode;
}
BoxConstraints childConstraints;
if (child is RenderBoxModel) {
childConstraints = child.getConstraints();
} else if (child is RenderTextBox) {
childConstraints = child.getConstraints();
} else {
childConstraints = BoxConstraints();
}
// Whether child need to layout
bool isChildNeedsLayout = true;
if (child.hasSize &&
!needsRelayout &&
(childConstraints == child.constraints) &&
((child is RenderBoxModel && !child.needsLayout) ||
(child is RenderTextBox && !child.needsLayout))) {
isChildNeedsLayout = false;
}
if (isChildNeedsLayout) {
late DateTime childLayoutStart;
if (kProfileMode) {
childLayoutStart = DateTime.now();
}
// Inflate constraints of percentage renderBoxModel to force it layout after percentage resolved
// cause Flutter will skip child layout if its constraints not changed between two layouts.
if (child is RenderBoxModel && needsRelayout) {
childConstraints = BoxConstraints(
minWidth: childConstraints.maxWidth != double.infinity
? childConstraints.maxWidth
: 0,
maxWidth: double.infinity,
minHeight: childConstraints.maxHeight != double.infinity
? childConstraints.maxHeight
: 0,
maxHeight: double.infinity,
);
}
child.layout(childConstraints, parentUsesSize: true);
if (kProfileMode) {
DateTime childLayoutEnd = DateTime.now();
childLayoutDuration += (childLayoutEnd.microsecondsSinceEpoch -
childLayoutStart.microsecondsSinceEpoch);
}
}
double childMainAxisExtent = _getMainAxisExtent(child);
double childCrossAxisExtent = _getCrossAxisExtent(child);
if (isPositionPlaceholder(child)) {
RenderPositionPlaceholder positionHolder = child as RenderPositionPlaceholder;
RenderBoxModel? childRenderBoxModel = positionHolder.positioned;
if (childRenderBoxModel != null) {
RenderLayoutParentData childParentData =
childRenderBoxModel.parentData as RenderLayoutParentData;
if (childParentData.isPositioned) {
childMainAxisExtent = childCrossAxisExtent = 0;
}
}
}
// white-space property not only specifies whether and how white space is collapsed
// but only specifies whether lines may wrap at unforced soft wrap opportunities
// https://www.w3.org/TR/css-text-3/#line-breaking
bool isChildBlockLevel = _isChildBlockLevel(child);
bool isPreChildBlockLevel = _isChildBlockLevel(preChild);
bool isLineLengthExceedContainer = whiteSpace != WhiteSpace.nowrap &&
(runMainAxisExtent + childMainAxisExtent > mainAxisLimit);
if (runChildren.isNotEmpty &&
(isChildBlockLevel ||
isPreChildBlockLevel ||
isLineLengthExceedContainer)) {
mainAxisExtent = math.max(mainAxisExtent, runMainAxisExtent);
crossAxisExtent += runCrossAxisExtent;
runMetrics.add(_RunMetrics(
runMainAxisExtent,
runCrossAxisExtent,
maxSizeAboveBaseline,
runChildren,
));
runChildren = {};
runMainAxisExtent = 0.0;
runCrossAxisExtent = 0.0;
maxSizeAboveBaseline = 0.0;
maxSizeBelowBaseline = 0.0;
}
runMainAxisExtent += childMainAxisExtent;
/// Calculate baseline extent of layout box
RenderStyle childRenderStyle = _getChildRenderStyle(child)!;
VerticalAlign verticalAlign = childRenderStyle.verticalAlign;
bool isLineHeightValid = _isLineHeightValid(child);
// Vertical align is only valid for inline box
if (verticalAlign == VerticalAlign.baseline && isLineHeightValid) {
double childMarginTop = 0;
double childMarginBottom = 0;
if (child is RenderBoxModel) {
childMarginTop = _getChildMarginTop(child);
childMarginBottom = _getChildMarginBottom(child);
}
Size childSize = _getChildSize(child)!;
double? lineHeight = _getLineHeight(child);
// Leading space between content box and virtual box of child
double childLeading = 0;
if (child is! RenderTextBox && lineHeight != null) {
childLeading = lineHeight - childSize.height;
}
// When baseline of children not found, use boundary of margin bottom as baseline
double childAscent = _getChildAscent(child);
double extentAboveBaseline = childAscent + childLeading / 2;
double extentBelowBaseline = childMarginTop +
childSize.height +
childMarginBottom -
childAscent +
childLeading / 2;
maxSizeAboveBaseline = math.max(
extentAboveBaseline,
maxSizeAboveBaseline,
);
maxSizeBelowBaseline = math.max(
extentBelowBaseline,
maxSizeBelowBaseline,
);
runCrossAxisExtent = maxSizeAboveBaseline + maxSizeBelowBaseline;
} else {
runCrossAxisExtent = math.max(runCrossAxisExtent, childCrossAxisExtent);
}
runChildren[childNodeId] = child;
childParentData.runIndex = runMetrics.length;
preChild = child;
child = childParentData.nextSibling;
}
if (runChildren.isNotEmpty) {
mainAxisExtent = math.max(mainAxisExtent, runMainAxisExtent);
crossAxisExtent += runCrossAxisExtent;
runMetrics.add(_RunMetrics(
runMainAxisExtent,
runCrossAxisExtent,
maxSizeAboveBaseline,
runChildren,
));
}
final int runCount = runMetrics.length;
Size layoutSize = getLayoutSize(
logicalContentWidth: logicalContentWidth,
logicalContentHeight: logicalContentHeight,
contentWidth: mainAxisExtent,
contentHeight: crossAxisExtent,
);
double constraintWidth = layoutSize.width;
double constraintHeight = layoutSize.height;
// Main and cross content size of flow layout
double mainAxisContentSize = 0.0;
double crossAxisContentSize = 0.0;
switch (direction) {
case Axis.horizontal:
Size logicalSize = Size(constraintWidth, constraintHeight);
size = getBoxSize(logicalSize);
mainAxisContentSize = contentSize.width;
crossAxisContentSize = contentSize.height;
break;
case Axis.vertical:
Size logicalSize = Size(crossAxisExtent, mainAxisExtent);
size = getBoxSize(logicalSize);
mainAxisContentSize = contentSize.height;
crossAxisContentSize = contentSize.width;
break;
}
_setMaxScrollableSizeForFlow(runMetrics);
autoMinWidth = _getMainAxisAutoSize(runMetrics);
autoMinHeight = _getCrossAxisAutoSize(runMetrics);
final double crossAxisFreeSpace =
math.max(0.0, crossAxisContentSize - crossAxisExtent);
double runLeadingSpace = 0.0;
double runBetweenSpace = 0.0;
switch (runAlignment) {
case MainAxisAlignment.start:
break;
case MainAxisAlignment.end:
runLeadingSpace = crossAxisFreeSpace;
break;
case MainAxisAlignment.center:
runLeadingSpace = crossAxisFreeSpace / 2.0;
break;
case MainAxisAlignment.spaceBetween:
runBetweenSpace =
runCount > 1 ? crossAxisFreeSpace / (runCount - 1) : 0.0;
break;
case MainAxisAlignment.spaceAround:
runBetweenSpace = crossAxisFreeSpace / runCount;
runLeadingSpace = runBetweenSpace / 2.0;
break;
case MainAxisAlignment.spaceEvenly:
runBetweenSpace = crossAxisFreeSpace / (runCount + 1);
runLeadingSpace = runBetweenSpace;
break;
}
double crossAxisOffset = flipCrossAxis
? crossAxisContentSize - runLeadingSpace
: runLeadingSpace;
child = firstChild;
/// Set offset of children
for (int i = 0; i < runCount; ++i) {
final _RunMetrics metrics = runMetrics[i];
final double runMainAxisExtent = metrics.mainAxisExtent;
final double runCrossAxisExtent = metrics.crossAxisExtent;
final double runBaselineExtent = metrics.baselineExtent;
final Map<int?, RenderBox> runChildren = metrics.runChildren;
final int runChildrenCount = metrics.runChildren.length;
final double mainAxisFreeSpace = math.max(0.0, mainAxisContentSize - runMainAxisExtent);
double childLeadingSpace = 0.0;
double childBetweenSpace = 0.0;
// Whether inline level child exists in this run.
bool runContainInlineChild = true;
int? firstChildKey = runChildren.keys.elementAt(0);
RenderBox? firstChild = runChildren[firstChildKey];
// Block level and inline level child can not exists at the same run,
// so only need to loop the first child.
if (firstChild is RenderBoxModel) {
CSSDisplay? childEffectiveDisplay = firstChild.renderStyle.effectiveDisplay;
runContainInlineChild = childEffectiveDisplay != CSSDisplay.block && childEffectiveDisplay != CSSDisplay.flex;
}
// Text-align only works on inline level children
if (runContainInlineChild) {
switch (renderStyle.textAlign) {
case TextAlign.left:
case TextAlign.start:
break;
case TextAlign.right:
case TextAlign.end:
childLeadingSpace = mainAxisFreeSpace;
break;
case TextAlign.center:
childLeadingSpace = mainAxisFreeSpace / 2.0;
break;
case TextAlign.justify:
childBetweenSpace = runChildrenCount > 1 ? mainAxisFreeSpace / (runChildrenCount - 1) : 0.0;
break;
default:
break;
}
}
double childMainPosition = flipMainAxis
? mainAxisContentSize - childLeadingSpace
: childLeadingSpace;
if (flipCrossAxis) crossAxisOffset -= runCrossAxisExtent;
while (child != null) {
final RenderLayoutParentData childParentData =
child.parentData as RenderLayoutParentData;
if (childParentData.isPositioned) {
child = childParentData.nextSibling;
continue;
}
if (childParentData.runIndex != i) break;
final double childMainAxisExtent = _getMainAxisExtent(child);
final double childCrossAxisExtent = _getCrossAxisExtent(child);
// Calculate margin auto length according to CSS spec
// https://www.w3.org/TR/CSS21/visudet.html#blockwidth
// margin-left and margin-right auto takes up available space
// between element and its containing block on block-level element
// which is not positioned and computed to 0px in other cases
if (child is RenderBoxModel) {
RenderStyle childRenderStyle = child.renderStyle;
CSSDisplay? childEffectiveDisplay =
childRenderStyle.effectiveDisplay;
CSSLengthValue marginLeft = childRenderStyle.marginLeft;
CSSLengthValue marginRight = childRenderStyle.marginRight;
// 'margin-left' + 'border-left-width' + 'padding-left' + 'width' + 'padding-right' +
// 'border-right-width' + 'margin-right' = width of containing block
if (childEffectiveDisplay == CSSDisplay.block ||
childEffectiveDisplay == CSSDisplay.flex) {
if (marginLeft.isAuto) {
double remainingSpace = mainAxisContentSize - childMainAxisExtent;
if (marginRight.isAuto) {
childMainPosition = remainingSpace / 2;
} else {
childMainPosition = remainingSpace;
}
}
}
}
// Always align to the top of run when positioning positioned element placeholder
// @HACK(kraken): Judge positioned holder to impl top align.
final double childCrossAxisOffset = isPositionPlaceholder(child)
? 0
: _getChildCrossAxisOffset(
flipCrossAxis, runCrossAxisExtent, childCrossAxisExtent);
if (flipMainAxis) childMainPosition -= childMainAxisExtent;
Size? childSize = _getChildSize(child);
// Line height of child
double? childLineHeight = _getLineHeight(child);
// Leading space between content box and virtual box of child
double childLeading = 0;
if (childLineHeight != null) {
childLeading = childLineHeight - childSize!.height;
}
// Child line extent caculated according to vertical align
double childLineExtent = childCrossAxisOffset;
bool isLineHeightValid = _isLineHeightValid(child);
if (isLineHeightValid) {
// Distance from top to baseline of child
double childAscent = _getChildAscent(child);
RenderStyle childRenderStyle = _getChildRenderStyle(child)!;
VerticalAlign verticalAlign = childRenderStyle.verticalAlign;
// Leading between height of line box's content area and line height of line box
double lineBoxLeading = 0;
double? lineBoxHeight = _getLineHeight(this);
if (child is! RenderTextBox && lineBoxHeight != null) {
lineBoxLeading = lineBoxHeight - runCrossAxisExtent;
}
switch (verticalAlign) {
case VerticalAlign.baseline:
childLineExtent =
lineBoxLeading / 2 + (runBaselineExtent - childAscent);
break;
case VerticalAlign.top:
childLineExtent = childLeading / 2;
break;
case VerticalAlign.bottom:
childLineExtent =
(lineBoxHeight ?? runCrossAxisExtent) -
childSize!.height -
childLeading / 2;
break;
// @TODO Vertical align middle needs to caculate the baseline of the parent box plus half the x-height of the parent from W3C spec,
// currently flutter lack the api to caculate x-height of glyph
// case VerticalAlign.middle:
// break;
}
}
double? childMarginLeft = 0;
double? childMarginTop = 0;
if (child is RenderBoxModel) {
childMarginLeft = child.renderStyle.marginLeft.computedValue;
childMarginTop = _getChildMarginTop(child);
}
// No need to add padding and border for scrolling content box.
Offset relativeOffset = _getOffset(
childMainPosition +
renderStyle.paddingLeft.computedValue +
renderStyle.effectiveBorderLeftWidth.computedValue +
childMarginLeft,
crossAxisOffset +
childLineExtent +
renderStyle.paddingTop.computedValue +
renderStyle.effectiveBorderTopWidth.computedValue +
childMarginTop);
// Apply position relative offset change.
CSSPositionedLayout.applyRelativeOffset(relativeOffset, child);
if (flipMainAxis)
childMainPosition -= childBetweenSpace;
else
childMainPosition += childMainAxisExtent + childBetweenSpace;
child = childParentData.nextSibling;
}
if (flipCrossAxis)
crossAxisOffset -= runBetweenSpace;
else
crossAxisOffset += runCrossAxisExtent + runBetweenSpace;
}
}
/// Compute distance to baseline of flow layout
@override
double? computeDistanceToBaseline() {
double? lineDistance;
CSSDisplay? effectiveDisplay = renderStyle.effectiveDisplay;
bool isInline = effectiveDisplay == CSSDisplay.inline;
// Margin does not work for inline element.
double marginTop = !isInline ? renderStyle.marginTop.computedValue : 0;
double marginBottom = !isInline ? renderStyle.marginBottom.computedValue : 0;
bool isParentFlowLayout = parent is RenderFlowLayout;
bool isDisplayInline = effectiveDisplay == CSSDisplay.inline ||
effectiveDisplay == CSSDisplay.inlineBlock ||
effectiveDisplay == CSSDisplay.inlineFlex;
// Use margin bottom as baseline if layout has no children
if (lineBoxMetrics.isEmpty) {
if (isDisplayInline) {
// Flex item baseline does not includes margin-bottom
lineDistance = isParentFlowLayout
? marginTop + boxSize!.height + marginBottom
: marginTop + boxSize!.height;
return lineDistance;
} else {
return null;
}
}
// Use baseline of last line in flow layout and layout is inline-level
// otherwise use baseline of first line
bool isLastLineBaseline = isParentFlowLayout && isDisplayInline;
_RunMetrics lineMetrics = isLastLineBaseline
? lineBoxMetrics[lineBoxMetrics.length - 1]
: lineBoxMetrics[0];
// Use the max baseline of the children as the baseline in flow layout
lineMetrics.runChildren.forEach((int? hashCode, RenderBox child) {
double? childMarginTop =
child is RenderBoxModel ? _getChildMarginTop(child) : 0;
RenderLayoutParentData? childParentData =
child.parentData as RenderLayoutParentData?;
double? childBaseLineDistance;
if (child is RenderBoxModel) {
childBaseLineDistance = child.computeDistanceToBaseline();
} else if (child is RenderTextBox) {
// Text baseline not depends on its own parent but its grand parents
childBaseLineDistance = isLastLineBaseline