I wrote a composable function for full grids. But ...
# compose
a
I wrote a composable function for full grids. But whenever the row count is an odd number and larger than 5, I get these 1 pixel spaces. Does anyone know how I can fix them. Code in đŸ§µ
Copy code
// call site 
FullGrid(
    columns = 7,
    rows = 7,
) { _, size ->
    Box(
        modifier = Modifier
            .size(size)
            .background(Color.LightGray)
    ) { /*...*/ }
}

@Composable
fun FullGrid(
    columns: Int,
    rows: Int,
    modifier: Modifier = Modifier,
    content: @Composable RowScope.(index: Int, size: Dp) -> Unit,
) {
    var size by remember { mutableStateOf(0.dp) }
    val density = LocalDensity.current
    Column(modifier = modifier) {
        repeat(rows) { row ->
            Row(
                modifier = Modifier
                    .weight(1f)
                    .onSizeChanged {
                        with(density) {
                            size = it.height.toDp()
                        }
                    },
            ) {
                repeat(columns) { col ->
                    content(col + (row * columns), size)
                }
            }
        }
    }
}
z
Maybe a rounding error somewhere converting from pixels to dps and back?
a
This is what I got from logs
z
Why are you passing the size to the lambda as a param and using that to size your cell content via modifier? Even if you fix the gap, there will always be a frame lag between when your grid is measured and your cells catch up
a
I use them to calculate the size of smaller elements inside a cell as well as the cell's size
Using .fillMaxSize() on a cell makes the grid disproportionate as I want them to be square
z
You should pass layout constraints to your cells via the layout system, that’s why it exists
If you want the cells to be square, pass square constraints
a
I haven't used that before. How can I achieve that?
z
a
Thanks I'll check it out