Skip to content

Commit 5939067

Browse files
author
fbchen
committed
enhance grid again
1 parent 5625dca commit 5939067

File tree

5 files changed

+29
-17
lines changed

5 files changed

+29
-17
lines changed

example/grid.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ class GridExample extends StatelessWidget {
3232
columnCount: 8,
3333
itemWidth: 100,
3434
itemHeight: 100,
35-
itemBuilder: (index) {
35+
itemBuilder: (index, _, __) {
3636
return Container(
3737
color: colors[index % colors.length],
3838
);
3939
},
40-
itemMarginBuilder: (index) {
40+
itemMarginBuilder: (index, _, __) {
4141
return const EdgeInsets.only(
4242
left: 10,
4343
top: 10,

example/horizontal_list.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class HorizontalListExample extends StatelessWidget {
3636
columnCount: 10,
3737
itemWidth: wrapContent,
3838
itemHeight: 100,
39-
itemBuilder: (index) {
39+
itemBuilder: (index, _, __) {
4040
return Container(
4141
color: colors[index % colors.length],
4242
padding: const EdgeInsets.symmetric(
@@ -45,7 +45,7 @@ class HorizontalListExample extends StatelessWidget {
4545
child: Text('child $index'),
4646
);
4747
},
48-
itemMarginBuilder: (index) {
48+
itemMarginBuilder: (index, _, __) {
4949
return const EdgeInsets.only(
5050
left: 10,
5151
top: 10,

example/staggered_grid.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,14 @@ class StaggeredGridExample extends StatelessWidget {
5050
top: parent.top,
5151
itemCount: 50,
5252
columnCount: columnCount,
53-
itemBuilder: (index) {
53+
itemBuilder: (index, _, __) {
5454
return Container(
5555
color: colors[index % colors.length],
5656
alignment: Alignment.center,
5757
child: Text('$index'),
5858
);
5959
},
60-
itemSizeBuilder: (index) {
60+
itemSizeBuilder: (index, _, __) {
6161
if (index == 0) {
6262
return const Size(
6363
smallestSize * columnCount + 35, smallestSize);
@@ -95,7 +95,7 @@ class StaggeredGridExample extends StatelessWidget {
9595
}
9696
return 1;
9797
},
98-
itemMarginBuilder: (index) {
98+
itemMarginBuilder: (index, _, __) {
9999
return const EdgeInsets.only(
100100
left: 5,
101101
top: 5,

example/vertical_list.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class VerticalListExample extends StatelessWidget {
3434
),
3535
itemCount: 20,
3636
columnCount: 1,
37-
itemBuilder: (index) {
37+
itemBuilder: (index, _, __) {
3838
return Container(
3939
color: colors[index % colors.length],
4040
padding: const EdgeInsets.symmetric(
@@ -43,10 +43,10 @@ class VerticalListExample extends StatelessWidget {
4343
child: Text('child $index'),
4444
);
4545
},
46-
itemSizeBuilder: (index) {
46+
itemSizeBuilder: (index, _, __) {
4747
return const Size(100, wrapContent);
4848
},
49-
itemMarginBuilder: (index) {
49+
itemMarginBuilder: (index, _, __) {
5050
return const EdgeInsets.only(
5151
left: 10,
5252
top: 10,

lib/src/constraint_layout.dart

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,11 @@ List<Widget> constraintGrid({
111111
required int columnCount,
112112
double? itemWidth,
113113
double? itemHeight,
114-
Size Function(int index)? itemSizeBuilder,
115-
required Widget Function(int index) itemBuilder,
116-
EdgeInsets Function(int index)? itemMarginBuilder,
114+
Size Function(int index, int rowIndex, int columnIndex)? itemSizeBuilder,
115+
required Widget Function(int index, int rowIndex, int columnIndex)
116+
itemBuilder,
117+
EdgeInsets Function(int index, int rowIndex, int columnIndex)?
118+
itemMarginBuilder,
117119
int Function(int index)? itemSpanBuilder,
118120
EdgeInsets margin = EdgeInsets.zero,
119121
CLVisibility visibility = visible,
@@ -154,12 +156,13 @@ List<Widget> constraintGrid({
154156
ConstraintId itemId = ConstraintId(id.id + '_grid_item_$i');
155157
allChildIds.add(itemId);
156158

157-
EdgeInsets childMargin = itemMarginBuilder?.call(i) ?? EdgeInsets.zero;
158159
int itemSpan = itemSpanBuilder?.call(i) ?? 1;
159160
assert(itemSpan >= 1 && itemSpan <= columnCount);
160161
currentRowUsedSpanCount += itemSpan;
161162
totalUsedSpanCount += itemSpan;
162163

164+
late EdgeInsets childMargin;
165+
163166
/// New row start
164167
if (currentRowUsedSpanCount > columnCount) {
165168
currentRowIndex++;
@@ -180,7 +183,14 @@ List<Widget> constraintGrid({
180183
/// First column
181184
leftAnchor = left;
182185
leftChildIds.add(itemId);
183-
childMargin = childMargin.add(leftMargin) as EdgeInsets;
186+
childMargin = (itemMarginBuilder?.call(
187+
i, currentRowIndex, currentRowUsedSpanCount - 1) ??
188+
EdgeInsets.zero)
189+
.add(leftMargin) as EdgeInsets;
190+
} else {
191+
childMargin = itemMarginBuilder?.call(
192+
i, currentRowIndex, currentRowUsedSpanCount - 1) ??
193+
EdgeInsets.zero;
184194
}
185195

186196
// First row
@@ -218,8 +228,10 @@ List<Widget> constraintGrid({
218228
}
219229
}
220230

221-
Widget widget = itemBuilder(i);
222-
Size? itemSize = itemSizeBuilder?.call(i);
231+
Widget widget =
232+
itemBuilder(i, currentRowIndex, currentRowUsedSpanCount - 1);
233+
Size? itemSize =
234+
itemSizeBuilder?.call(i, currentRowIndex, currentRowUsedSpanCount - 1);
223235
double width = itemWidth ?? itemSize!.width;
224236
double height = itemHeight ?? itemSize!.height;
225237

0 commit comments

Comments
 (0)