https://kotlinlang.org logo
Title
s

Sean Proctor

05/18/2023, 1:27 PM
I'm confused trying get
AnimatedVisibility
working. It works fine to hide my composable, but it doesn't show it when
visible
changes back to true.
I'm using the example from https://stackoverflow.com/questions/74381573/jetpack-compose-fab-hide-on-scroll-behavior to show/hide a FAB on scroll.
Scaffold(
    floatingActionButton = {
            val showFab = listState.isScrollingUp().value
            println("showing fab: $showFab")
            AnimatedVisibility(visible = showFab) {
                FloatingActionButton(
                    onClick = {
                        ...
                    }
                ) {
                    Icon(
                        imageVector = Icons.Default.Add,
                        contentDescription = "add"
                    )
                }
            }
    },
    ...
)
y

Yves Kalume

05/18/2023, 2:01 PM
Is the showFab changing when you scroll?
s

Sean Proctor

05/18/2023, 2:28 PM
Yes, it does.
z

zokipirlo

05/18/2023, 4:56 PM
I would make
showFab
as
derivedStateOf
of
listState
. Like explained here: https://medium.com/androiddevelopers/jetpack-compose-when-should-i-use-derivedstateof-63ce7954c11b
val isEnabled = remember {
derivedStateOf { lazyListState.firstVisibleItemIndex > 0 }
}
s

Sean Proctor

05/19/2023, 11:39 AM
I don't see why. That article states not to use
derivedStateOf
to combine values. I want the FAB to be visible when the user has last scrolled up, so the previous index needs to be remembered.
z

zokipirlo

05/19/2023, 11:50 AM
What happens if you move
showFab
before
Scaffold
?