Skip to content

Commit aa985ff

Browse files
committed
enhance base constraints
1 parent 4bdf0e2 commit aa985ff

File tree

1 file changed

+95
-8
lines changed

1 file changed

+95
-8
lines changed

lib/src/constraint_layout.dart

Lines changed: 95 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,8 @@ class ConstraintId {
766766
.._margin = _topMargin
767767
.._goneMargin = _topGoneMargin;
768768

769+
late final _Align center = _Align(this, AlignType.center);
770+
769771
@override
770772
bool operator ==(Object other) {
771773
if (identical(this, other)) {
@@ -834,11 +836,12 @@ class _Align {
834836
final AlignType _type;
835837
double? _margin;
836838
double? _goneMargin;
839+
double? _bias;
837840

838841
_Align(this._id, this._type);
839842

840843
_Align margin(double margin) {
841-
if (_margin != null || _goneMargin != null) {
844+
if (_margin != null || _goneMargin != null || _bias != null) {
842845
_margin = margin;
843846
return this;
844847
} else {
@@ -847,24 +850,34 @@ class _Align {
847850
}
848851

849852
_Align goneMargin(double goneMargin) {
850-
if (_margin != null || _goneMargin != null) {
853+
if (_margin != null || _goneMargin != null || _bias != null) {
851854
_goneMargin = goneMargin;
852855
return this;
853856
} else {
854857
return _Align(_id, _type).._goneMargin = goneMargin;
855858
}
856859
}
857860

861+
_Align bias(double bias) {
862+
if (_margin != null || _goneMargin != null || _bias != null) {
863+
_bias = bias;
864+
return this;
865+
} else {
866+
return _Align(_id, _type).._bias = bias;
867+
}
868+
}
869+
858870
@override
859871
bool operator ==(Object other) =>
860872
identical(this, other) ||
861873
other is _Align &&
862874
runtimeType == other.runtimeType &&
863875
_id == other._id &&
864-
_type == other._type;
876+
_type == other._type &&
877+
_bias == other._bias;
865878

866879
@override
867-
int get hashCode => _id.hashCode ^ _type.hashCode;
880+
int get hashCode => _id.hashCode ^ _type.hashCode ^ _bias.hashCode;
868881
}
869882

870883
typedef OnLayoutCallback = void Function(RenderObject renderObject, Rect rect);
@@ -1428,15 +1441,24 @@ class Constraint extends ConstraintDefine {
14281441
assert(_checkSize(height));
14291442
assert(size == null || _checkSize(size!));
14301443
assert(left == null ||
1431-
(left!._type == AlignType.left || left!._type == AlignType.right));
1444+
(left!._type == AlignType.left ||
1445+
left!._type == AlignType.center ||
1446+
left!._type == AlignType.right));
14321447
assert(top == null ||
1433-
(top!._type == AlignType.top || top!._type == AlignType.bottom));
1448+
(top!._type == AlignType.top ||
1449+
top!._type == AlignType.center ||
1450+
top!._type == AlignType.bottom));
14341451
assert(right == null ||
1435-
(right!._type == AlignType.left || right!._type == AlignType.right));
1452+
(right!._type == AlignType.left ||
1453+
right!._type == AlignType.center ||
1454+
right!._type == AlignType.right));
14361455
assert(bottom == null ||
1437-
(bottom!._type == AlignType.top || bottom!._type == AlignType.bottom));
1456+
(bottom!._type == AlignType.top ||
1457+
bottom!._type == AlignType.center ||
1458+
bottom!._type == AlignType.bottom));
14381459
assert(baseline == null ||
14391460
(baseline!._type == AlignType.top ||
1461+
baseline!._type == AlignType.center ||
14401462
baseline!._type == AlignType.bottom ||
14411463
baseline!._type == AlignType.baseline));
14421464
assert(_debugEnsurePercent('widthPercent', widthPercent));
@@ -2085,6 +2107,7 @@ enum AlignType {
20852107
top,
20862108
bottom,
20872109
baseline,
2110+
center,
20882111
}
20892112

20902113
class ConstraintBoxData extends ContainerBoxParentData<RenderBox> {
@@ -3362,12 +3385,18 @@ class _ConstraintRenderBox extends RenderBox
33623385
double left;
33633386
if (node.leftAlignType == AlignType.left) {
33643387
left = node.leftConstraint!.getX();
3388+
} else if (node.leftAlignType == AlignType.center) {
3389+
left = node.leftConstraint!
3390+
.getXPercent(node.parentData.left!._bias ?? 0.5, this);
33653391
} else {
33663392
left = node.leftConstraint!.getRight(this);
33673393
}
33683394
double right;
33693395
if (node.rightAlignType == AlignType.left) {
33703396
right = node.rightConstraint!.getX();
3397+
} else if (node.rightAlignType == AlignType.center) {
3398+
right = node.rightConstraint!
3399+
.getXPercent(node.parentData.right!._bias ?? 0.5, this);
33713400
} else {
33723401
right = node.rightConstraint!.getRight(this);
33733402
}
@@ -3454,12 +3483,18 @@ class _ConstraintRenderBox extends RenderBox
34543483
double top;
34553484
if (node.topAlignType == AlignType.top) {
34563485
top = node.topConstraint!.getY();
3486+
} else if (node.topAlignType == AlignType.center) {
3487+
top = node.topConstraint!
3488+
.getYPercent(node.parentData.top!._bias ?? 0.5, this);
34573489
} else {
34583490
top = node.topConstraint!.getBottom(this);
34593491
}
34603492
double bottom;
34613493
if (node.bottomAlignType == AlignType.top) {
34623494
bottom = node.bottomConstraint!.getY();
3495+
} else if (node.bottomAlignType == AlignType.center) {
3496+
bottom = node.bottomConstraint!
3497+
.getYPercent(node.parentData.bottom!._bias ?? 0.5, this);
34633498
} else {
34643499
bottom = node.bottomConstraint!.getBottom(this);
34653500
}
@@ -3617,12 +3652,18 @@ class _ConstraintRenderBox extends RenderBox
36173652
double left;
36183653
if (node.leftAlignType == AlignType.left) {
36193654
left = node.leftConstraint!.getX();
3655+
} else if (node.leftAlignType == AlignType.center) {
3656+
left = node.leftConstraint!
3657+
.getXPercent(node.parentData.left!._bias ?? 0.5, this);
36203658
} else {
36213659
left = node.leftConstraint!.getRight(this);
36223660
}
36233661
double right;
36243662
if (node.rightAlignType == AlignType.left) {
36253663
right = node.rightConstraint!.getX();
3664+
} else if (node.rightAlignType == AlignType.center) {
3665+
right = node.rightConstraint!
3666+
.getXPercent(node.parentData.right!._bias ?? 0.5, this);
36263667
} else {
36273668
right = node.rightConstraint!.getRight(this);
36283669
}
@@ -3654,6 +3695,9 @@ class _ConstraintRenderBox extends RenderBox
36543695
double left;
36553696
if (node.leftAlignType == AlignType.left) {
36563697
left = node.leftConstraint!.getX();
3698+
} else if (node.leftAlignType == AlignType.center) {
3699+
left = node.leftConstraint!
3700+
.getXPercent(node.parentData.left!._bias ?? 0.5, this);
36573701
} else {
36583702
left = node.leftConstraint!.getRight(this);
36593703
}
@@ -3667,6 +3711,9 @@ class _ConstraintRenderBox extends RenderBox
36673711
double right;
36683712
if (node.rightAlignType == AlignType.left) {
36693713
right = node.rightConstraint!.getX();
3714+
} else if (node.rightAlignType == AlignType.center) {
3715+
right = node.rightConstraint!
3716+
.getXPercent(node.parentData.right!._bias ?? 0.5, this);
36703717
} else {
36713718
right = node.rightConstraint!.getRight(this);
36723719
}
@@ -3686,12 +3733,18 @@ class _ConstraintRenderBox extends RenderBox
36863733
double top;
36873734
if (node.topAlignType == AlignType.top) {
36883735
top = node.topConstraint!.getY();
3736+
} else if (node.topAlignType == AlignType.center) {
3737+
top = node.topConstraint!
3738+
.getYPercent(node.parentData.top!._bias ?? 0.5, this);
36893739
} else {
36903740
top = node.topConstraint!.getBottom(this);
36913741
}
36923742
double bottom;
36933743
if (node.bottomAlignType == AlignType.top) {
36943744
bottom = node.bottomConstraint!.getY();
3745+
} else if (node.bottomAlignType == AlignType.center) {
3746+
bottom = node.bottomConstraint!
3747+
.getYPercent(node.parentData.bottom!._bias ?? 0.5, this);
36953748
} else {
36963749
bottom = node.bottomConstraint!.getBottom(this);
36973750
}
@@ -3722,6 +3775,9 @@ class _ConstraintRenderBox extends RenderBox
37223775
double top;
37233776
if (node.topAlignType == AlignType.top) {
37243777
top = node.topConstraint!.getY();
3778+
} else if (node.topAlignType == AlignType.center) {
3779+
top = node.topConstraint!
3780+
.getYPercent(node.parentData.top!._bias ?? 0.5, this);
37253781
} else {
37263782
top = node.topConstraint!.getBottom(this);
37273783
}
@@ -3735,6 +3791,9 @@ class _ConstraintRenderBox extends RenderBox
37353791
double bottom;
37363792
if (node.bottomAlignType == AlignType.top) {
37373793
bottom = node.bottomConstraint!.getY();
3794+
} else if (node.bottomAlignType == AlignType.center) {
3795+
bottom = node.bottomConstraint!
3796+
.getYPercent(node.parentData.bottom!._bias ?? 0.5, this);
37383797
} else {
37393798
bottom = node.bottomConstraint!.getBottom(this);
37403799
}
@@ -3749,6 +3808,10 @@ class _ConstraintRenderBox extends RenderBox
37493808
if (node.baselineAlignType == AlignType.top) {
37503809
offsetY = node.baselineConstraint!.getY() -
37513810
node.getDistanceToBaseline(node.textBaseline, false);
3811+
} else if (node.baselineAlignType == AlignType.center) {
3812+
offsetY = node.baselineConstraint!
3813+
.getYPercent(node.parentData.baseline!._bias ?? 0.5) -
3814+
node.getDistanceToBaseline(node.textBaseline, false);
37523815
} else if (node.baselineAlignType == AlignType.bottom) {
37533816
offsetY = node.baselineConstraint!.getBottom(this) -
37543817
node.getDistanceToBaseline(node.textBaseline, false);
@@ -4286,13 +4349,27 @@ class ConstrainedNode {
42864349
return offset.dx;
42874350
}
42884351

4352+
double getXPercent(double percent, [RenderBox? parent]) {
4353+
if (isParent()) {
4354+
return parent!.size.width * percent;
4355+
}
4356+
return offset.dx + getMeasuredWidth() * percent;
4357+
}
4358+
42894359
double getY() {
42904360
if (isParent()) {
42914361
return 0;
42924362
}
42934363
return offset.dy;
42944364
}
42954365

4366+
double getYPercent(double percent, [RenderBox? parent]) {
4367+
if (isParent()) {
4368+
return parent!.size.height * percent;
4369+
}
4370+
return offset.dy + getMeasuredHeight() * percent;
4371+
}
4372+
42964373
double getRight([RenderBox? parent]) {
42974374
if (isParent()) {
42984375
return parent!.size.width;
@@ -4427,6 +4504,8 @@ class ConstrainedNode {
44274504
if (leftConstraint != null) {
44284505
if (leftAlignType == AlignType.left) {
44294506
map['leftAlignType'] = 'toLeft';
4507+
} else if (leftAlignType == AlignType.center) {
4508+
map['leftAlignType'] = 'toCenter';
44304509
} else {
44314510
map['leftAlignType'] = 'toRight';
44324511
}
@@ -4439,6 +4518,8 @@ class ConstrainedNode {
44394518
if (topConstraint != null) {
44404519
if (topAlignType == AlignType.top) {
44414520
map['topAlignType'] = 'toTop';
4521+
} else if (topAlignType == AlignType.center) {
4522+
map['topAlignType'] = 'toCenter';
44424523
} else {
44434524
map['topAlignType'] = 'toBottom';
44444525
}
@@ -4451,6 +4532,8 @@ class ConstrainedNode {
44514532
if (rightConstraint != null) {
44524533
if (rightAlignType == AlignType.left) {
44534534
map['rightAlignType'] = 'toLeft';
4535+
} else if (rightAlignType == AlignType.center) {
4536+
map['rightAlignType'] = 'toCenter';
44544537
} else {
44554538
map['rightAlignType'] = 'toRight';
44564539
}
@@ -4463,6 +4546,8 @@ class ConstrainedNode {
44634546
if (bottomConstraint != null) {
44644547
if (bottomAlignType == AlignType.top) {
44654548
map['bottomAlignType'] = 'toTop';
4549+
} else if (bottomAlignType == AlignType.center) {
4550+
map['bottomAlignType'] = 'toCenter';
44664551
} else {
44674552
map['bottomAlignType'] = 'toBottom';
44684553
}
@@ -4475,6 +4560,8 @@ class ConstrainedNode {
44754560
if (baselineConstraint != null) {
44764561
if (baselineAlignType == AlignType.top) {
44774562
map['baselineAlignType'] = 'toTop';
4563+
} else if (baselineAlignType == AlignType.center) {
4564+
map['baselineAlignType'] = 'toCenter';
44784565
} else if (baselineAlignType == AlignType.bottom) {
44794566
map['baselineAlignType'] = 'toBottom';
44804567
} else {

0 commit comments

Comments
 (0)