@@ -80,30 +80,60 @@ List<Widget> constraintGrid({
80
80
required _Align top,
81
81
required int itemCount,
82
82
required int columnCount,
83
- required double itemWidth,
84
- required double itemHeight,
83
+ double ? itemWidth,
84
+ double ? itemHeight,
85
+ Size Function (int index)? itemSizeBuilder,
85
86
required Widget Function (int index) itemBuilder,
86
87
EdgeInsets Function (int index)? itemMarginBuilder,
88
+ EdgeInsets margin = EdgeInsets .zero,
87
89
}) {
88
90
assert (itemCount > 0 );
89
91
assert (columnCount > 0 );
90
- assert (itemWidth > 0 );
91
- assert (itemHeight > 0 );
92
+ assert (itemWidth == null || (itemWidth > 0 || itemWidth == wrapContent));
93
+ assert (itemHeight == null || (itemHeight > 0 || itemHeight == wrapContent));
94
+ assert ((itemSizeBuilder == null && itemWidth != null && itemHeight != null ) ||
95
+ (itemSizeBuilder != null && itemWidth == null && itemHeight == null ));
92
96
List <Widget > widgets = [];
93
97
_Align leftAnchor = left;
94
98
_Align topAnchor = top;
99
+
100
+ EdgeInsets leftMargin = EdgeInsets .only (
101
+ left: margin.left,
102
+ );
103
+ EdgeInsets topMargin = EdgeInsets .only (
104
+ top: margin.top,
105
+ );
106
+ EdgeInsets calculateItemMargin (
107
+ int index,
108
+ int columnCount,
109
+ EdgeInsets margin,
110
+ ) {
111
+ /// First row
112
+ if (index < columnCount) {
113
+ margin = margin.add (topMargin) as EdgeInsets ;
114
+ }
115
+
116
+ /// First column
117
+ if (index % columnCount == 0 ) {
118
+ margin = margin.add (leftMargin) as EdgeInsets ;
119
+ }
120
+ return margin;
121
+ }
122
+
95
123
for (int i = 0 ; i < itemCount; i++ ) {
96
124
ConstraintId itemId = ConstraintId (id.id + '_grid_item_$i ' );
97
125
Widget widget = itemBuilder (i);
126
+ Size ? itemSize = itemSizeBuilder? .call (i);
98
127
widgets.add (Constrained (
99
128
child: widget,
100
129
constraint: Constraint (
101
130
id: itemId,
102
- width: itemWidth,
103
- height: itemHeight,
131
+ width: itemWidth ?? itemSize ! .width ,
132
+ height: itemHeight ?? itemSize ! .height ,
104
133
left: leftAnchor,
105
134
top: topAnchor,
106
- margin: itemMarginBuilder? .call (i) ?? EdgeInsets .zero,
135
+ margin: calculateItemMargin (
136
+ i, columnCount, itemMarginBuilder? .call (i) ?? EdgeInsets .zero),
107
137
),
108
138
));
109
139
leftAnchor = itemId.right;
0 commit comments