Just Simple Alternative
In my case, i am using infinite builder by default. no itemCount. and my list is widget.images
.
PageView.builder(
controller: _pageController,
allowImplicitScrolling: true,
itemBuilder: (context, index) {
final url = widget.images[index % widget.images.length];
return InteractiveViewer(maxScale: 3, child: Image.asset(url));
},
),
But also, i want to view initial page, so i manage using _pageController
. To initialize _pageController
, i sent initialIndex
for example index 2
.
When i snap to right, infinite looping work. after i reach last page, the page show first page again and keep going.
But !!, when i star over again. after pageview
show initial page then i snap to left, infinite loop not work. from this i learn that is pageview
default infinite only work for upcoming index.
To fix this, i create schema to handle range page/index e.g. from 0-1000. and create function to calculateActualInitialIndex. to make infinite loop to left direction, i set initialIndex
to center of range page. if range 0-1000, then initialIndex
will be 500. This will work to infitie loop for left direction until indexValue is 0.
and here to initialize _pageCntroller and calculate initial:
late final PageController _pageController;
static const int _infiniteScrollFactor = 1000; // range for 0-1000
int _calculateInitialPage() {
return (_infiniteScrollFactor ~/ 2) * widget.images.length + widget.initialIndex;
}
@override
void initState() {
_pageController = PageController(initialPage: _calculateInitialPage());
super.initState();
}