Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fillViewPort behavior in Jetpack Compose Column

Is there something like ScrollView fillViewPort in Jetpack Compose Column?

See this example:

@Composable
fun FillViewPortIssue() {
    Column(
        Modifier
            .fillMaxSize()
            .padding(16.dp)
    ) {
        for (i in 0..5) {
            Box(
                modifier = Modifier
                    .padding(vertical = 8.dp)
                    .background(Color.Red)
                    .fillMaxWidth()
                    .height(72.dp)
            )
        }
        Spacer(modifier = Modifier.weight(1f))
        Button(
            modifier = Modifier.fillMaxWidth(),
            onClick = { /*TODO*/ }
        ) {
            Text("Ok")
        }
    }
}

This is the result:

enter image description here

When the device is in landscape, the content is cropped, because there's no scroll. If I add the verticalScroll modifier do the Column...

    ...
    Column(
        Modifier
            .verticalScroll(rememberScrollState()) // <<-- this
            .fillMaxSize()
            .padding(16.dp)
    ) {
    ...

... the scroll problem is fixed, but the button goes up, like this.

enter image description here

In the traditional toolkit, we can fix this using ScrollView + fillViewPort property. Is there something equivalent to Compose?

like image 745
nglauber Avatar asked Jun 11 '21 00:06

nglauber


People also ask

How do you make a column scrollable in jetpack compose?

We can make the Column scrollable by using the verticalScroll() modifier.

What is mutableStateOf in jetpack compose?

mutableStateOf creates an observable MutableState<T> , which is an observable type integrated with the compose runtime. interface MutableState<T> : State<T> { override var value: T. } Any changes to value will schedule recomposition of any composable functions that read value .

What is LazyColumn in jetpack compose?

A LazyColumn is a vertically scrolling list that only composes and lays out the currently visible items. It's similar to a Recyclerview in the classic Android View system.

Is jetpack compose reactive?

Jetpack Compose is a modern toolkit designed to simplify UI development. It combines a reactive programming model with the conciseness and ease of use of the Kotlin programming language. It is fully declarative, meaning you describe your UI by calling a series of functions that transform data into a UI hierarchy.


2 Answers

I had a similar issue recently and substituted verticalScroll for scrollable(rememberScrollState(), Orientation.Vertical) which seemed to work for my case and allow the screen to fill its viewport while allowing scrolling

like image 74
alfietap Avatar answered Oct 18 '22 01:10

alfietap


Just change the order of the modifiers worked... Thanks to Abhishek Dewan (from Kotlin Slack channel)!

Column(
    Modifier
        .fillMaxSize() // first, set the max size
        .verticalScroll(rememberScrollState()) // then set the scroll
) {
like image 31
nglauber Avatar answered Oct 18 '22 03:10

nglauber