Baseflow / flutter_cached_network_image

Download, cache and show images in a flutter app
https://baseflow.com
2.44k stars 656 forks source link

using placeholder on CachedNetworkImageProvider or directly use CachedNetworkImage #177

Closed pishguy closed 5 years ago

pishguy commented 5 years ago

when i try to use this library on Container to make circle image i get error

The argument type 'CachedNetworkImage' can't be assigned to the parameter type 'ImageProvider'.

code:

image: DecorationImage(
  image: CachedNetworkImage(
    imageUrl: "http://via.placeholder.com/350x150",
    placeholder: (context, url) => new CircularProgressIndicator(),
    errorWidget: (context, url, error) => Image.asset("assets/images/profile.jpg"),
  ),
),

now i try to use another way for example:

image: new DecorationImage(image:new CachedNetworkImageProvider(url)),

so how can i have placeholder and errorWidget in this implementation?

colt005 commented 5 years ago

Did u find a solution to this?

pishguy commented 5 years ago

@colt005 no, i can't

colt005 commented 5 years ago

Use CachedNetworkImageProvider instead of CachedNetworkImage, It would solve the problem.

pishguy commented 5 years ago

@colt005

in my issue as i said

now i try to use another way for example: image: new DecorationImage(image:new CachedNetworkImageProvider(url)), so how can i have placeholder and errorWidget in this implementation?

colt005 commented 5 years ago

I used an if condition to check if the url is empty, it simply solved the issue in my use-case.

renefloor commented 5 years ago

@MahdiPishguy sorry for my late reaction.

ImageProviders can only return 1 image, so this cannot return a placeholder first. All image widgets on Android expect an imageprovider, so indeed you cannot use CachedNetworkImage directly. CachedNetworkImage does provide a callback which gives an imageprovider. What you can do is something like this (I'll improve the readme file to show this option):

CachedNetworkImage(
  imageUrl: "http://notAvalid.uri",
  imageBuilder: (context, imageProvider) => Container(
        decoration: BoxDecoration(
          image: DecorationImage(
            image: imageProvider,
            fit: BoxFit.cover,
          ),
        ),
      ),
  placeholder: (context, url) => CircularProgressIndicator(),
  errorWidget: (context, url, error) => Icon(Icons.error),
);
pishguy commented 5 years ago

@renefloor

thanks to reply, please let me to check that

renefloor commented 5 years ago

I've also added it to the readme now.

AekaderFELLAGUE commented 3 years ago

i have the same issue, i want to use CachedNetworkImage on image property for the DecorationImage but it says: The argument type 'CachedNetworkImage' can't be assigned to the parameter type 'ImageProvider' Any help

renefloor commented 3 years ago

If you want to use it with DecorationImage you need to use CachedNetworkImageProvider. If you want to combine it with a placeholder you can do like this:

CachedNetworkImage(
  imageUrl: "http://via.placeholder.com/200x150",
  imageBuilder: (context, imageProvider) => 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),
),
AbdulazizAlshami commented 5 months ago

How to use placeholder with CachedNetworkImageProvider?!