photo_view
photo_view copied to clipboard
get current page index inside loadingbuilder
Is your feature request related to a problem? Please describe. I am unable to get the current page index inside the loadingbuilder. When I try to set the index inside the builder, the images will switch around, because the index is set twice, by both pages
Describe the solution you'd like Get the current page index passed into the loadingbuilder
Describe alternatives you've considered Trying to set the index inside the builder with a variable that spans the entire context.
Additional context
the code I tried for this looks something like this:
int current;
return PhotoViewGallery.builder(
builder: (BuildContext context, int index) {
current = index;
imageProvider: CachedNetworkImageProvider(sourceImages[index]),
},
loadingBuilder: (buildContext, imageChunkEvent) => CachedNetworkImage(
imageUrl: previewImages[current],
),
),
the issue I was having with my workaround is basically, that the next image in the pageview gets loaded while the first one also gets loaded, because the current int is set by the both page that are loaded by the pageview.
Setting a control variable (current) via builder creates a race condition. This happens because the index passed to Pageview's builder is the source of truthness only inside the builder scope. Outside of it, we can't rely on that since we have a state when we have two pages in display (like in the middle of a swipe gesture) and both builders (for each page) got to run.
To get to know the "current" page, use PageController.page (PhotoViewGallery supports that).
beautiful, that should to it. thank you.
oh no, that doesnt work either, @renancaraujo . because PageController.page is a double, and rounding it will not work either because the next page will be loaded while the double is closer to the current one etc.
You have to use animateToPage
🤔 I am a bit confused now. How would animateToPage help me get the current index inside the loadingbuilder? I am not setting the page manually anywhere.
For sure, loadingBuilder has to be moved to PhotoViewGalleryPageOptions
I have solved this with #339
I think it would still be nice to have the loading builder inside the options.