How to preload images with cached_network_image? - flutter

I've just implemented the Flutter package cached_network_image and I wonder how I can preload images so they're available later in an instant. I retrieve all image-urls that will be used later from the our server.
I've defined my custom cache manager getter:
class LocalCacheManager {
static const key = 'customCacheKey';
static CacheManager instance = CacheManager(
Config(
key,
stalePeriod: const Duration(days: 14),
maxNrOfCacheObjects: 200,
repo: JsonCacheInfoRepository(databaseName: key),
fileSystem: LocalCacheFileSystem(key),
fileService: HttpFileService(),
),
);
}
Here's how I currently try to preload the image:
LocalCacheManager.instance.downloadFile(MY_IMAGE_URL)),
And here's how I create the widget:
child: CachedNetworkImage(imageUrl: MY_IMAGE_URL, cacheManager: LocalCacheManager.instance),
But I can clearly see that files are always cached again as soon as I create the CachedNetworkImage.

You can use Flutter Cache Manager like this
DefaultCacheManager().downloadFile(MY_IMAGE_URL).then((_) {});
Later, just use your cached image like this
child: CachedNetworkImage(imageUrl: MY_IMAGE_URL,),

Simplest and workable way is to use precacheImage (flutter build-in function) with CachedNetworkImageProvider:
Image image = Image(
image: CachedNetworkImageProvider(/* url */),
fit: BoxFit.cover,
);
precacheImage(image.image, context);
return Padding(
padding: EdgeInsets.symmetric(horizontal: 16.0),
child: ClipRRect(
borderRadius: BorderRadius.circular(8.0),
child: image,
),
);

cached_network_image: ^2.0.0
CachedNetworkImage(
imageUrl: yoururl,
errorWidget: (context, url, error) => Text("error"),
imageBuilder: (context, imageProvider) => CircleAvatar(
radius: 120,
backgroundImage: imageProvider,
),
placeholder: (context, url) => CircularProgressIndicator(
backgroundColor: primary,
),
),

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),
),

Any widgets in flutter allow me to have a profile picture but if they don't have one the initials show instead?

Im trying to make a flutter app for HR and for my employee's section I need to have it so that their initials are shown if a profile picture is unavailable is there a widget which allows me to do this?
Below is an example of what im trying to achieve
You can try it with CachedNetworkImage
CachedNetworkImage(
imageUrl: "http://myimage...",
placeholder: (context, url) => new CircularProgressIndicator(),
errorWidget: (context, url, error) => new Text("my initials here"),
),
You need to Do Something Like This, but first Need to check for Image thats depends on how you are managing data and then
_isImageAvailable ? ProfileImage() : Text(Name)
Container(
height: 100.0,
width: 100.0,
color: Colors.grey,
child: ClipRRect(
borderRadius: BorderRadius.circular(8.0),
child: hasImage
? Image.network(
imageUrl,
fit: BoxFit.contain,
)
: Center(
child: Text('$nameInitial'),
),
),
)

Caching images on mobile with flutter

I used https://pub.dev/packages/cached_network_image to cache images it's work with pages but when I re open the app again it reload the image again, I need to store the image on mobile with database and call it again or something like that
if when you enter again it reloads the image it is because something failed with the cache, try with this code use these plugins
cached_network_image: ^3.0.0
flutter_cache_manager: ^3.2.0
static final customCacheManager = CacheManager(Config('customCacheKey',
stalePeriod: Duration(days: 15), maxNrOfCacheObjects: 100));
CachedNetworkImage(
cacheManager: customCacheManager,
height: 40,
width: 40,
fit: BoxFit.cover,
imageUrl: imageUrl,
placeholder: (context, url) => CircularProgressIndicator(
valueColor: AlwaysStoppedAnimation<Color>(Colors.white),
backgroundColor: const Color(0xFF02204c),
),
errorWidget: (context, url, error) => Center(
child: Image.asset(
'assets/images/errorImage.png',
height: 60,
width: 60,
),
),
),

flutter OpenContainer problems

Flexible(
fit: FlexFit.tight,
child: OpenContainer(
closedShape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(20))),
transitionDuration: const Duration(milliseconds: 1000),
openBuilder: (context, _) {
return MealDetailScreen(
meal: meal,
);
},
closedBuilder: (context, openContainer) {
return GestureDetector(
onTap: openContainer,
child: ClipRRect(
borderRadius: BorderRadius.all(Radius.circular(20)),
child: Image.asset(
meal.imagePath,
width: 150,
fit: BoxFit.fill,
),
),
);
},
),
),
The method 'OpenContainer' isn't defined for the type '_MealCard'. (Documentation) Try correcting the name to the name of an existing method, or defining a method named 'OpenContainer'.
When i try use OpenContainer i am encountering this error. I dont have animations.dart in my flutter version.do i need this dart ? Or is there another source of the problem? What should I do to write the code in the correct format? Thanks to everyone who has helped/tryed to help so far!
Yes, OpenContainer is part of the animations package and if you want to use it, you must install it.

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)