Skip to content

Commit 3ed2cb1

Browse files
author
fbchen
committed
add percentage layout support
1 parent 095b323 commit 3ed2cb1

File tree

3 files changed

+64
-13
lines changed

3 files changed

+64
-13
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ ConstraintLayout will only be measured once, This results in extremely high layo
2828
6. bias
2929
7. z-index
3030
8. translate
31+
9. percentage layout
32+
10. guideline
3133

3234
Coming soon:
3335

34-
1. guideline
35-
2. barrier
36-
3. constraints visualization
37-
4. chain
38-
5. percentage layout
39-
6. more...
36+
1. barrier
37+
2. constraints visualization
38+
3. chain
39+
4. more...
4040

4141
Support platform:
4242

lib/constrait_layout/constraint_layout.dart

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ import 'package:flutter/rendering.dart';
5151
/// 5. constraint integrity hint
5252
/// 6. bias
5353
/// 7. z-index
54+
/// 8. percentage layout
55+
/// 9. guideline
5456
///
5557
/// not implement
5658
/// . guideline
@@ -135,6 +137,7 @@ List<Constrained> hChain({
135137
}) {
136138
assert(hChainList.length > 1,
137139
'The number of child elements in the chain must be > 1.');
140+
for (final constrained in hChainList) {}
138141
return [];
139142
}
140143

@@ -232,6 +235,8 @@ class _ConstraintBoxData extends ContainerBoxParentData<RenderBox> {
232235
String? baselineToBottom;
233236
String? baselineToBaseline;
234237
TextBaseline? textBaseline;
238+
double? widthPercent;
239+
double? heightPercent;
235240
}
236241

237242
class Constrained extends ParentDataWidget<_ConstraintBoxData> {
@@ -283,6 +288,12 @@ class Constrained extends ParentDataWidget<_ConstraintBoxData> {
283288
final Offset translate;
284289
final bool translateConstraint;
285290

291+
// only takes effect when width is CL.matchConstraint
292+
final double widthPercent;
293+
294+
// only takes effect when height is CL.matchConstraint
295+
final double heightPercent;
296+
286297
// TODO support chain
287298
// final ChainStyle? chainStyle;
288299
// TODO support circle positioned
@@ -322,7 +333,15 @@ class Constrained extends ParentDataWidget<_ConstraintBoxData> {
322333
this.baselineToBottom,
323334
this.baselineToBaseline,
324335
this.textBaseline = TextBaseline.alphabetic,
325-
}) : super(
336+
this.widthPercent = 1,
337+
this.heightPercent = 1,
338+
}) : assert(child is! Constrained,
339+
'Constrained can not be wrapped with Constrained.'),
340+
assert(child is! Guideline,
341+
'Guideline can not be wrapped with Constrained.'),
342+
assert(
343+
child is! Barrier, 'Barrier can not be wrapped with Constrained.'),
344+
super(
326345
key: key,
327346
child: child,
328347
);
@@ -343,9 +362,6 @@ class Constrained extends ParentDataWidget<_ConstraintBoxData> {
343362

344363
@override
345364
void applyParentData(RenderObject renderObject) {
346-
assert(renderObject is! _InternalBox,
347-
'Guideline, Barrier can not be wrapped with Constrained.');
348-
349365
// bounds check
350366
assert(checkSize(width));
351367
assert(checkSize(height));
@@ -365,6 +381,8 @@ class Constrained extends ParentDataWidget<_ConstraintBoxData> {
365381
'baselineToBaseline', this.baselineToBaseline));
366382
assert(_debugEnsurePercent('horizontalBias', horizontalBias));
367383
assert(_debugEnsurePercent('verticalBias', verticalBias));
384+
assert(_debugEnsurePercent('widthPercent', widthPercent));
385+
assert(_debugEnsurePercent('heightPercent', heightPercent));
368386

369387
String? leftToLeft = this.leftToLeft;
370388
String? leftToRight = this.leftToRight;
@@ -641,6 +659,16 @@ class Constrained extends ParentDataWidget<_ConstraintBoxData> {
641659
}
642660
}
643661

662+
if (parentData.widthPercent != widthPercent) {
663+
parentData.widthPercent = widthPercent;
664+
needsLayout = true;
665+
}
666+
667+
if (parentData.heightPercent != heightPercent) {
668+
parentData.heightPercent = heightPercent;
669+
needsLayout = true;
670+
}
671+
644672
if (needsLayout) {
645673
AbstractNode? targetParent = renderObject.parent;
646674
if (targetParent is RenderObject) {
@@ -1232,7 +1260,7 @@ class _ConstraintRenderBox extends RenderBox
12321260
} else {
12331261
right -= _getRightInsets(margin);
12341262
}
1235-
minWidth = right - left;
1263+
minWidth = (right - left) * element.widthPercent;
12361264
assert(() {
12371265
if (_debugCheckConstraints) {
12381266
if (minWidth < 0) {
@@ -1287,7 +1315,7 @@ class _ConstraintRenderBox extends RenderBox
12871315
} else {
12881316
bottom -= _getBottomInsets(margin);
12891317
}
1290-
minHeight = bottom - top;
1318+
minHeight = (bottom - top) * element.heightPercent;
12911319
assert(() {
12921320
if (_debugCheckConstraints) {
12931321
if (minHeight < 0) {
@@ -1677,6 +1705,10 @@ class _ConstrainedNode {
16771705

16781706
TextBaseline get textBaseline => parentData.textBaseline!;
16791707

1708+
double get widthPercent => parentData.widthPercent!;
1709+
1710+
double get heightPercent => parentData.heightPercent!;
1711+
16801712
set offset(Offset value) {
16811713
parentData.offset = value;
16821714
}

lib/example.dart

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,26 @@ class ExampleState extends State<Example> {
181181
),
182182
),
183183
],
184-
)
184+
),
185+
Constrained(
186+
id: 'box12',
187+
width: CL.matchConstraint,
188+
height: CL.matchConstraint,
189+
leftToLeft: CL.parent,
190+
rightToRight: CL.parent,
191+
topToTop: CL.parent,
192+
bottomToBottom: CL.parent,
193+
widthPercent: 0.5,
194+
heightPercent: 0.3,
195+
translate: Offset(x, y),
196+
zIndex: 6,
197+
child: Container(
198+
color: Colors.yellow,
199+
alignment: Alignment.bottomCenter,
200+
child: const Text(
201+
'percentage layout, width: 50% of parent, height: 30% of parent'),
202+
),
203+
),
185204
],
186205
),
187206
),

0 commit comments

Comments
 (0)