androidkotlinandroid-jetpack-compose

Can't use fillMaxWidth or fillParentMaxWidth in LazyGrid - Jetpack Compose


I want to create a LazyVerticalGrid with items that have a height as high as the LazyVerticalGrid. The LazyVerticalGrid should have a height of fillMaxSize(). When I use fillMaxHeight in the item, the items are hidden. I tried using fillParentMaxHeight but this is not available in LazyGridItemScope How can I create items with a height of something like fillMaxHeight()

My Code:

LazyVerticalGrid(columns = GridCells.Fixed(2), modifier = Modifier
            .weight(1f)
            .fillMaxSize(),content = {
            items(count = 100){
                val cardColor = Color((0..255).random(),(0..255).random(),(0..255).random())
                Card(modifier = Modifier
                    .padding(4.dp)
                    .fillMaxHeight(3f), colors = CardDefaults.cardColors(containerColor = cardColor)) {
                }
            }
        })

Solution

  • Thank You @Ali Elgamal :) but I fixed it by using BoxWithConstraints like this:

    BoxWithConstraints(modifier = Modifier.padding(4.dp).fillMaxSize()) {
                var boxWithConstraintsScope = this
                LazyVerticalGrid(columns = GridCells.Fixed(2), modifier = Modifier
                    .fillMaxSize()
                    .onSizeChanged {
                    },content = {
                    items(count = 100){
                        val cardColor = Color((0..255).random(),(0..255).random(),(0..255).random())
                        Card(modifier = Modifier
                            .padding(4.dp)
                            .height(boxWithConstraintsScope.maxHeight-8.dp), colors = CardDefaults.cardColors(containerColor = cardColor)) {
                        }
                    }
                })
            }