Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

on loaded notification / callback needed #71

Closed
BerndWessels opened this issue Aug 28, 2018 · 15 comments
Closed

on loaded notification / callback needed #71

BerndWessels opened this issue Aug 28, 2018 · 15 comments

Comments

@BerndWessels
Copy link

I often need to know if the image is still loading or has successfully loaded yet.

Can you please add a callback or notification for the loading state.

@RyanYANG52
Copy link

I added two callback, and tested in my project. Hope that helps
#97

@Cotspheer
Copy link

Would love to see this merged! Exactly what I need, because images may not be available for a long time I want to hide the whole widget when it fails to load an image. This would make it possible.

@renefloor
Copy link
Collaborator

Now working with the builders

@RyanYANG52
Copy link

How is the builders going to notify when the image is loaded. Placeholder builder called when start loading, error widget builder called when image loaded failed.

@renefloor
Copy link
Collaborator

@RyanYANG52 If you set the imageBuilder with a simple Image yourself you also get a callback on loaded.

@RyanYANG52
Copy link

OK, thanks.

@kwent
Copy link

kwent commented Feb 6, 2020

@renefloor We need a tutorial or a section in the README cause i doubt most of us know how you do it ...

@kwent
Copy link

kwent commented Feb 6, 2020

Actually i figure it out. Posting code here for others.

final VoidCallback onLoaded;
...
CachedNetworkImage(
  imageUrl: "http://via.placeholder.com/200x150",
  imageBuilder: (context, imageProvider) {
    if (onLoaded != null) {
      onLoaded();
    }
    Container(
      decoration: BoxDecoration(
        image: DecorationImage(
            image: imageProvider,
            fit: BoxFit.cover,
            colorFilter: ColorFilter.mode(Colors.red, BlendMode.colorBurn)),
      ),
    );
  },
  placeholder: (context, url) => CircularProgressIndicator(),
  errorWidget: (context, url, error) => Icon(Icons.error),
);

@guenth39
Copy link

This is way too complicated and too much unnecessary code in my opinion. Why do I have to write the complete imageBuilder myself just because I want to know when the image is loaded @renefloor ? What's wrong with adding a simple onLoadedCallback or at least calling the progressIndicatorBuilder again with progress = 1.0 in every case when the loading is done?
The answer from @kwent works but clean and clear code looks different to me.

@kamleshwebtech
Copy link

Actually i figure it out. Posting code here for others.

final VoidCallback onLoaded;
...
CachedNetworkImage(
  imageUrl: "http://via.placeholder.com/200x150",
  imageBuilder: (context, imageProvider) {
    if (onLoaded != null) {
      onLoaded();
    }
    Container(
      decoration: BoxDecoration(
        image: DecorationImage(
            image: imageProvider,
            fit: BoxFit.cover,
            colorFilter: ColorFilter.mode(Colors.red, BlendMode.colorBurn)),
      ),
    );
  },
  placeholder: (context, url) => CircularProgressIndicator(),
  errorWidget: (context, url, error) => Icon(Icons.error),
);

@kwent What is onLoaded? Kindly add a note for the same. Thanks.

@kamleshwebtech
Copy link

Actually i figure it out. Posting code here for others.

final VoidCallback onLoaded;
...
CachedNetworkImage(
  imageUrl: "http://via.placeholder.com/200x150",
  imageBuilder: (context, imageProvider) {
    if (onLoaded != null) {
      onLoaded();
    }
    Container(
      decoration: BoxDecoration(
        image: DecorationImage(
            image: imageProvider,
            fit: BoxFit.cover,
            colorFilter: ColorFilter.mode(Colors.red, BlendMode.colorBurn)),
      ),
    );
  },
  placeholder: (context, url) => CircularProgressIndicator(),
  errorWidget: (context, url, error) => Icon(Icons.error),
);

still getting "CacheManager: Failed to download file from...." error message in VS code console panel.

@kamleshwebtech
Copy link

Now working with the builders

Kindly share your implemented solution. I am still searching the solution. Thanks.

@fareesh
Copy link

fareesh commented Jul 23, 2021

You can write something like this - since the imageBuilder is invoked after the image is loaded, if I understand correctly

imageBuilder: (context, imageProvider) {
  /// Onload code goes here
  return Image(image: imageProvider);
}

@Edijae
Copy link

Edijae commented Jun 14, 2022

I agree. there should be fail and success methods just like in other image loading libraries like Glide.

@DavidOrakpo
Copy link

DavidOrakpo commented Feb 19, 2024

2024 and the solution given here is still not clear.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants