Hello I am trying to have a column that will take ...
# compose
a
Hello I am trying to have a column that will take the whole available height as long as it is between a
MIN_H
and
MAX_H
. If the vertical available space is more than
MAX_H
, the column should leave some empty space at the bottom, and if it is less than
MIN_H
, the content will be scrollable. I thought I could get this quite easily with
Modifier.heightIn()
but it doesn't matter where I put it or what I pass to it I never get the desired result. Example code in the thread.
Copy code
@Composable
fun Test() {
    Column(Modifier
            .background(Color.Blue)
            .verticalScroll(rememberScrollState())
            .heightIn(min = 300.dp, max = 600.dp)
            .fillMaxWidth()
    ) {
        Text("one", Modifier
                .fillMaxWidth()
                .background(Color.Red)
                .height(100.dp))
        Text("two", Modifier
                .fillMaxWidth()
                .background(Color.Yellow)
                .weight(1f))
        Text("three", Modifier
                .fillMaxWidth()
                .background(Color.Green)
                .height(100.dp))
    }
}
EDIT: replacing
heightIn
with
requiredHeightIn
leads to the same results.
Ideally the final effect would be: • Between 300 and 600 dp of total height the Text "two" takes all available space so that the column in turn takes all available space • Past 600 the Text "two" takes up 400 dp, the column takes up 600 dp and the rest of the space is white • Less than 300 the Text "two" takes up 100 dp, the column takes up 300 dp and is scrollable
What I get instead is that the column is always 600 dp high (and it's scrollable when available height is less than 600 dp)
j
Try
.weight(1F, fill = false)
a
no luck, the Text "two" just wraps the content in that case (and, interestingly, the column height is fixed to 300 dp instead of 600 dp in this case).
I can make it work as I want using
BoxWithConstraints
but it feels contrived:
Copy code
@Composable
fun Test() {
    BoxWithConstraints(Modifier.fillMaxSize()) {
        Column(Modifier
                .background(Color.Blue)
                .verticalScroll(rememberScrollState())
                .height(maxHeight.coerceIn(300.dp, 600.dp))
                .fillMaxWidth()
        ) {
            Text("one", Modifier
                    .fillMaxWidth()
                    .background(Color.Red)
                    .height(100.dp))
            Text("two", Modifier
                    .fillMaxWidth()
                    .background(Color.Yellow)
                    .weight(1f))
            Text("three", Modifier
                    .fillMaxWidth()
                    .background(Color.Green)
                    .height(100.dp))
        }
    }
}