How to apply Ink.image() on CachedNetworkImage()? - flutter

I want my image to have inkwell effect when tapped, previously I could do that with Ink.image widget and then Provide NetworkImage() as image attribute like this:
Ink.image(
fit: BoxFit.fitWidth,
image: NetworkImage(
product.imageUrl,
),
),
But now I want to use CachedNetworkImage() because it has placeholder property to show loading status of my network image but the problem is when I use cachedNetworkImage i can no longer wrap this widget with ink.image because it requires image as an attrubute not any other widget.

Ink.image constructor requires image param to be a ImageProvider, not a widget. You can use CachedNetworkImageProvider instead of CachedNetworkImage like this:
Ink.image(
fit: BoxFit.fitWidth,
image: CachedNetworkImageProvider(
product.imageUrl,
)
),
However with this you'll lose placeholder capacity. However you can use CachedNetworkImageProvider createStream method to get an ImageStream that can be listened for errors and completion, with which you can create your own custom widget that updates the UI based on this stream.

You can use the imageBuilder from the CachedNetworkImage and pass the imageProvider to Ink.image.
Here's a sample code.
CachedNetworkImage(
imageUrl: "$imagePath",
imageBuilder: (context, imageProvider) {
return Ink.image(
image: imageProvider,
fit: fit,
);
},
)

Related

How can I show an asset Image while NetworkImage is loading

I am not able to show a circular loading indicator or an image while flutter is loading the image from the network.
image: DecorationImage(
image: NetworkImage(
Juegos_de_siempre[index].url_foto,
),
fit: BoxFit.fill,
),
Flutter's documentation suggested the usage of FadeInImage for the placeholder functionality. Here's a link to an example on Flutter's documentation.
You cannot use FadeInImage inside a DecorationImage, so you have to wrap it inside a Stack instead like shown in this thread.
Use CacheNetworkImage Package.Click here.
This is sample code.
CachedNetworkImage(
width: 100,
height: 100,
fit: BoxFit.cover,
imageUrl: imageUrl,
placeholder: (context, url) => const Center(
child: CupertinoActivityIndicator(),
), // replace with your asset image
errorWidget: (context, url, error) =>
const Icon(Icons.error),
),

Set container width according to image.network widh in flutter?

i want to fix my container size(width) according to the image which is getting from api(image.network..).So, how can i go for this?
i am trying to set dynamic width of container according to image which i get from image.network() in flutter.
You will get overflow error in your device when the image was bigger than your device screen. I recommend you to use this
Container(
width: 100,
height: 100,
decoration: const BoxDecoration(
image: DecorationImage(
fit: BoxFit.cover,
image: NetworkImage(
'image link'),
),
),
),

use loadingBuilder in NetworkImage class flutter

I faced some problems in my flutter app when I use network image:
the image takes too much time to load.
I found the solution by adding loading Builder prosperity in the network.image widget like that :
Image.network( 'URL image}',
fit: BoxFit.fitHeight,
loadingBuilder:(BuildContext context, Widget child,ImageChunkEvent loadingProgress) {
if (loadingProgress == null) return child;
return Center(
child: CircularProgressIndicator(
value: loadingProgress.expectedTotalBytes != null ?
loadingProgress.cumulativeBytesLoaded / loadingProgress.expectedTotalBytes
: null,
),
);
},
),
This works out right now, but then I need to add the same property (loadingBuilder) with the NetworkImage class (into the BoxDecoration)
as follows:
image: DecorationImage(
image: NetworkImage(widget.cover),
fit: BoxFit.cover,
),
How do I add the loadingBuilder with the NetworkImage class (in the last part of the code)?
There is a solution that I used although it seems like trick.
Using 'Stack' widget, at first show loading widget and draw image widget over that.
If that, at first loading widget will be shown and after image loading completed, image will be shown over the loading widget.
Stack(
children: <Widget>[
// Locate loading widget
LoadingWidget(),
//
Container(
decoration: BoxDecoration(
image: DecorationImage(
image: NetworkImage(widget.cover),
fit: BoxFit.cover,
),
),
),
],
);

Can't set Image.file to Circular Avatar in flutter

I am trying to use Circular Avatar in my design. however i want to set the image based on a file chosen by the User in their gallery. this image is set to:
File _imageUpload;
This loads properly if i use a container with Image.file
However, CircularAvatar wont accept it as part of the backgroundImage property.
am i meant to convert the file to another file type before assigning it to the Circular Avatar?
Try this code
CircleAvatar(
radius: 57,
backgroundColor: Color(0xff476cfb),
child: ClipOval(
child: new SizedBox(
width: 100.0,
height: 100.0,
child: (_image != null)
? Image.file(
_image,
fit: BoxFit.fill,
)
: Image.network(
"Any Url from the internet to display image",
fit: BoxFit.fill,
),
),
),
),
I found the issue. In circular avatar you should not use Image.file. instead you should be using FileImage
Use Image.file for showing a local photo
Image.file(_imageUpload)

How can I convert CachedNetworkImage to ImageProvider?

I want to show CachedNetworkImage in CircleAvatar Widget but backgroundImage parameter require ImageProvider.
Use CachedNetworkImageProvider
Creates an ImageProvider which loads an image from the url, using the
scale. When the image fails to load errorListener is called.
If what you want is just a circle-shaped CachedNetworkImage, you can use ClipOval:
ClipOval(
child: CachedNetworkImage(
width: 32,
height: 32,
fit: BoxFit.cover,
imageUrl: 'BLA.jpg',
placeholder: CircularProgressIndicator(),
),
),
please use imageBuilder from CachedNetworkImage
CachedNetworkImage(
placeholder: (context, url) => CircularProgressIndicator(),
errorWidget: (context, url, error) => new Icon(Icons.error),
fit: BoxFit.contain,
imageUrl: imagePath,
imageBuilder: (context, imageProvider) { // you can access to imageProvider
return CircleAvatar( // or any widget that use imageProvider like (PhotoView)
backgroundImage: imageProvider,
);
},
)
If your intention is to show the image inside a circular.
Use:
ClipRRect(borderRadius: BorderRadius.circular(100),
child: CachedNetworkImage(...))
If you want to convert the CachedNetworkImage into ImageProvider
Use:
CachedNetworkImageProvider(url)