@@ -80,30 +80,60 @@ List<Widget> constraintGrid({
8080 required _Align top,
8181 required int itemCount,
8282 required int columnCount,
83- required double itemWidth,
84- required double itemHeight,
83+ double ? itemWidth,
84+ double ? itemHeight,
85+ Size Function (int index)? itemSizeBuilder,
8586 required Widget Function (int index) itemBuilder,
8687 EdgeInsets Function (int index)? itemMarginBuilder,
88+ EdgeInsets margin = EdgeInsets .zero,
8789}) {
8890 assert (itemCount > 0 );
8991 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 ));
9296 List <Widget > widgets = [];
9397 _Align leftAnchor = left;
9498 _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+
95123 for (int i = 0 ; i < itemCount; i++ ) {
96124 ConstraintId itemId = ConstraintId (id.id + '_grid_item_$i ' );
97125 Widget widget = itemBuilder (i);
126+ Size ? itemSize = itemSizeBuilder? .call (i);
98127 widgets.add (Constrained (
99128 child: widget,
100129 constraint: Constraint (
101130 id: itemId,
102- width: itemWidth,
103- height: itemHeight,
131+ width: itemWidth ?? itemSize ! .width ,
132+ height: itemHeight ?? itemSize ! .height ,
104133 left: leftAnchor,
105134 top: topAnchor,
106- margin: itemMarginBuilder? .call (i) ?? EdgeInsets .zero,
135+ margin: calculateItemMargin (
136+ i, columnCount, itemMarginBuilder? .call (i) ?? EdgeInsets .zero),
107137 ),
108138 ));
109139 leftAnchor = itemId.right;
0 commit comments