Is it possible to specify a weight and a minimum height?

Viewed 1150

I'm trying to have an element that has a height of at least 160 dp, but scales to use all of the space available when possible. I think that perhaps this is more difficult than what I expected.

The following does not work, but maybe it conveys the idea of what I want to do.

Column(modifier = Modifier.verticalScroll(rememberScrollState()).fillMaxSize) {
    Box(modifier = Modifier.weight(1f).sizeIn(minHeight = 160.dp))
    Box(modifier = Modifier.height(600.dp))
}

When the content doesn't fit the screen, I want the content to be scrollable, and the first box to be 160 dp in height. If the screen is more than 760 dp, I want first box to fill as much space as possible without causing the need to scroll.

1 Answers

You can use the BoxWithConstraints to define its own content according to the available space.
Something like:

BoxWithConstraints {
    val height = maxHeight
    Column(
        modifier = Modifier
            .background(Color.Red)
            .verticalScroll(state = rememberScrollState(0))
            .fillMaxSize()
    ) {

        val modifierBox1 : Modifier = if (height > 760.dp)
            Modifier.heightIn(height - 600.dp)
        else
            Modifier.heightIn(160.dp)

        Box(
            modifier = modifierBox1
                .fillMaxWidth()
                .background(Color.Blue)
        )
        Box(
            modifier = Modifier
                .height(600.dp)
                .fillMaxWidth()
                .background(Color.Green)
        )
    }
}

enter image description here

Related