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

Add custom cache key to support dynamic url images #288

Closed
Hassico opened this issue Jan 21, 2021 · 16 comments
Closed

Add custom cache key to support dynamic url images #288

Hassico opened this issue Jan 21, 2021 · 16 comments

Comments

@Hassico
Copy link

Hassico commented Jan 21, 2021

Please add this feature because it is a vital one for many apps like mine. Thank you.

@zmtzawqlp
Copy link
Member

something like this ?

fluttercandies/extended_image_library#20

@anass-naoushi
Copy link

anass-naoushi commented Feb 25, 2021

@zmtzawqlp ,
No, something that overrides the the url passed and identifies an image from a unique key, like this:

ExtendedImage.network(
url: 'whatever it is',
cacheKey: 'Image 1' // The image is retrieved from cache using this key as an identifier!
);

@zmtzawqlp
Copy link
Member

store key with url+md5 now

@anass-naoushi
Copy link

But the url is dynamic, it changes every time it is fetched. Would not hashing it produce a different key every time the image is fetched?
One more thing, where do I insert the hashed url in the parameters of extendedImage. Can you please elaborate more using an example

@zmtzawqlp
Copy link
Member

I don't want to make this library more complex. we store key as md5 now, you can override image provider and make cache logic by yourself.

@zmtzawqlp
Copy link
Member

i will do this later

zmtzawqlp added a commit that referenced this issue Mar 1, 2021
* Improve:
  Add cacheKey for NetworkProvider. #288
  Web capability at pub.dev.
  Add change event for editor. #300
  Don't deprecate cornerSize and cornerColor
zmtzawqlp added a commit that referenced this issue Mar 2, 2021
* Improve:
  Add cacheKey for NetworkProvider. #288
  Web capability at pub.dev.
  Add change event for editor. #300

* Breaking change:
  Use [EditorCropLayerPainter] instead of [ExtendedImageCropLayerCornerPainter]
@momoDragon
Copy link

Hi, Firstly, thanks for this feature. However, i am realising that when i have the same cacheKey but different url, the widget blinks every-time there is a rebuild.

However when using cache network image, this does not occur.

ExtendedImage.network

Screen.Recording.2022-01-19.at.10.00.42.PM.mov

ExtendedImage.network(
widget.file.source,
cacheKey: widget.file.fileId,
retries: 5,
timeRetry: const Duration(seconds: 2),
enableMemoryCache: true,
loadStateChanged: (state) {
switch (state.extendedImageLoadState) {
case LoadState.loading:
return _ImageByState.loading();
case LoadState.failed:
return _ImageByState.failed();
case LoadState.completed:
return _ImageByState.successFolderFile(
image: state.imageProvider,
file: widget.file,
privateFolderController: widget.privateFolderController);
default:
return const SizedBox();
}
},
),

CachedNetworkImage

Screen.Recording.2022-01-19.at.10.01.11.PM.mov

CachedNetworkImage(
cacheKey: widget.file.fileId,
imageUrl: widget.file.source,
progressIndicatorBuilder: (context, url, downloadProgress) =>
CircularProgressIndicator(value: downloadProgress.progress),
errorWidget: (context, url, error) => Icon(Icons.error),
),

@zmtzawqlp
Copy link
Member

Hi, Firstly, thanks for this feature. However, i am realising that when i have the same cacheKey but different url, the widget blinks every-time there is a rebuild.

However when using cache network image, this does not occur.

ExtendedImage.network

Screen.Recording.2022-01-19.at.10.00.42.PM.mov
ExtendedImage.network( widget.file.source, cacheKey: widget.file.fileId, retries: 5, timeRetry: const Duration(seconds: 2), enableMemoryCache: true, loadStateChanged: (state) { switch (state.extendedImageLoadState) { case LoadState.loading: return _ImageByState.loading(); case LoadState.failed: return _ImageByState.failed(); case LoadState.completed: return _ImageByState.successFolderFile( image: state.imageProvider, file: widget.file, privateFolderController: widget.privateFolderController); default: return const SizedBox(); } }, ),

CachedNetworkImage

Screen.Recording.2022-01-19.at.10.01.11.PM.mov
CachedNetworkImage( cacheKey: widget.file.fileId, imageUrl: widget.file.source, progressIndicatorBuilder: (context, url, downloadProgress) => CircularProgressIndicator(value: downloadProgress.progress), errorWidget: (context, url, error) => Icon(Icons.error), ),

final bool gaplessPlayback;
it will help

@momoDragon
Copy link

momoDragon commented Jan 20, 2022

