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

Image get from cache only #47

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 35 additions & 11 deletions lib/cached_network_image.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,19 @@ class CachedNetworkImage extends StatefulWidget {
this.placeholder,
@required this.imageUrl,
this.errorWidget,
this.fadeOutDuration: const Duration(milliseconds: 300),
this.fadeOutCurve: Curves.easeOut,
this.fadeInDuration: const Duration(milliseconds: 700),
this.fadeInCurve: Curves.easeIn,
this.fadeOutDuration = const Duration(milliseconds: 300),
this.fadeOutCurve = Curves.easeOut,
this.fadeInDuration = const Duration(milliseconds: 700),
this.fadeInCurve = Curves.easeIn,
this.width,
this.height,
this.fit,
this.alignment: Alignment.center,
this.repeat: ImageRepeat.noRepeat,
this.matchTextDirection: false,
this.matchTextDirection = false,
this.httpHeaders,
this.onlyIfCached = false,
this.cachOnlyFallbackWidget,
}) : assert(imageUrl != null),
assert(fadeOutDuration != null),
assert(fadeOutCurve != null),
Expand All @@ -53,6 +55,7 @@ class CachedNetworkImage extends StatefulWidget {
assert(alignment != null),
assert(repeat != null),
assert(matchTextDirection != null),
assert(onlyIfCached != null),
super(key: key);

/// Widget displayed while the target [imageUrl] is loading.
Expand Down Expand Up @@ -145,6 +148,13 @@ class CachedNetworkImage extends StatefulWidget {
// Optional headers for the http request of the image url
final Map<String, String> httpHeaders;

/// Gets the image only on a cache hit. Skips the download.
final bool onlyIfCached;

/// Widget displayed while [onlyIfCached] is true and the target [imageUrl]
/// failed loading.
final Widget cachOnlyFallbackWidget;

@override
State<StatefulWidget> createState() => new _CachedNetworkImageState();
}
Expand Down Expand Up @@ -228,7 +238,8 @@ class _CachedNetworkImageState extends State<CachedNetworkImage>
void initState() {
_hasError = false;
_imageProvider = new CachedNetworkImageProvider(widget.imageUrl,
headers: widget.httpHeaders, errorListener: _imageLoadingFailed);
headers: widget.httpHeaders, errorListener: _imageLoadingFailed,
onlyIfCached: widget.onlyIfCached);
_imageResolver =
new _ImageProviderResolver(state: this, listener: _updatePhase);

Expand Down Expand Up @@ -296,7 +307,7 @@ class _CachedNetworkImageState extends State<CachedNetworkImage>
_phase = ImagePhase.waiting;
break;
case ImagePhase.waiting:
if (_hasError && widget.errorWidget == null) {
if (_hasError && widget.errorWidget == null && widget.cachOnlyFallbackWidget == null) {
_phase = ImagePhase.completed;
return;
}
Expand Down Expand Up @@ -365,7 +376,8 @@ class _CachedNetworkImageState extends State<CachedNetworkImage>
return true;
case ImagePhase.fadeIn:
case ImagePhase.completed:
return _hasError && widget.errorWidget == null;
return _hasError && ( widget.onlyIfCached ?
widget.cachOnlyFallbackWidget == null : widget.errorWidget == null );
}

return null;
Expand All @@ -390,6 +402,10 @@ class _CachedNetworkImageState extends State<CachedNetworkImage>
return _fadedWidget(widget.placeholder);
}

if (_hasError && widget.onlyIfCached && widget.cachOnlyFallbackWidget != null) {
return _fadedWidget(widget.cachOnlyFallbackWidget);
}

if (_hasError && widget.errorWidget != null) {
return _fadedWidget(widget.errorWidget);
}
Expand Down Expand Up @@ -434,7 +450,7 @@ class CachedNetworkImageProvider
/// Creates an ImageProvider which loads an image from the [url], using the [scale].
/// When the image fails to load [errorListener] is called.
const CachedNetworkImageProvider(this.url,
{this.scale: 1.0, this.errorListener, this.headers})
{this.scale = 1.0, this.errorListener, this.headers, this.onlyIfCached = false})
: assert(url != null),
assert(scale != null);

Expand All @@ -450,6 +466,8 @@ class CachedNetworkImageProvider
// Set headers for the image provider, for example for authentication
final Map<String, String> headers;

final bool onlyIfCached;

@override
Future<CachedNetworkImageProvider> obtainKey(
ImageConfiguration configuration) {
Expand All @@ -469,7 +487,7 @@ class CachedNetworkImageProvider

Future<ui.Codec> _loadAsync(CachedNetworkImageProvider key) async {
var cacheManager = await CacheManager.getInstance();
var file = await cacheManager.getFile(url, headers: headers);
var file = await cacheManager.getFile(url, headers: headers, onlyFromCache: onlyIfCached);
if (file == null) {
if (errorListener != null) errorListener();
throw new Exception("Couldn't download or retreive file.");
Expand All @@ -488,7 +506,13 @@ class CachedNetworkImageProvider
throw new Exception("File was empty");
}

return await ui.instantiateImageCodec(bytes);
Future<ui.Codec> codecF = ui.instantiateImageCodec(bytes)
..catchError((e){
if (errorListener != null) errorListener();
throw new Exception("File not a valid Image");
});

return codecF;
}

@override
Expand Down
8 changes: 6 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ environment:
dependencies:
flutter:
sdk: flutter
flutter_cache_manager: ^0.2.0

#flutter_cache_manager: ^0.2.0
flutter_cache_manager:
git:
url: git://github.com/rmarau/flutter_cache_manager.git

dev_dependencies:
test: ^1.3.0
test: ^1.3.0