Paul Woitaschek
10/26/2022, 8:05 AMtransparent
or transparent2
?
@Composable
public fun TransparentTopAppBar(
contentListState: LazyListState,
) {
val transparent by remember {
derivedStateOf {
contentListState.firstVisibleItemIndex == 0 && contentListState.firstVisibleItemScrollOffset == 0
}
}
val transparent2 by remember(contentListState) {
derivedStateOf {
contentListState.firstVisibleItemIndex == 0 && contentListState.firstVisibleItemScrollOffset == 0
}
}
}
hfhbd
10/26/2022, 8:33 AMhfhbd
10/26/2022, 8:34 AMPaul Woitaschek
10/26/2022, 8:35 AMStylianos Gakis
10/26/2022, 9:20 AMState
.
But inside the derivedStateOf here, it’s reading firstVisibleItemIndex
and firstVisibleItemScrollOffset
which both are backed by MutableState inside the LazyListScrollPosition
class, so derivedStateOf should be notified about those changes.
So maybe if contentListState changes, derivedStateOf
would normally not get notified, but since it’s reading some `MutableState`s from that object itself, it still turns out to be notified as it should.
One thing one could do to test this out is to make a sample where you pass in a new LazyListState, and try it once where firstVisibleItemIndex
and firstVisibleItemScrollOffset
are the same, and once when they’re different, and see if it works properly. I can’t say for sure if I’d be confident that it does 🤷♂️Stylianos Gakis
10/26/2022, 9:21 AMAlbert Chang
10/26/2022, 9:31 AMcontentListState
instance will be captured by the lambda of derivedStateOf
and will never change even if the argument changes. No matter derivedStateOf
is notified or not, it will always calculate the value using the old contentListState
instance.
The doc is using the second one.Stylianos Gakis
10/26/2022, 9:33 AMPaul Woitaschek
10/26/2022, 10:18 AMAlbert Chang
10/26/2022, 10:19 AMtodoTasks
never changes as it's not an argument.Stylianos Gakis
10/26/2022, 10:22 AM