google / flutter.widgets

https://pub.dev/packages/flutter_widgets
BSD 3-Clause "New" or "Revised" License
1.38k stars 488 forks source link

[scrollable_positioned_list] How to append items at the top and preserve scroll position (Up and Down Lazy load)? #373

Open MiniSuperDev opened 2 years ago

MiniSuperDev commented 2 years ago

Hi, I had created bidirectional lazy load list (load next when reached last item, and load previous when reached first item, like discord chat when search a message and you select it, it loads messages around the that message, so the user can load previous and next messages based on the scroll) Like the like flutter docs said https://dartpad.dev/?id=c8790d62affcda2ad56325f0588d4810 https://github.com/flutter/flutter/blob/5464c5bac742001448fe4fc0597be939379f88ea/packages/flutter/lib/src/widgets/scroll_view.dart#L502-L513 https://github.com/flutter/flutter/issues/21541#issuecomment-629121578

But I also need the scroll to index behavior, so how can I implement this behavior with scrollable_positioned_list?

Since I can't use 2 ScrollablePositionedList inside a CustomScrollView and use a centerKey on the second list, I'm wondering if can I add items to the top while preserving the scroll, or maybe you know of another way to achieve this behavior.

thank you

syahnurizal commented 2 years ago

after append the item on top of the list u can call scrollController.jumpTo(index: lastPosition); before refreshing the UI.

works for me

musclejack commented 2 years ago

good question! if you have some way to realization it, pls tell me the way.

lifeapps-42 commented 5 months ago

after append the item on top of the list u can call scrollController.jumpTo(index: lastPosition); before refreshing the UI.

works for me

This works, but stops an ongoing animation, e.g. when user is scrolling