@@ -134,4 +134,63 @@ interface SimpleGridCells {
134134 return hash
135135 }
136136 }
137+
138+ /* *
139+ * Make grid to have as many rows or columns as possible and each cell has exactly [size].
140+ * If [size] is bigger than container's size, the cell will have the same size to the container.
141+ *
142+ * For example, `FixedSize(20.dp)` for `VerticalGrid(Modifier.width(66.dp)` means that there
143+ * will be 3 columns and each cell will have 20dp width and 6dp is remained.
144+ *
145+ * In other case, `FixedSize(150.dp)` for `VerticalGrid(Modifier.width(100.dp)` means that there
146+ * will be only one column and the cell will have 100dp width.
147+ *
148+ * @param size The size which each cell should have.
149+ * @param fill When `true`, item composable fill cell's width or height.
150+ */
151+ @ExperimentalGridApi
152+ class FixedSize (
153+ private val size : Dp ,
154+ private val fill : Boolean = true ,
155+ ) : SimpleGridCells {
156+ init {
157+ if (size <= 0 .dp) {
158+ throw IllegalArgumentException (" FixedSize size must be a positive, but $size " )
159+ }
160+ }
161+
162+ override fun Density.calculateCrossAxisCellSizes (
163+ availableSize : Int ,
164+ spacing : Int
165+ ): List <Int > {
166+ val cellSize = size.roundToPx()
167+ val availableSizeWithSpacing = availableSize + spacing
168+ val cellSizeWithSpacing = cellSize + spacing
169+
170+ return if (cellSizeWithSpacing < availableSizeWithSpacing) {
171+ val count = availableSizeWithSpacing / cellSizeWithSpacing
172+ return List (count) { cellSize }
173+ } else {
174+ List (1 ) { availableSize }
175+ }
176+ }
177+
178+ override fun fillCellSize (): Boolean {
179+ return fill
180+ }
181+
182+ override fun equals (other : Any? ): Boolean {
183+ if (other !is FixedSize ) return false
184+ if (this .size != other.size) return false
185+ if (this .fill != other.fill) return false
186+ return true
187+ }
188+
189+ override fun hashCode (): Int {
190+ var hash = 1
191+ hash = hash * 31 + size.hashCode()
192+ hash = hash * 31 + fill.hashCode()
193+ return hash
194+ }
195+ }
137196}
0 commit comments