Skip to content

Commit e5da666

Browse files
author
fbchen
committed
enhance margin again
1 parent 400b582 commit e5da666

File tree

4 files changed

+159
-69
lines changed

4 files changed

+159
-69
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -905,6 +905,12 @@ class MarginExample extends StatelessWidget {
905905
top: sId(-1).bottom,
906906
right: parent.right.margin(100),
907907
),
908+
Container(
909+
color: Colors.pink,
910+
).applyConstraint(
911+
size: 50,
912+
topRightTo: parent.rightMargin(20).topMargin(50),
913+
),
908914
],
909915
),
910916
);

example/margin.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ class MarginExample extends StatelessWidget {
3232
top: sId(-1).bottom,
3333
right: parent.right.margin(100),
3434
),
35+
Container(
36+
color: Colors.pink,
37+
).applyConstraint(
38+
size: 50,
39+
topRightTo: parent.rightMargin(20).topMargin(50),
40+
),
3541
],
3642
),
3743
);

lib/src/constraint_layout.dart

Lines changed: 147 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -668,16 +668,106 @@ enum PercentageAnchor {
668668

669669
class ConstraintId {
670670
String id;
671+
double? _leftMargin;
672+
double? _topMargin;
673+
double? _rightMargin;
674+
double? _bottomMargin;
675+
double? _leftGoneMargin;
676+
double? _topGoneMargin;
677+
double? _rightGoneMargin;
678+
double? _bottomGoneMargin;
671679

672680
_ConstrainedNode? contextCacheNode;
673681
int? contextHash;
674682

675-
ConstraintId(this.id) {
676-
left.id = this;
677-
top.id = this;
678-
right.id = this;
679-
bottom.id = this;
680-
baseline.id = this;
683+
ConstraintId(this.id);
684+
685+
@protected
686+
ConstraintId copy() {
687+
return ConstraintId(id);
688+
}
689+
690+
bool _isMarginSet() {
691+
return _leftMargin != null ||
692+
_topMargin != null ||
693+
_rightMargin != null ||
694+
_bottomMargin != null ||
695+
_leftGoneMargin != null ||
696+
_topGoneMargin != null ||
697+
_rightGoneMargin != null ||
698+
_bottomGoneMargin != null;
699+
}
700+
701+
ConstraintId leftMargin(double margin) {
702+
if (_isMarginSet()) {
703+
_leftMargin = margin;
704+
return this;
705+
} else {
706+
return copy().._leftMargin = margin;
707+
}
708+
}
709+
710+
ConstraintId topMargin(double margin) {
711+
if (_isMarginSet()) {
712+
_topMargin = margin;
713+
return this;
714+
} else {
715+
return copy().._topMargin = margin;
716+
}
717+
}
718+
719+
ConstraintId rightMargin(double margin) {
720+
if (_isMarginSet()) {
721+
_rightMargin = margin;
722+
return this;
723+
} else {
724+
return copy().._rightMargin = margin;
725+
}
726+
}
727+
728+
ConstraintId bottomMargin(double margin) {
729+
if (_isMarginSet()) {
730+
_bottomMargin = margin;
731+
return this;
732+
} else {
733+
return copy().._bottomMargin = margin;
734+
}
735+
}
736+
737+
ConstraintId leftGoneMargin(double margin) {
738+
if (_isMarginSet()) {
739+
_leftGoneMargin = margin;
740+
return this;
741+
} else {
742+
return copy().._leftGoneMargin = margin;
743+
}
744+
}
745+
746+
ConstraintId topGoneMargin(double margin) {
747+
if (_isMarginSet()) {
748+
_topGoneMargin = margin;
749+
return this;
750+
} else {
751+
return copy().._topGoneMargin = margin;
752+
}
753+
}
754+
755+
ConstraintId rightGoneMargin(double margin) {
756+
if (_isMarginSet()) {
757+
_rightGoneMargin = margin;
758+
return this;
759+
} else {
760+
return copy().._rightGoneMargin = margin;
761+
}
762+
}
763+
764+
ConstraintId bottomGoneMargin(double margin) {
765+
if (_isMarginSet()) {
766+
_bottomGoneMargin = margin;
767+
return this;
768+
} else {
769+
return copy().._bottomGoneMargin = margin;
770+
}
681771
}
682772

683773
_ConstrainedNode? getCacheNode(int hash) {
@@ -692,15 +782,25 @@ class ConstraintId {
692782
contextCacheNode = node;
693783
}
694784

695-
_Align left = _Align(null, _AlignType.left);
785+
late _Align left = _Align(this, _AlignType.left)
786+
.._margin = _leftMargin
787+
.._goneMargin = _leftGoneMargin;
696788

697-
_Align top = _Align(null, _AlignType.top);
789+
late _Align top = _Align(this, _AlignType.top)
790+
.._margin = _topMargin
791+
.._goneMargin = _topGoneMargin;
698792

699-
_Align right = _Align(null, _AlignType.right);
793+
late _Align right = _Align(this, _AlignType.right)
794+
.._margin = _rightMargin
795+
.._goneMargin = _rightGoneMargin;
700796

701-
_Align bottom = _Align(null, _AlignType.bottom);
797+
late _Align bottom = _Align(this, _AlignType.bottom)
798+
.._margin = _bottomMargin
799+
.._goneMargin = _bottomGoneMargin;
702800

703-
_Align baseline = _Align(null, _AlignType.baseline);
801+
late _Align baseline = _Align(this, _AlignType.baseline)
802+
.._margin = _bottomMargin
803+
.._goneMargin = _bottomGoneMargin;
704804

705805
@override
706806
bool operator ==(Object other) {
@@ -727,63 +827,41 @@ class ConstraintId {
727827
}
728828
}
729829

730-
class IndexConstraintId extends ConstraintId {
830+
class _IndexConstraintId extends ConstraintId {
731831
final int siblingIndex;
732832

733-
IndexConstraintId(this.siblingIndex)
833+
_IndexConstraintId(this.siblingIndex)
734834
: super('parent.children[$siblingIndex]');
735835

736836
@override
737-
bool operator ==(Object other) =>
738-
identical(this, other) ||
739-
super == other &&
740-
other is IndexConstraintId &&
741-
runtimeType == other.runtimeType &&
742-
siblingIndex == other.siblingIndex;
743-
744-
@override
745-
int get hashCode => super.hashCode ^ siblingIndex.hashCode;
746-
747-
@override
748-
String toString() {
749-
return 'IndexConstraintId{siblingIndex: $siblingIndex}';
837+
ConstraintId copy() {
838+
return _IndexConstraintId(siblingIndex);
750839
}
751840
}
752841

753842
ConstraintId rId(int childIndex) {
754-
return IndexConstraintId(childIndex);
843+
assert(childIndex >= 0);
844+
return _IndexConstraintId(childIndex);
755845
}
756846

757-
class RelativeConstraintId extends ConstraintId {
847+
class _RelativeConstraintId extends ConstraintId {
758848
final int siblingIndexOffset;
759849

760-
RelativeConstraintId(this.siblingIndexOffset)
761-
: super('sibling@$siblingIndexOffset');
850+
_RelativeConstraintId(this.siblingIndexOffset) : super('$siblingIndexOffset');
762851

763852
@override
764-
bool operator ==(Object other) =>
765-
identical(this, other) ||
766-
super == other &&
767-
other is RelativeConstraintId &&
768-
runtimeType == other.runtimeType &&
769-
siblingIndexOffset == other.siblingIndexOffset;
770-
771-
@override
772-
int get hashCode => super.hashCode ^ siblingIndexOffset.hashCode;
773-
774-
@override
775-
String toString() {
776-
return 'RelativeConstraintId{siblingIndexOffset: $siblingIndexOffset}';
853+
ConstraintId copy() {
854+
return _RelativeConstraintId(siblingIndexOffset);
777855
}
778856
}
779857

780858
ConstraintId sId(int siblingIndexOffset) {
781859
assert(siblingIndexOffset != 0);
782-
return RelativeConstraintId(siblingIndexOffset);
860+
return _RelativeConstraintId(siblingIndexOffset);
783861
}
784862

785863
class _Align {
786-
ConstraintId? id;
864+
ConstraintId id;
787865
_AlignType type;
788866
double? _margin;
789867
double? _goneMargin;
@@ -826,8 +904,8 @@ class ConstraintDefine {
826904
final ConstraintId? id;
827905

828906
ConstraintDefine(this.id)
829-
: assert(id is! IndexConstraintId),
830-
assert(id is! RelativeConstraintId);
907+
: assert(id is! _IndexConstraintId),
908+
assert(id is! _RelativeConstraintId);
831909

832910
@override
833911
bool operator ==(Object other) =>
@@ -2110,19 +2188,19 @@ class _ConstraintRenderBox extends RenderBox
21102188
}
21112189
}
21122190
if (childParentData.left != null) {
2113-
constraintsIdSet.add(childParentData.left!.id!);
2191+
constraintsIdSet.add(childParentData.left!.id);
21142192
}
21152193
if (childParentData.top != null) {
2116-
constraintsIdSet.add(childParentData.top!.id!);
2194+
constraintsIdSet.add(childParentData.top!.id);
21172195
}
21182196
if (childParentData.right != null) {
2119-
constraintsIdSet.add(childParentData.right!.id!);
2197+
constraintsIdSet.add(childParentData.right!.id);
21202198
}
21212199
if (childParentData.bottom != null) {
2122-
constraintsIdSet.add(childParentData.bottom!.id!);
2200+
constraintsIdSet.add(childParentData.bottom!.id);
21232201
}
21242202
if (childParentData.baseline != null) {
2125-
constraintsIdSet.add(childParentData.baseline!.id!);
2203+
constraintsIdSet.add(childParentData.baseline!.id);
21262204
}
21272205
if (child is _BarrierRenderBox) {
21282206
constraintsIdSet.addAll(childParentData._referencedIds!);
@@ -2139,10 +2217,10 @@ class _ConstraintRenderBox extends RenderBox
21392217

21402218
/// The id used by all constraints must be defined
21412219
Set<ConstraintId> illegalIdSet = constraintsIdSet.difference(idSet);
2142-
Set<IndexConstraintId> indexIds =
2143-
illegalIdSet.whereType<IndexConstraintId>().toSet();
2144-
Set<RelativeConstraintId> relativeIds =
2145-
illegalIdSet.whereType<RelativeConstraintId>().toSet();
2220+
Set<_IndexConstraintId> indexIds =
2221+
illegalIdSet.whereType<_IndexConstraintId>().toSet();
2222+
Set<_RelativeConstraintId> relativeIds =
2223+
illegalIdSet.whereType<_RelativeConstraintId>().toSet();
21462224
if ((indexIds.length + relativeIds.length) != illegalIdSet.length) {
21472225
throw ConstraintLayoutException(
21482226
'These ids ${illegalIdSet.difference(indexIds).difference(relativeIds)} are not yet defined.');
@@ -2286,9 +2364,9 @@ class _ConstraintRenderBox extends RenderBox
22862364
return parentNode;
22872365
}
22882366

2289-
if (id is RelativeConstraintId) {
2367+
if (id is _RelativeConstraintId) {
22902368
int targetIndex = childIndex! + id.siblingIndexOffset;
2291-
id = IndexConstraintId(targetIndex);
2369+
id = _IndexConstraintId(targetIndex);
22922370
}
22932371

22942372
/// Fewer reads to nodesMap for faster constraint building
@@ -2311,25 +2389,25 @@ class _ConstraintRenderBox extends RenderBox
23112389
for (final element in _helperNodeMap.values) {
23122390
if (element.parentData.left != null) {
23132391
element.leftConstraint =
2314-
_getConstrainedNodeForChild(element.parentData.left!.id!);
2392+
_getConstrainedNodeForChild(element.parentData.left!.id);
23152393
element.leftAlignType = element.parentData.left!.type;
23162394
}
23172395

23182396
if (element.parentData.top != null) {
23192397
element.topConstraint =
2320-
_getConstrainedNodeForChild(element.parentData.top!.id!);
2398+
_getConstrainedNodeForChild(element.parentData.top!.id);
23212399
element.topAlignType = element.parentData.top!.type;
23222400
}
23232401

23242402
if (element.parentData.right != null) {
23252403
element.rightConstraint =
2326-
_getConstrainedNodeForChild(element.parentData.right!.id!);
2404+
_getConstrainedNodeForChild(element.parentData.right!.id);
23272405
element.rightAlignType = element.parentData.right!.type;
23282406
}
23292407

23302408
if (element.parentData.bottom != null) {
23312409
element.bottomConstraint =
2332-
_getConstrainedNodeForChild(element.parentData.bottom!.id!);
2410+
_getConstrainedNodeForChild(element.parentData.bottom!.id);
23332411
element.bottomAlignType = element.parentData.bottom!.type;
23342412
}
23352413

@@ -2348,38 +2426,38 @@ class _ConstraintRenderBox extends RenderBox
23482426
childParentData._constrainedNodeMap = nodesMap;
23492427

23502428
_ConstrainedNode currentNode = _getConstrainedNodeForChild(
2351-
childParentData.id ?? IndexConstraintId(childIndex));
2429+
childParentData.id ?? _IndexConstraintId(childIndex));
23522430
currentNode.parentData = childParentData;
23532431
currentNode.index = childIndex;
23542432
currentNode.renderBox = child;
23552433

23562434
if (childParentData.left != null) {
23572435
currentNode.leftConstraint =
2358-
_getConstrainedNodeForChild(childParentData.left!.id!, childIndex);
2436+
_getConstrainedNodeForChild(childParentData.left!.id, childIndex);
23592437
currentNode.leftAlignType = childParentData.left!.type;
23602438
}
23612439

23622440
if (childParentData.top != null) {
23632441
currentNode.topConstraint =
2364-
_getConstrainedNodeForChild(childParentData.top!.id!, childIndex);
2442+
_getConstrainedNodeForChild(childParentData.top!.id, childIndex);
23652443
currentNode.topAlignType = childParentData.top!.type;
23662444
}
23672445

23682446
if (childParentData.right != null) {
23692447
currentNode.rightConstraint =
2370-
_getConstrainedNodeForChild(childParentData.right!.id!, childIndex);
2448+
_getConstrainedNodeForChild(childParentData.right!.id, childIndex);
23712449
currentNode.rightAlignType = childParentData.right!.type;
23722450
}
23732451

23742452
if (childParentData.bottom != null) {
2375-
currentNode.bottomConstraint = _getConstrainedNodeForChild(
2376-
childParentData.bottom!.id!, childIndex);
2453+
currentNode.bottomConstraint =
2454+
_getConstrainedNodeForChild(childParentData.bottom!.id, childIndex);
23772455
currentNode.bottomAlignType = childParentData.bottom!.type;
23782456
}
23792457

23802458
if (childParentData.baseline != null) {
23812459
currentNode.baselineConstraint = _getConstrainedNodeForChild(
2382-
childParentData.baseline!.id!, childIndex);
2460+
childParentData.baseline!.id, childIndex);
23832461
currentNode.baselineAlignType = childParentData.baseline!.type;
23842462
}
23852463

margin.webp

1.46 KB
Loading

0 commit comments

Comments
 (0)