photo_view icon indicating copy to clipboard operation
photo_view copied to clipboard

get current page index inside loadingbuilder

Open clragon opened this issue 4 years ago • 8 comments

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 8153171

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.

clragon avatar Aug 13 '20 20:08 clragon

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).

renancaraujo avatar Aug 14 '20 08:08 renancaraujo

beautiful, that should to it. thank you.

clragon avatar Aug 14 '20 08:08 clragon

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.

clragon avatar Aug 14 '20 20:08 clragon

You have to use animateToPage

renancaraujo avatar Aug 28 '20 10:08 renancaraujo

🤔 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.

clragon avatar Aug 28 '20 16:08 clragon

For sure, loadingBuilder has to be moved to PhotoViewGalleryPageOptions

renancaraujo avatar Aug 31 '20 08:08 renancaraujo

I have solved this with #339

clragon avatar Sep 18 '20 11:09 clragon

I think it would still be nice to have the loading builder inside the options.

clragon avatar Oct 13 '20 15:10 clragon