Skip to content

Commit 87f7f0c

Browse files
author
fbchen
committed
enhance relative id
1 parent 7b172da commit 87f7f0c

File tree

2 files changed

+72
-19
lines changed

2 files changed

+72
-19
lines changed

example/relative_id.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,20 @@ class RelativeIdExample extends StatelessWidget {
3131
right: rId(0).right,
3232
top: rId(0).top,
3333
bottom: rId(0).top,
34+
),
35+
Container(
36+
color: Colors.green,
37+
).applyConstraint(
38+
width: 50,
39+
height: 50,
40+
centerBottomRightTo: sId(-1),
41+
),
42+
Container(
43+
color: Colors.blue,
44+
).applyConstraint(
45+
width: 50,
46+
height: 50,
47+
centerBottomRightTo: sId(-3),
3448
)
3549
],
3650
),

lib/src/constraint_layout.dart

Lines changed: 58 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -682,30 +682,59 @@ class ConstraintId {
682682
}
683683
}
684684

685+
class IndexConstraintId extends ConstraintId {
686+
final int siblingIndex;
687+
688+
IndexConstraintId(this.siblingIndex)
689+
: super('parent.children[$siblingIndex]');
690+
691+
@override
692+
bool operator ==(Object other) =>
693+
identical(this, other) ||
694+
super == other &&
695+
other is IndexConstraintId &&
696+
runtimeType == other.runtimeType &&
697+
siblingIndex == other.siblingIndex;
698+
699+
@override
700+
int get hashCode => super.hashCode ^ siblingIndex.hashCode;
701+
702+
@override
703+
String toString() {
704+
return 'IndexConstraintId{siblingIndex: $siblingIndex}';
705+
}
706+
}
707+
708+
ConstraintId rId(int childIndex) {
709+
return IndexConstraintId(childIndex);
710+
}
711+
685712
class RelativeConstraintId extends ConstraintId {
686-
final int childIndex;
713+
final int siblingIndexOffset;
687714

688-
RelativeConstraintId(this.childIndex) : super('child[$childIndex]');
715+
RelativeConstraintId(this.siblingIndexOffset)
716+
: super('sibling@$siblingIndexOffset');
689717

690718
@override
691719
bool operator ==(Object other) =>
692720
identical(this, other) ||
693721
super == other &&
694722
other is RelativeConstraintId &&
695723
runtimeType == other.runtimeType &&
696-
childIndex == other.childIndex;
724+
siblingIndexOffset == other.siblingIndexOffset;
697725

698726
@override
699-
int get hashCode => super.hashCode ^ childIndex.hashCode;
727+
int get hashCode => super.hashCode ^ siblingIndexOffset.hashCode;
700728

701729
@override
702730
String toString() {
703-
return 'RelativeConstraintId{childIndex: $childIndex}';
731+
return 'RelativeConstraintId{siblingIndexOffset: $siblingIndexOffset}';
704732
}
705733
}
706734

