I want to create an infinite scrollable list like it is possible to do with ListView.builder
:
ListView.builder(
itemBuilder: (context, index) => Text(index.toString()),
);
But doing so only creates an infinite list in "one direction" (down): index
can only be 0 <= index
.
I would like to also be able to scroll up which would be equivalent to having an index
being possibly negative.
Is there any built-in widget for that? I couldn’t find anything, even among the slivers:
2
Answers
No programming language allows us to use a negative index value. But they have different functionalities in different languages (in Python you are refering with -1 to the last object and -2 to the second last). But Dart doesnt support negative indexes, so the lists do not do it either.
I think what you wan to do is to combine two "infinite" Lists/arrays. I would advise you to use two lists. Use
shrinkWrap: true and physics: const NeverScrollableScrollPhysics()
in both lists and Wrap them with SingleChildScrollView.
To give you a better help, I would need more Informations. But this should do it for the start.
I don’t know if that’s the sleekest solution, but you could add a listener to your ScrollController, check if
_controller.position.pixels <= _controller.position.minScrollExtent
, then add the data you want to have ‘above’ the current index to the list of the ListView.builder and then use the ScrollController to jump down to where you were before you inserted the data.To make it easier to scroll back to the right item you could use the scrollable_positioned_list package like here.
I didn’t test if that results in visible jitter or not, but maybe worth a try.