Home > Software design >  In Compose, how to set minimum height (minHeight) without consuming all available height?
In Compose, how to set minimum height (minHeight) without consuming all available height?

Time:08-06

In Jetpack Compose, I have a card composable that I want to be a minimum of 100.dp tall. However, if I use heightIn, the card consumes all available height up until the max. How do I set a minHeight without consuming all height?

Surface(
    modifier = heightIn(100.dp, 9999.dp),
) {
    // Content
}

To be clear, what I want is the following algorithm:

height = if (contentHeight > minHeight) {
    contentHeight 
} else {
    minHeight
}

CodePudding user response:

Set Modifier.height(contentHeight.coerceAtLeast(minHeight))

CodePudding user response:

Use Modifier.defaultMinSize

Surface(
    modifier = Modifier.defaultMinSize(height = 100.dp),
) {
    // Content
}
  • Related