Skip to content

Commit 6b783f7

Browse files
committed
support min、max size set
1 parent 5357e82 commit 6b783f7

File tree

4 files changed

+93
-12
lines changed

4 files changed

+93
-12
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
v.0.8.9-stable:
2+
support min、max size set.
3+
14
v.0.8.8-stable:
25
make parent final.
36

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,12 @@ dependencies:
7979
flutter_constraintlayout:
8080
git:
8181
url: 'https://github.com/hackware1993/Flutter-ConstraintLayout.git'
82-
ref: 'v0.8.8-stable'
82+
ref: 'v0.8.9-stable'
8383
```
8484
8585
```yaml
8686
dependencies:
87-
flutter_constraintlayout: ^0.8.8-stable
87+
flutter_constraintlayout: ^0.8.9-stable
8888
```
8989
9090
```dart

lib/src/constraint_layout.dart

Lines changed: 87 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,10 @@ extension WidgetsExt on Widget {
175175
OnLayoutCallback? callback,
176176
double chainWeight = 1,
177177
bool percentageTranslate = false,
178+
double minWidth = 0,
179+
double maxWidth = matchParent,
180+
double minHeight = 0,
181+
double maxHeight = matchParent,
178182
}) {
179183
return Constrained(
180184
key: key,
@@ -216,6 +220,10 @@ extension WidgetsExt on Widget {
216220
callback: callback,
217221
chainWeight: chainWeight,
218222
percentageTranslate: percentageTranslate,
223+
minWidth: minWidth,
224+
maxWidth: maxWidth,
225+
minHeight: minHeight,
226+
maxHeight: maxHeight,
219227
),
220228
child: this,
221229
);
@@ -449,6 +457,14 @@ class Constraint {
449457
final double chainWeight;
450458
final bool percentageTranslate;
451459

460+
/// Only takes effect when width is wrapContent
461+
final double minWidth;
462+
final double maxWidth;
463+
464+
/// Only takes effect when height is wrapContent
465+
final double minHeight;
466+
final double maxHeight;
467+
452468
Constraint({
453469
this.id,
454470
this.width = wrapContent,
@@ -487,6 +503,10 @@ class Constraint {
487503
this.callback,
488504
this.chainWeight = 1,
489505
this.percentageTranslate = false,
506+
this.minWidth = 0,
507+
this.maxWidth = matchParent,
508+
this.minHeight = 0,
509+
this.maxHeight = matchParent,
490510
});
491511

492512
@override
@@ -529,7 +549,11 @@ class Constraint {
529549
centerHorizontalTo == other.centerHorizontalTo &&
530550
centerVerticalTo == other.centerVerticalTo &&
531551
callback == other.callback &&
532-
percentageTranslate == other.percentageTranslate;
552+
percentageTranslate == other.percentageTranslate &&
553+
minWidth == other.minWidth &&
554+
maxWidth == other.maxWidth &&
555+
minHeight == other.minHeight &&
556+
maxHeight == other.maxHeight;
533557

534558
@override
535559
int get hashCode =>
@@ -568,7 +592,11 @@ class Constraint {
568592
centerHorizontalTo.hashCode ^
569593
centerVerticalTo.hashCode ^
570594
callback.hashCode ^
571-
percentageTranslate.hashCode;
595+
percentageTranslate.hashCode ^
596+
minWidth.hashCode ^
597+
maxWidth.hashCode ^
598+
minHeight.hashCode ^
599+
maxHeight.hashCode;
572600

573601
bool checkSize(double size) {
574602
if (size == matchParent || size == wrapContent || size == matchConstraint) {
@@ -621,6 +649,10 @@ class Constraint {
621649
_debugEnsureNegativePercent('xTranslate', translate.dx));
622650
assert(!percentageTranslate ||
623651
_debugEnsureNegativePercent('yTranslate', translate.dy));
652+
assert(minWidth >= 0);
653+
assert(maxWidth == matchParent || maxWidth >= minWidth);
654+
assert(minHeight >= 0);
655+
assert(maxHeight == matchParent || maxHeight >= minHeight);
624656
return true;
625657
}
626658

@@ -856,6 +888,26 @@ class Constraint {
856888
needsPaint = true;
857889
}
858890

891+
if (parentData.minWidth != minWidth) {
892+
parentData.minWidth = minWidth;
893+
needsLayout = true;
894+
}
895+
896+
if (parentData.maxWidth != maxWidth) {
897+
parentData.maxWidth = maxWidth;
898+
needsLayout = true;
899+
}
900+
901+
if (parentData.minHeight != minHeight) {
902+
parentData.minHeight = minHeight;
903+
needsLayout = true;
904+
}
905+
906+
if (parentData.maxHeight != maxHeight) {
907+
parentData.maxHeight = maxHeight;
908+
needsLayout = true;
909+
}
910+
859911
if (needsLayout) {
860912
AbstractNode? targetParent = renderObject.parent;
861913
if (targetParent is RenderObject) {
@@ -912,6 +964,10 @@ class _ConstraintBoxData extends ContainerBoxParentData<RenderBox> {
912964
double? verticalBias;
913965
OnLayoutCallback? callback;
914966
bool? percentageTranslate;
967+
double? minWidth;
968+
double? maxWidth;
969+
double? minHeight;
970+
double? maxHeight;
915971

916972
// for internal use
917973
late Map<ConstraintId, _ConstrainedNode> _tempConstrainedNodes;
@@ -1440,19 +1496,24 @@ class _ConstraintRenderBox extends RenderBox
14401496
EdgeInsets goneMargin = element.goneMargin;
14411497

14421498
/// Calculate child width
1443-
double minWidth = 0;
1444-
double maxWidth = double.infinity;
1445-
double minHeight = 0;
1446-
double maxHeight = double.infinity;
1499+
double minWidth;
1500+
double maxWidth;
1501+
double minHeight;
1502+
double maxHeight;
14471503
if (element.visibility == gone) {
14481504
minWidth = 0;
14491505
maxWidth = 0;
1450-
minWidth = 0;
1506+
minHeight = 0;
14511507
maxHeight = 0;
14521508
} else {
14531509
double width = element.width;
14541510
if (width == wrapContent) {
1455-
maxWidth = size.width;
1511+
minWidth = element.minWidth;
1512+
if (element.maxWidth == matchParent) {
1513+
maxWidth = size.width;
1514+
} else {
1515+
maxWidth = element.maxWidth;
1516+
}
14561517
} else if (width == matchParent) {
14571518
minWidth = size.width -
14581519
_getHorizontalInsets(
@@ -1523,7 +1584,12 @@ class _ConstraintRenderBox extends RenderBox
15231584
/// Calculate child height
15241585
double height = element.height;
15251586
if (height == wrapContent) {
1526-
maxHeight = size.height;
1587+
minHeight = element.minHeight;
1588+
if (element.maxHeight == matchParent) {
1589+
maxHeight = size.height;
1590+
} else {
1591+
maxHeight = element.maxHeight;
1592+
}
15271593
} else if (height == matchParent) {
15281594
minHeight = size.height -
15291595
_getVerticalInsets(margin, element.percentageMargin, size.height);
@@ -2165,6 +2231,14 @@ class _ConstrainedNode {
21652231

21662232
bool get percentageMargin => parentData.percentageMargin!;
21672233

2234+
double get minWidth => parentData.minWidth!;
2235+
2236+
double get maxWidth => parentData.maxWidth!;
2237+
2238+
double get minHeight => parentData.minHeight!;
2239+
2240+
double get maxHeight => parentData.maxHeight!;
2241+
21682242
PercentageAnchor get widthPercentageAnchor =>
21692243
parentData.widthPercentageAnchor!;
21702244

@@ -2402,6 +2476,10 @@ class _InternalBox extends RenderBox {
24022476
constraintBoxData._direction = null;
24032477
constraintBoxData._referencedIds = null;
24042478
constraintBoxData.percentageTranslate = false;
2479+
constraintBoxData.minWidth = 0;
2480+
constraintBoxData.maxWidth = double.infinity;
2481+
constraintBoxData.minHeight = 0;
2482+
constraintBoxData.maxHeight = double.infinity;
24052483
}
24062484
}
24072485

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: flutter_constraintlayout
22
description: Build flexible layouts with constraints, Similar to Android ConstraintLayout.
3-
version: 0.8.8-stable
3+
version: 0.8.9-stable
44
anthor: hackware
55
homepage: https://github.com/hackware1993/Flutter-ConstraintLayout
66

0 commit comments

Comments
 (0)