https://kotlinlang.org logo
#compose
Title
# compose
b

Brian G

10/18/2021, 10:07 PM
Is it possible to observe scroll offset? This code does not seem to work:
Copy code
val state = rememberScrollState()
LaunchedEffect(state) {
  snapshotFlow { state.value }.collect {
    Log.v(TAG, "$it") // This is never called?
  }
}
Box(Modifier.fillMaxSize().verticalScroll(state)) {
   // ... Lot of box contents
}
😢 1
a

Albert Chang

10/19/2021, 12:00 AM
I think it's because your box doesn't actually scroll. Try making it scrollable, e.g.
Box(Modifier.fillMaxSize().verticalScroll(state).height(1000.dp)
.
b

Brian G

10/19/2021, 2:24 PM
It does scroll, I just didn't include the content in my sample code.
a

Albert Chang

10/19/2021, 3:50 PM
Well I added the height modifier to your code and it does work.
z

Zach Klippenstein (he/him) [MOD]

10/19/2021, 4:51 PM
yea that code looks right
b

Brian G

10/19/2021, 5:23 PM
Hmmm, why doesn't it work for me :(
z

Zach Klippenstein (he/him) [MOD]

10/20/2021, 5:27 PM
Can you post more of your real code?
b

Brian G

10/20/2021, 6:27 PM
After y'all said it looked right, I looked deeper and noticed I was also using a LazyColumn deeper in the tree. The LazyColumn was doing the scrolling, so this one wasn't called. I fixed it 🙂