Hi, Firstly, thanks for this feature. However, i am realising that when i have the same cacheKey but different url, the widget blinks every-time there is a rebuild.
However when using cache network image, this does not occur.
ExtendedImage.network
Screen.Recording.2022-01-19.at.10.00.42.PM.mov
ExtendedImage.network( widget.file.source, cacheKey: widget.file.fileId, retries: 5, timeRetry: const Duration(seconds: 2), enableMemoryCache: true, loadStateChanged: (state) { switch (state.extendedImageLoadState) { case LoadState.loading: return _ImageByState.loading(); case LoadState.failed: return _ImageByState.failed(); case LoadState.completed: return _ImageByState.successFolderFile( image: state.imageProvider, file: widget.file, privateFolderController: widget.privateFolderController); default: return const SizedBox(); } }, ),
CachedNetworkImage
Screen.Recording.2022-01-19.at.10.01.11.PM.mov
CachedNetworkImage( cacheKey: widget.file.fileId, imageUrl: widget.file.source, progressIndicatorBuilder: (context, url, downloadProgress) => CircularProgressIndicator(value: downloadProgress.progress), errorWidget: (context, url, error) => Icon(Icons.error), ),

final bool gaplessPlayback;

it will help

just tried
gaplessPlayback: true,
does help abit that the LoadState.completed will remain even when i refresh, however it still blinks.

gaplessPlayback: false,

Screen.Recording.2022-01-20.at.9.36.51.AM.mov

gaplessPlayback: true,

Screen.Recording.2022-01-20.at.9.35.58.AM.mov

@momoDragon
Copy link

I also realised this is the case only when the url is different.

ExtendedImage.network( "https://www.planetware.com/wpimages/2020/03/best-hot-air-balloon-rides-cappadocia-turkey.jpg", cacheKey: file.fileId, retries: 5, timeRetry: const Duration(seconds: 2), enableMemoryCache: true, gaplessPlayback: false, loadStateChanged: (state) { switch (state.extendedImageLoadState) { case LoadState.loading: return _ImageByState.loading(); case LoadState.failed: return _ImageByState.failed(); case LoadState.completed: return _ImageByState.successFolderFile( image: state.imageProvider, file: file, privateFolderController: privateFolderController); default: return const SizedBox(); } }, ),

This code even with gaplessPlayback: false, it does not blink.
Its only when the url is dynamic that the images blink.

@zmtzawqlp
Copy link
Member

i didn't get what is that url is dynamic? maybe a simple runnable demo is better

@momoDragon
Copy link

`import 'dart:math';

import 'package:extended_image/extended_image.dart';
import 'package:flutter/material.dart';

class TestPage extends StatefulWidget {
const TestPage({Key? key}) : super(key: key);

@OverRide
_TestPageState createState() => _TestPageState();
}

class _TestPageState extends State {
@OverRide
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
Expanded(
child: GridView.builder(
padding:
const EdgeInsets.symmetric(horizontal: 15, vertical: 10),
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 4,
childAspectRatio: 1,
crossAxisSpacing: 10,
mainAxisSpacing: 10),
itemCount: 50,
itemBuilder: (BuildContext context, index) {
return ExtendedImage.network(
"https://www.planetware.com/wpimages/2020/03/best-hot-air-balloon-rides-cappadocia-turkey.jpg?index=" +
Random().nextInt(1000).toString(),
cacheKey: index.toString(),
retries: 5,
timeRetry: const Duration(seconds: 2),
enableMemoryCache: true,
gaplessPlayback: true,
loadStateChanged: (state) {
switch (state.extendedImageLoadState) {
case LoadState.loading:
return Container(color: Colors.yellow);
case LoadState.failed:
return Container(color: Colors.red);
case LoadState.completed:
return Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(5),
color: Colors.black12,
image: DecorationImage(
image: state.imageProvider,
fit: BoxFit.cover,
),
),
);
default:
return const SizedBox();
}
},
);
}),
),
TextButton(
onPressed: () {
setState(() {});
print('Tapped');
},
child: Text('set state'))
],
),
);
}
}
`

Screen.Recording.2022-01-20.at.9.56.56.AM.mov

@zmtzawqlp
Copy link
Member

"https://www.planetware.com/wpimages/2020/03/best-hot-air-balloon-rides-cappadocia-turkey.jpg?index=" +
Random().nextInt(1000).toString(),

ImageSource is changed every time, i can't find any solution. you can use CacheImageProvider instead of ExtendedImageProvider. i didn't do any thing about the dynamic url

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

4 participants