Skip to content

Commit 29b3f55

Browse files
committed
add self size set
1 parent 67157c3 commit 29b3f55

File tree

3 files changed

+114
-73
lines changed

3 files changed

+114
-73
lines changed

example/home.dart

Lines changed: 54 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:flutter/material.dart';
2+
import 'package:flutter_constraintlayout/flutter_constraintlayout.dart';
23

34
import 'badge.dart';
45
import 'barrier.dart';
@@ -14,12 +15,29 @@ import 'vertical_list.dart';
1415
import 'wrapper_constraints.dart';
1516

1617
class ExampleHome extends StatelessWidget {
17-
const ExampleHome({Key? key}) : super(key: key);
18+
ExampleHome({Key? key}) : super(key: key);
19+
20+
final Map<String, Widget?> exampleMap = {
21+
'Summary': const SummaryExample(),
22+
'Guideline': const GuidelineExample(),
23+
'Barrier': const BarrierExample(),
24+
'ComplexList': const ComplexListExample(),
25+
'Badge': const BadgeExample(),
26+
'PercentageLayout': const PercentageLayoutExample(),
27+
'DimensionRatio': const DimensionRatioExample(),
28+
'Relative Id': const RelativeIdExample(),
29+
'Wrapper Constraints': const WrapperConstraintsExample(),
30+
'Grid': const GridExample(),
31+
'Horizontal List': const HorizontalListExample(),
32+
'Vertical List': const VerticalListExample(),
33+
'Chain (Coming soon)': null,
34+
};
1835

1936
@override
2037
Widget build(BuildContext context) {
38+
List<String> keyList = exampleMap.keys.toList();
2139
return Scaffold(
22-
body: Column(
40+
body: ConstraintLayout(
2341
children: [
2442
const Text(
2543
'Flutter ConstraintLayout Example\nby hackeware',
@@ -28,48 +46,37 @@ class ExampleHome extends StatelessWidget {
2846
height: 1.5,
2947
),
3048
textAlign: TextAlign.center,
49+
).applyConstraint(
50+
topCenterTo: parent,
3151
),
32-
const SizedBox(
33-
height: 20,
52+
...constraintGrid(
53+
id: ConstraintId('example_list'),
54+
margin: const EdgeInsets.only(
55+
top: 20,
56+
),
57+
left: parent.left,
58+
top: rId(0).bottom,
59+
itemCount: 13,
60+
columnCount: 1,
61+
itemWidth: matchParent,
62+
itemHeight: 40,
63+
itemBuilder: (index) {
64+
Widget? example = exampleMap[keyList[index]];
65+
return TextButton(
66+
onPressed: example == null
67+
? null
68+
: () {
69+
push(context, example);
70+
},
71+
child: Text(
72+
keyList[index],
73+
style: const TextStyle(
74+
fontSize: 20,
75+
),
76+
),
77+
);
78+
},
3479
),
35-
button('Summary', () {
36-
push(context, const SummaryExample());
37-
}),
38-
button('Guideline', () {
39-
push(context, const GuidelineExample());
40-
}),
41-
button('Barrier', () {
42-
push(context, const BarrierExample());
43-
}),
44-
button('ComplexList', () {
45-
push(context, const ComplexListExample());
46-
}),
47-
button('Badge', () {
48-
push(context, const BadgeExample());
49-
}),
50-
button('PercentageLayout', () {
51-
push(context, const PercentageLayoutExample());
52-
}),
53-
button('DimensionRatio', () {
54-
push(context, const DimensionRatioExample());
55-
}),
56-
button('Relative Id', () {
57-
push(context, const RelativeIdExample());
58-
}),
59-
button('Wrapper Constraints', () {
60-
push(context, const WrapperConstraintsExample());
61-
}),
62-
button('Grid', () {
63-
push(context, const GridExample());
64-
}),
65-
button('Horizontal List', () {
66-
push(context, const HorizontalListExample());
67-
}),
68-
button('Vertical List', () {
69-
push(context, const VerticalListExample());
70-
}),
71-
button('Chain (Coming soon)', null),
72-
const Spacer(),
7380
const Text(
7481
'Powered by Flutter Web & ConstraintLayout',
7582
style: TextStyle(
@@ -78,10 +85,12 @@ class ExampleHome extends StatelessWidget {
7885
height: 1.5,
7986
),
8087
textAlign: TextAlign.center,
88+
).applyConstraint(
89+
bottomCenterTo: parent,
90+
margin: const EdgeInsets.only(
91+
bottom: 20,
92+
),
8193
),
82-
const SizedBox(
83-
height: 20,
84-
)
8594
],
8695
),
8796
);
@@ -92,20 +101,4 @@ class ExampleHome extends StatelessWidget {
92101
return widget;
93102
}));
94103
}
95-
96-
Widget button(String title, GestureTapCallback? callback) {
97-
return SizedBox(
98-
height: 40,
99-
width: double.infinity,
100-
child: TextButton(
101-
onPressed: callback,
102-
child: Text(
103-
title,
104-
style: const TextStyle(
105-
fontSize: 20,
106-
),
107-
),
108-
),
109-
);
110-
}
111104
}

example/main.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import 'package:flutter/material.dart';
33
import 'home.dart';
44

55
void main() {
6-
runApp(const MaterialApp(
6+
runApp(MaterialApp(
77
home: ExampleHome(),
88
));
99
}

lib/src/constraint_layout.dart

Lines changed: 59 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ class ConstraintLayout extends MultiChildRenderObjectWidget {
2525
final bool debugShowZIndex;
2626
final bool debugShowChildDepth;
2727

28+
// fixed size、matchParent(wrapContent is not supported yet)
29+
final double width;
30+
final double height;
31+
2832
ConstraintLayout({
2933
Key? key,
3034
this.childConstraints,
@@ -38,6 +42,8 @@ class ConstraintLayout extends MultiChildRenderObjectWidget {
3842
this.debugName,
3943
this.debugShowZIndex = false,
4044
this.debugShowChildDepth = false,
45+
this.width = matchParent,
46+
this.height = matchParent,
4147
}) : super(
4248
key: key,
4349
children: children,
@@ -46,6 +52,8 @@ class ConstraintLayout extends MultiChildRenderObjectWidget {
4652
@override
4753
RenderObject createRenderObject(BuildContext context) {
4854
assert(_debugEnsureNotEmptyString('debugName', debugName));
55+
assert(width >= 0 || width == matchParent);
56+
assert(height >= 0 || height == matchParent);
4957
return _ConstraintRenderBox()
5058
..childConstraints = childConstraints
5159
.._debugShowGuideline = debugShowGuideline
@@ -56,7 +64,9 @@ class ConstraintLayout extends MultiChildRenderObjectWidget {
5664
.._releasePrintLayoutTime = releasePrintLayoutTime
5765
.._debugName = debugName
5866
.._debugShowZIndex = debugShowZIndex
59-
.._debugShowChildDepth = debugShowChildDepth;
67+
.._debugShowChildDepth = debugShowChildDepth
68+
.._width = width
69+
.._height = height;
6070
}
6171

6272
@override
@@ -65,6 +75,8 @@ class ConstraintLayout extends MultiChildRenderObjectWidget {
6575
covariant RenderObject renderObject,
6676
) {
6777
assert(_debugEnsureNotEmptyString('debugName', debugName));
78+
assert(width >= 0 || width == matchParent);
79+
assert(height >= 0 || height == matchParent);
6880
(renderObject as _ConstraintRenderBox)
6981
..childConstraints = childConstraints
7082
..debugShowGuideline = debugShowGuideline
@@ -75,7 +87,9 @@ class ConstraintLayout extends MultiChildRenderObjectWidget {
7587
..releasePrintLayoutTime = releasePrintLayoutTime
7688
..debugName = debugName
7789
..debugShowZIndex = debugShowZIndex
78-
..debugShowChildDepth = debugShowChildDepth;
90+
..debugShowChildDepth = debugShowChildDepth
91+
..width = width
92+
..height = height;
7993
}
8094
}
8195

@@ -98,8 +112,9 @@ List<Widget> constraintGrid({
98112
}) {
99113
assert(itemCount > 0);
100114
assert(columnCount > 0);
101-
assert(itemWidth == null || (itemWidth > 0 || itemWidth == wrapContent));
102-
assert(itemHeight == null || (itemHeight > 0 || itemHeight == wrapContent));
115+
assert(itemWidth == null || (itemWidth >= 0 || itemWidth != matchConstraint));
116+
assert(
117+
itemHeight == null || (itemHeight >= 0 || itemHeight != matchConstraint));
103118
assert((itemSizeBuilder == null && itemWidth != null && itemHeight != null) ||
104119
(itemSizeBuilder != null && itemWidth == null && itemHeight == null));
105120
List<Widget> widgets = [];
@@ -151,14 +166,16 @@ List<Widget> constraintGrid({
151166
}
152167
Widget widget = itemBuilder(i);
153168
Size? itemSize = itemSizeBuilder?.call(i);
169+
double width = itemWidth ?? itemSize!.width;
170+
double height = itemHeight ?? itemSize!.height;
154171
widgets.add(Constrained(
155172
child: widget,
156173
constraint: Constraint(
157174
id: itemId,
158-
width: itemWidth ?? itemSize!.width,
159-
height: itemHeight ?? itemSize!.height,
160-
left: leftAnchor,
161-
top: topAnchor,
175+
width: width,
176+
height: height,
177+
left: width == matchParent ? null : leftAnchor,
178+
top: height == matchParent ? null : topAnchor,
162179
zIndex: zIndex,
163180
translate: translate,
164181
visibility: visibility,
@@ -1609,6 +1626,9 @@ class _ConstraintRenderBox extends RenderBox
16091626
late bool _debugShowZIndex;
16101627
late bool _debugShowChildDepth;
16111628

1629+
late double _width;
1630+
late double _height;
1631+
16121632
bool _needsRecalculateConstraints = true;
16131633
bool _needsReorderChildren = true;
16141634
int _buildNodeTreesCount = 0;
@@ -1765,6 +1785,20 @@ class _ConstraintRenderBox extends RenderBox
17651785
}
17661786
}
17671787

1788+
set width(double value) {
1789+
if (_width != value) {
1790+
_width = value;
1791+
markNeedsLayout();
1792+
}
1793+
}
1794+
1795+
set height(double value) {
1796+
if (_height != value) {
1797+
_height = value;
1798+
markNeedsLayout();
1799+
}
1800+
}
1801+
17681802
@override
17691803
void setupParentData(covariant RenderObject child) {
17701804
if (child.parentData is! _ConstraintBoxData) {
@@ -2102,17 +2136,31 @@ class _ConstraintRenderBox extends RenderBox
21022136
return true;
21032137
}());
21042138

2105-
/// Always fill the parent layout
2106-
/// TODO will support wrap_content in the future
2139+
/// TODO will support wrapContent in the future
2140+
double width;
21072141
double consMaxWidth = constraints.maxWidth;
21082142
if (consMaxWidth == double.infinity) {
21092143
consMaxWidth = window.physicalSize.width / window.devicePixelRatio;
21102144
}
2145+
if (_width >= 0) {
2146+
width = _width.clamp(constraints.minWidth, consMaxWidth);
2147+
} else {
2148+
width = consMaxWidth;
2149+
}
2150+
2151+
double height;
21112152
double consMaxHeight = constraints.maxHeight;
21122153
if (consMaxHeight == double.infinity) {
21132154
consMaxHeight = window.physicalSize.height / window.devicePixelRatio;
21142155
}
2115-
size = constraints.constrain(Size(consMaxWidth, consMaxHeight));
2156+
if (_height >= 0) {
2157+
height = _height.clamp(constraints.minHeight, consMaxHeight);
2158+
} else {
2159+
height = consMaxHeight;
2160+
}
2161+
2162+
size = constraints.constrain(Size(width, height));
2163+
assert(constraints.isSatisfiedBy(size));
21162164

21172165
if (_needsRecalculateConstraints) {
21182166
assert(() {

0 commit comments

Comments
 (0)