I’m having some very inconsistent behavior doing a...
# compose
r
I’m having some very inconsistent behavior doing an alpha scroll animation with
derivedStateOf
🧵
With the following code:
Copy code
val groupLazyListState = rememberLazyListState()

val topBarBackgroundAlpha by remember(groupLazyListState) {
    derivedStateOf {
        val layoutInfo = groupLazyListState.layoutInfo
        val itemInfo = layoutInfo.visibleItemsInfo
            .firstOrNull { it.contentType == ImmersiveCollectionUiModel::class }
            ?: return@derivedStateOf 1f

        val adjustedOffset = itemInfo.offset.absoluteValue - layoutInfo.viewportStartOffset
        adjustedOffset / itemInfo.size.toFloat()
    }
}
I have some top menu items where I completely switch the following
Group
composable. This ends up being totally new
LazyColumns
underneath, but using the same
LazyListState
.
Copy code
Group(
    model = group,
    lazyListState = groupLazyListState,
)
When I’m switching between these menu items, I’ll end up in a state where
derivedStateOf
stops being called and my
topBarBackgroundAlpha
no longer changes.
Group
is totally fine, scroll is working fine, its just that observing those scroll state changes will no longer work. What am I missing here?
I guess the issue must be similar to https://issuetracker.google.com/issues/294436482
rememberUpdatedState
seems to solve it, but its not very obvious right away why
derivedStateOf
doesn’t retrigger on updated
layoutInfo
.