resizing dynamically a CachedNetworkImage image in flutter - flutter

I want to display dynamically an image from internet in flutter according to its size
CachedNetworkImage(
fit: if (width >height) {BoxFit.fitWidth}else{BoxFit.fitHeight},
imageUrl: "url",
placeholder: (context, url) =>
CupertinoActivityIndicator(),
errorWidget: (context, url, error) =>
Icon(Icons.error),
)
I want to have width and the height of the image so I can define which fit I want to use
thanks

Maybe you just need BoxFit.contain?

Related

FLUTTER - Reduce the quality of network image coming from http request so that it can load faster

I'm trying to fetch images through an http request and ends up getting an image with a high resolution which sometimes is not loading if the internet connection is poor.
Is there any package/plugin available which can reduce the quality of an image that is coming from an http request?
Image.network(
"${widget.jsonData["articles"][index]["urlToImage"]}",
fit: BoxFit.fill,
),
it's better if you use cache_network_image :
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),
),
The quality of the image is determined from the image file format. If it is a Jpeg or PNG image it can be saved as "progressive" (aka interlaced) and saved as such on the backend.
Baseline JPEG loads all of the image in one step.
Progressive (interlaced) JPEG is suitable for large images where a blurred image is shown upon initial loading of the file, and as the file is loaded, more details are made visible.
You can convert the images with an editor such as IrfanView.
I don't believe you can do anything on the client side to control this behavior.

Flutter - How to handle loading of images that may not exist

Is there any way to handle image url that not containt an image by using CachedNetworkImage package?
This is my code and when I do images loading, these errors are always popping up
CachedNetworkImage(
imageUrl: imageUrl,
placeholder: (context, url) => Center(
child: CircularProgressIndicator(
color: Theme.of(context).accentColor)),
errorWidget: (context, url, error) => BookErrorImage(),
fit: BoxFit.cover,
),
[]

Render an image widget before showing on screen

My app has a list of pages you can swipe between. These pages include network images. The problem is, the network images are fetched and built only when they are visible, meaning the user watches the whole process happen.
I understand this is the intentional functionality of flutter for efficiency, but I am looking for a way to fetch, load and cache a page (that contains network images) before the user navigates to it.
Now
Goal
In your pubspec.yaml file,
Add this as a dependency
meet_network_image:^0.1.2
In your dart file, where you want the image to be, place this line of code
MeetNetworkImage(
imageUrl:
"Your image url.jpg",
loadingBuilder: (context) => Center(
child: CircularProgressIndicator(),
),
errorBuilder: (context, e) => Center(
child: Text('Error appear!'),
),
),
just use cached_network_image package like this:
CachedNetworkImage(
imageUrl: "image path",
placeholder: (context, url) => CircularProgressIndicator(),
errorWidget: (context, url, error) => Icon(Icons.error),
),

Flutter not loading images that failed to load the first time

Switch airplane on mode and navigate to a StatefulWidget that includes a NetworkImage. The image fails to load since there is no connectivity. Now switch on airplane mode off. The image doesn't load. Navigating to another view and back again also doesn't help. Minimizing the app doesn't help, but restarting it completely does.
How would you resolve this? How do I make Flutter try to reload the image connectivity is regained?
Add the flutter_image plugin and use NetworkImageWithRetry.
After 2 days of trying all image plugins and answers on stackoverflow. I realized the error goes away when I go back and come back to same screen. So I retried loading images 3 times.
CachedNetworkImage(
key: ValueKey(imageUrl
.split),
placeholder: (context, url) => Padding(
padding: EdgeInsets.all(
MediaQuery.of(context).size.width * 0.2),
child: CircularProgressIndicator()),
errorWidget: (context, url, error) => CachedNetworkImage(
key: ValueKey(imageUrlsplit +
'retry1'),
placeholder: (context, url) =>
Padding(padding: EdgeInsets.all(MediaQuery.of(context).size.width * 0.2), child: CircularProgressIndicator()),
errorWidget: (context, url, error) => CachedNetworkImage(key: ValueKey(imageUrlsplit + 'retry2'), placeholder: (context, url) => Padding(padding: EdgeInsets.all(MediaQuery.of(context).size.width * 0.2), child: CircularProgressIndicator()), errorWidget: (context, url, error) => Text(error.toString()), imageUrl: imageUrlsplit),
imageUrl: imageUrlsplit),
imageUrl: imageUrlsplit)

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)