707-
ConstraintId rId(int childIndex) {
708-
return RelativeConstraintId(childIndex);
735+
ConstraintId sId(int siblingIndexOffset) {
736+
assert(siblingIndexOffset != 0);
737+
return RelativeConstraintId(siblingIndexOffset);
709738
}
710739

711740
class _Align {
@@ -731,7 +760,9 @@ typedef OnLayoutCallback = void Function(RenderObject renderObject, Rect rect);
731760
class ConstraintDefine {
732761
final ConstraintId? id;
733762

734-
ConstraintDefine(this.id);
763+
ConstraintDefine(this.id)
764+
: assert(id is! IndexConstraintId),
765+
assert(id is! RelativeConstraintId);
735766

736767
@override
737768
bool operator ==(Object other) =>
@@ -1931,11 +1962,13 @@ class _ConstraintRenderBox extends RenderBox
19311962

19321963
/// The id used by all constraints must be defined
19331964
Set<ConstraintId> illegalIdSet = constraintsIdSet.difference(idSet);
1965+
Set<IndexConstraintId> indexIds =
1966+
illegalIdSet.whereType<IndexConstraintId>().toSet();
19341967
Set<RelativeConstraintId> relativeIds =
19351968
illegalIdSet.whereType<RelativeConstraintId>().toSet();
1936-
if (relativeIds.length != illegalIdSet.length) {
1969+
if ((indexIds.length + relativeIds.length) != illegalIdSet.length) {
19371970
throw ConstraintLayoutException(
1938-
'These ids ${illegalIdSet.difference(relativeIds)} are not yet defined.');
1971+
'These ids ${illegalIdSet.difference(indexIds).difference(relativeIds)} are not yet defined.');
19391972
}
19401973
}
19411974

@@ -2061,11 +2094,17 @@ class _ConstraintRenderBox extends RenderBox
20612094
Map<ConstraintId, _ConstrainedNode> nodesMap = HashMap();
20622095
_buildNodeTreesCount++;
20632096

2064-
_ConstrainedNode _getConstrainedNodeForChild(ConstraintId id) {
2097+
_ConstrainedNode _getConstrainedNodeForChild(ConstraintId id,
2098+
[int? childIndex]) {
20652099
if (id == parent) {
20662100
return _parentNode;
20672101
}
20682102

2103+
if (id is RelativeConstraintId) {
2104+
int targetIndex = childIndex! + id.siblingIndexOffset;
2105+
id = IndexConstraintId(targetIndex);
2106+
}
2107+
20692108
/// Fewer reads to nodesMap for faster constraint building
20702109
_ConstrainedNode? node = id.getCacheNode(_buildNodeTreesCount ^ hashCode);
20712110
if (node != null) {
@@ -2123,38 +2162,38 @@ class _ConstraintRenderBox extends RenderBox
21232162
childParentData._constrainedNodeMap = nodesMap;
21242163

21252164
_ConstrainedNode currentNode = _getConstrainedNodeForChild(
2126-
childParentData.id ?? RelativeConstraintId(childIndex));
2165+
childParentData.id ?? IndexConstraintId(childIndex));
21272166
currentNode.parentData = childParentData;
21282167
currentNode.index = childIndex;
21292168
currentNode.renderBox = child;
21302169

21312170
if (childParentData.left != null) {
21322171
currentNode.leftConstraint =
2133-
_getConstrainedNodeForChild(childParentData.left!.id!);
2172+
_getConstrainedNodeForChild(childParentData.left!.id!, childIndex);
21342173
currentNode.leftAlignType = childParentData.left!.type;
21352174
}
21362175

21372176
if (childParentData.top != null) {
21382177
currentNode.topConstraint =
2139-
_getConstrainedNodeForChild(childParentData.top!.id!);
2178+
_getConstrainedNodeForChild(childParentData.top!.id!, childIndex);
21402179
currentNode.topAlignType = childParentData.top!.type;
21412180
}
21422181

21432182
if (childParentData.right != null) {
21442183
currentNode.rightConstraint =
2145-
_getConstrainedNodeForChild(childParentData.right!.id!);
2184+
_getConstrainedNodeForChild(childParentData.right!.id!, childIndex);
21462185
currentNode.rightAlignType = childParentData.right!.type;
21472186
}
21482187

21492188
if (childParentData.bottom != null) {
2150-
currentNode.bottomConstraint =
2151-
_getConstrainedNodeForChild(childParentData.bottom!.id!);
2189+
currentNode.bottomConstraint = _getConstrainedNodeForChild(
2190+
childParentData.bottom!.id!, childIndex);
21522191
currentNode.bottomAlignType = childParentData.bottom!.type;
21532192
}
21542193

21552194
if (childParentData.baseline != null) {
2156-
currentNode.baselineConstraint =
2157-
_getConstrainedNodeForChild(childParentData.baseline!.id!);
2195+
currentNode.baselineConstraint = _getConstrainedNodeForChild(
2196+
childParentData.baseline!.id!, childIndex);
21582197
currentNode.baselineAlignType = childParentData.baseline!.type;
21592198
}
21602199

0 commit comments

Comments
 (0)