Hi Compose folks, I’m trying to implement auto-sc...
# compose
s
Hi Compose folks, I’m trying to implement auto-scroll to the bottom in Compose, similar to a standard AI/chat screen. The issue is that with
LazyList
, it does not seem very reliable: 1. Sometimes scrolling does not happen, probably because the list is still being laid out/drawn (probably). 2. Sometimes it does scroll, but the animation looks ugly. Does anyone have ideas or a good example of how to implement this properly?
o
Was also looking forward to an answer for this 🥲
s
@Orb All I found: run this code after post a new chunk of text:
Copy code
val totalItems = listState.layoutInfo.totalItemsCount
val lastVisible = listState.layoutInfo.visibleItemsInfo.lastOrNull()
val isNearBottom = lastVisible == null || lastVisible.index >= totalItems - 1

if (isNearBottom && totalItems > 0) {
    listState.requestScrollToItem(totalItems - 1)
}
but it works so so.