Skip to content

Commit ccd61d5

Browse files
author
fbchen
committed
add eIndex support
1 parent 856c3ef commit ccd61d5

File tree

1 file changed

+71
-15
lines changed

1 file changed

+71
-15
lines changed

lib/src/constraint_layout.dart

Lines changed: 71 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,7 @@ extension ConstrainedWidgetsExt on Widget {
422422
double maxHeight = matchParent,
423423
double? widthHeightRatio,
424424
bool? ratioBaseOnWidth,
425+
int? eIndex,
425426
}) {
426427
return Constrained(
427428
key: key,
@@ -485,6 +486,7 @@ extension ConstrainedWidgetsExt on Widget {
485486
centerBottomLeftTo: centerBottomLeftTo,
486487
centerBottomCenterTo: centerBottomCenterTo,
487488
centerBottomRightTo: centerBottomRightTo,
489+
eIndex: eIndex,
488490
),
489491
child: this,
490492
);
@@ -909,6 +911,8 @@ class Constraint extends ConstraintDefine {
909911
/// to specify the ratioBaseOnWidth parameter. The default value of null means automatically decide
910912
final bool? ratioBaseOnWidth;
911913

914+
final int? eIndex;
915+
912916
Constraint({
913917
ConstraintId? id,
914918
this.width = wrapContent,
@@ -969,6 +973,7 @@ class Constraint extends ConstraintDefine {
969973
this.maxHeight = matchParent,
970974
this.widthHeightRatio,
971975
this.ratioBaseOnWidth,
976+
this.eIndex,
972977
}) : super(id);
973978

974979
@override
@@ -1033,7 +1038,8 @@ class Constraint extends ConstraintDefine {
10331038
minHeight == other.minHeight &&
10341039
maxHeight == other.maxHeight &&
10351040
widthHeightRatio == other.widthHeightRatio &&
1036-
ratioBaseOnWidth == other.ratioBaseOnWidth;
1041+
ratioBaseOnWidth == other.ratioBaseOnWidth &&
1042+
eIndex == other.eIndex;
10371043

10381044
@override
10391045
int get hashCode =>
@@ -1094,7 +1100,8 @@ class Constraint extends ConstraintDefine {
10941100
minHeight.hashCode ^
10951101
maxHeight.hashCode ^
10961102
widthHeightRatio.hashCode ^
1097-
ratioBaseOnWidth.hashCode;
1103+
ratioBaseOnWidth.hashCode ^
1104+
eIndex.hashCode;
10981105

10991106
bool checkSize(double size) {
11001107
if (size == matchParent || size == wrapContent || size == matchConstraint) {
@@ -1357,7 +1364,8 @@ class Constraint extends ConstraintDefine {
13571364
renderObject.parentData! as _ConstraintBoxData;
13581365
bool needsLayout = false;
13591366
bool needsPaint = false;
1360-
bool needsReorderChildren = false;
1367+
bool needsReorderPaintingOrder = false;
1368+
bool needsReorderEventOrder = false;
13611369
bool needsRecalculateConstraints = false;
13621370

13631371
if (parentData.id != id) {
@@ -1463,7 +1471,8 @@ class Constraint extends ConstraintDefine {
14631471

14641472
if (parentData.zIndex != zIndex) {
14651473
parentData.zIndex = zIndex;
1466-
needsReorderChildren = true;
1474+
needsReorderPaintingOrder = true;
1475+
needsReorderEventOrder = true;
14671476
needsPaint = true;
14681477
}
14691478

@@ -1548,6 +1557,11 @@ class Constraint extends ConstraintDefine {
15481557
needsLayout = true;
15491558
}
15501559

1560+
if (parentData.eIndex != eIndex) {
1561+
parentData.eIndex = eIndex;
1562+
needsReorderEventOrder = true;
1563+
}
1564+
15511565
if (needsLayout) {
15521566
AbstractNode? targetParent = renderObject.parent;
15531567
if (needsRecalculateConstraints) {
@@ -1559,10 +1573,16 @@ class Constraint extends ConstraintDefine {
15591573
targetParent.markNeedsLayout();
15601574
}
15611575
} else {
1562-
if (needsReorderChildren) {
1576+
if (needsReorderPaintingOrder) {
1577+
AbstractNode? targetParent = renderObject.parent;
1578+
if (targetParent is _ConstraintRenderBox) {
1579+
targetParent.needsReorderPaintingOrder = true;
1580+
}
1581+
}
1582+
if (needsReorderEventOrder) {
15631583
AbstractNode? targetParent = renderObject.parent;
15641584
if (targetParent is _ConstraintRenderBox) {
1565-
targetParent.needsReorderChildren = true;
1585+
targetParent.needsReorderEventOrder = true;
15661586
}
15671587
}
15681588
if (needsPaint) {
@@ -1615,6 +1635,7 @@ class _ConstraintBoxData extends ContainerBoxParentData<RenderBox> {
16151635
double? maxHeight;
16161636
double? widthHeightRatio;
16171637
bool? ratioBaseOnWidth;
1638+
int? eIndex;
16181639

16191640
// for internal use
16201641
late Map<ConstraintId, _ConstrainedNode> _constrainedNodeMap;
@@ -1721,7 +1742,8 @@ class _ConstraintRenderBox extends RenderBox
17211742
late double _height;
17221743

17231744
bool _needsRecalculateConstraints = true;
1724-
bool _needsReorderChildren = true;
1745+
bool _needsReorderPaintingOrder = true;
1746+
bool _needsReorderEventOrder = true;
17251747
int _buildNodeTreesCount = 0;
17261748
final Map<ConstraintId, _ConstrainedNode> _helperNodeMap = HashMap();
17271749

@@ -1731,6 +1753,9 @@ class _ConstraintRenderBox extends RenderBox
17311753
/// For paint
17321754
late List<_ConstrainedNode> _paintingOrderList;
17331755

1756+
/// For event dispatch
1757+
late List<_ConstrainedNode> _eventOrderList;
1758+
17341759
static const int maxTimeUsage = 20;
17351760
Queue<int> layoutTimeUsage = Queue();
17361761
Queue<int> paintTimeUsage = Queue();
@@ -1869,13 +1894,19 @@ class _ConstraintRenderBox extends RenderBox
18691894
}
18701895
}
18711896

1872-
set needsReorderChildren(bool value) {
1873-
if (_needsReorderChildren != value) {
1874-
_needsReorderChildren = value;
1897+
set needsReorderPaintingOrder(bool value) {
1898+
if (_needsReorderPaintingOrder != value) {
1899+
_needsReorderPaintingOrder = value;
18751900
markNeedsPaint();
18761901
}
18771902
}
18781903

1904+
set needsReorderEventOrder(bool value) {
1905+
if (_needsReorderEventOrder != value) {
1906+
_needsReorderEventOrder = value;
1907+
}
1908+
}
1909+
18791910
set width(double value) {
18801911
if (_width != value) {
18811912
_width = value;
@@ -2219,7 +2250,8 @@ class _ConstraintRenderBox extends RenderBox
22192250

22202251
void markNeedsRecalculateConstraints() {
22212252
_needsRecalculateConstraints = true;
2222-
_needsReorderChildren = true;
2253+
_needsReorderPaintingOrder = true;
2254+
_needsReorderEventOrder = true;
22232255
}
22242256

22252257
@override
@@ -2285,6 +2317,7 @@ class _ConstraintRenderBox extends RenderBox
22852317
/// Sort by the depth of constraint from shallow to deep, the lowest depth is 0, representing parent
22862318
_layoutOrderList = nodesMap.values.toList();
22872319
_paintingOrderList = nodesMap.values.toList();
2320+
_eventOrderList = nodesMap.values.toList();
22882321

22892322
_layoutOrderList.sort((left, right) {
22902323
return left.getDepth() - right.getDepth();
@@ -2298,6 +2331,14 @@ class _ConstraintRenderBox extends RenderBox
22982331
return result;
22992332
});
23002333

2334+
_eventOrderList.sort((left, right) {
2335+
int result = right.eIndex - left.eIndex;
2336+
if (result == 0) {
2337+
result = right.index - left.index;
2338+
}
2339+
return result;
2340+
});
2341+
23012342
assert(() {
23022343
/// Print constraints
23032344
if (_debugPrintConstraints) {
@@ -2309,7 +2350,8 @@ class _ConstraintRenderBox extends RenderBox
23092350
}());
23102351

23112352
_needsRecalculateConstraints = false;
2312-
_needsReorderChildren = false;
2353+
_needsReorderPaintingOrder = false;
2354+
_needsReorderEventOrder = false;
23132355
}
23142356

23152357
_layoutByConstrainedNodeTrees();
@@ -2848,7 +2890,18 @@ class _ConstraintRenderBox extends RenderBox
28482890
BoxHitTestResult result, {
28492891
required Offset position,
28502892
}) {
2851-
for (final element in _paintingOrderList.reversed) {
2893+
if (_needsReorderEventOrder) {
2894+
_eventOrderList.sort((left, right) {
2895+
int result = right.eIndex - left.eIndex;
2896+
if (result == 0) {
2897+
result = right.index - left.index;
2898+
}
2899+
return result;
2900+
});
2901+
_needsReorderEventOrder = false;
2902+
}
2903+
2904+
for (final element in _eventOrderList) {
28522905
if (element.shouldNotPaint()) {
28532906
continue;
28542907
}
@@ -2912,15 +2965,15 @@ class _ConstraintRenderBox extends RenderBox
29122965
return true;
29132966
}());
29142967

2915-
if (_needsReorderChildren) {
2968+
if (_needsReorderPaintingOrder) {
29162969
_paintingOrderList.sort((left, right) {
29172970
int result = left.zIndex - right.zIndex;
29182971
if (result == 0) {
29192972
result = left.index - right.index;
29202973
}
29212974
return result;
29222975
});
2923-
_needsReorderChildren = false;
2976+
_needsReorderPaintingOrder = false;
29242977
}
29252978

29262979
for (final element in _paintingOrderList) {
@@ -3147,6 +3200,8 @@ class _ConstrainedNode {
31473200

31483201
int get zIndex => parentData.zIndex ?? index;
31493202

3203+
int get eIndex => parentData.eIndex ?? zIndex;
3204+
31503205
Offset get offset {
31513206
if (translateConstraint) {
31523207
return parentData.offset + translate;
@@ -3459,6 +3514,7 @@ class _HelperBox extends RenderBox {
34593514
constraintBoxData.maxHeight = matchParent;
34603515
constraintBoxData.widthHeightRatio = null;
34613516
constraintBoxData.ratioBaseOnWidth = null;
3517+
constraintBoxData.eIndex = null;
34623518
constraintBoxData._direction = null;
34633519
constraintBoxData._referencedIds = null;
34643520
constraintBoxData._isGuideline = false;

0 commit comments

Comments
 (0)