photo_view icon indicating copy to clipboard operation
photo_view copied to clipboard

[BUG] NetworkImages being loaded again on every swipe

Open JuliusJacobsohn opened this issue 2 years ago • 4 comments

Describe the bug

Using the provided gallery example from the readme (example #2), but with NetworkImage instead of AssetImage, causes each image to be downloaded again when swiping into it, even though it has been loaded before.

To Reproduce

I built the following minimal example that showcases this behaviour:

class WebGalleryWidget extends StatelessWidget {
  final List<NetworkImage> galleryItems = [
    NetworkImage("https://via.placeholder.com/3000"),
    NetworkImage("https://via.placeholder.com/2999"),
    NetworkImage("https://via.placeholder.com/2998"),
    NetworkImage("https://via.placeholder.com/2997"),
    NetworkImage("https://via.placeholder.com/2996"),
    NetworkImage("https://via.placeholder.com/2995"),
  ];

  WebGalleryWidget({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
        child: PhotoViewGallery.builder(
      scrollPhysics: const BouncingScrollPhysics(),
      builder: (BuildContext context, int index) {
        return PhotoViewGalleryPageOptions(
          imageProvider: galleryItems[index],
          initialScale: PhotoViewComputedScale.contained * 0.8,
        );
      },
      itemCount: galleryItems.length,
      loadingBuilder: (context, event) => Center(
        child: Container(
          width: 20.0,
          height: 20.0,
          child: CircularProgressIndicator(
            value: event == null || event.expectedTotalBytes == null
                ? 0
                : event.cumulativeBytesLoaded / event.expectedTotalBytes!,
          ),
        ),
      ),
    ));
  }
}

Tested on a Pixel 3a.

What is the current behavior?

When swiping between images, they get loaded again 95% of the time, showing the loading indicator etc. This is even more apparent when loading larger images from slower Hosters. Change the placeholder image URLs to see this in action. A video can be seen here

Expected behavior

If an image has been loaded before, it should stay that way, and not load again.

Screenshots

Which versions of Flutter/Photo View, and which browser / OS are affected by this issue? Did this work in previous versions of Photo View?

Flutter Channel stable, 3.0.4 photo_view: ^0.14.0

No idea if it worked in any other versions.

JuliusJacobsohn avatar Jul 09 '22 16:07 JuliusJacobsohn

I am not a flutter / bluefireteam dev, but can you try using CachedNetworkImageProvider instead of NetworkImage, and see if it does the trick?

beriaanirudh avatar Jul 18 '22 08:07 beriaanirudh

Sadly, CachedNetworkImageProvider leads to some weird animation bugs. The image stays invisible until the swipe animation is fully completed. Then, it just pops into existence.

JuliusJacobsohn avatar Jul 18 '22 09:07 JuliusJacobsohn

Any news on this?

JuliusJacobsohn avatar Jul 26 '22 09:07 JuliusJacobsohn

Any news on this? Didn't have time to look into it yet. Fell free to investigate

renancaraujo avatar Jul 26 '22 10:07 renancaraujo

thats expected behaviour. A PageView builds its children dynamically and throws them away once they leave the screen. It is neither the responsibility of the PageView nor of this Package to ensure that your Images arent redownloaded. Personally, I use CachedNetworkImage + the customChild builder for my image galleries. they work okay.

clragon avatar Sep 12 '22 19:09 clragon

thats expected behaviour. A PageView builds its children dynamically and throws them away once they leave the screen. It is neither the responsibility of the PageView nor of this Package to ensure that your Images arent redownloaded. Personally, I use CachedNetworkImage + the customChild builder for my image galleries. they work okay.

I expected this to be the case, but I couldn't confirm it. But honestly, whatever is happening here should not be the default behaviour.

JuliusJacobsohn avatar Sep 12 '22 23:09 JuliusJacobsohn