I’m doing something like a news feed and I’m using CustomScrollView with two slivers (one for initially loaded news and one for new news that should not shift elements when loaded).
Example:
return CustomScrollView(
center: centerKey,
slivers: [
renderOneList(newItems),
renderOneList(oldItems, centerKey),
],
);
Everything works as it should. I use UniqueKey to define the first element where the initial reading starts. Once every X seconds, I load new items and put them into an array of newItems. They are added to the top and the user can scroll to the top.
However, when clicking on the status bar in iOS Primary Scroll Controller scrolls to position 0 (which in my case is the boundary of new news and old news). This is correct from Flutter’s point of view, but it’s not the behavior I would like to achieve. Any ideas on how to scroll to the top?
What I think about it:
- It is possible to move records from oldItems to newItems when scrolling up, but that would cause a lot of redraws and I probably wouldn’t be able to guess the timing of the swap.
- While tapping on the status bar, scroll up X pixels in Y milliseconds. Doesn’t seem like a great option either.
I ended up with a dead end, but I’m sure the community has solved such a basic problem before. Hope for some help.
2
Answers
when tapping the status bar, you can achieve this by using the
primary
property of theCustomScrollView
and a combination ofScrollController
andScrollPhysics
.primary: true,
// Add this line before theslivers
propertyCustomize Scroll Physics
Create a custom scroll physics class that allows overscrolling at the top but snaps back to the top when released.
Wrap the
CustomScrollView
withNotificationListener
CustomScrollPhysics
allows overscrolling at the top while ensuring that the scrolling animation snaps back to the top when releases the touch.The
NotificationListener
captures the status bar tap and initiates the scrolling to the top.Remember to manage the
_scrollController
properly, including disposing it in the dispose method.