Introduced ImageProvider for asynchronously loading images.

Updated image cache to use any ImageProvider instance.
Renamed RawImage to AsyncImage and updated the interface
to use any ImageProvider instance.
This commit is contained in:
Misha Dynin
2015-11-18 17:41:17 -08:00
parent 5734821f48
commit 7436dfd177
2 changed files with 35 additions and 18 deletions

View File

@@ -11,24 +11,44 @@ import 'fetch.dart';
import 'image_decoder.dart';
import 'image_resource.dart';
Future<ui.Image> _fetchImage(String url) async {
UrlResponse response = await fetchUrl(url);
if (response.statusCode >= 400) {
print("Failed (${response.statusCode}) to load image $url");
return null;
/// Implements a way to retrieve an image, for example by fetching it from the network.
/// Also used as a key in the image cache.
abstract class ImageProvider {
Future<ui.Image> loadImage();
}
class _UrlFetcher implements ImageProvider {
final String _url;
_UrlFetcher(this._url);
Future<ui.Image> loadImage() async {
UrlResponse response = await fetchUrl(_url);
if (response.statusCode >= 400) {
print("Failed (${response.statusCode}) to load image $_url");
return null;
}
return await decodeImageFromDataPipe(response.body);
}
return await decodeImageFromDataPipe(response.body);
bool operator ==(other) => other is _UrlFetcher && _url == other._url;
int get hashCode => _url.hashCode;
}
class _ImageCache {
_ImageCache._();
final Map<String, ImageResource> _cache = new Map<String, ImageResource>();
final Map<ImageProvider, ImageResource> _cache =
new Map<ImageProvider, ImageResource>();
ImageResource loadProvider(ImageProvider provider) {
return _cache.putIfAbsent(provider, () {
return new ImageResource(provider.loadImage());
});
}
ImageResource load(String url) {
return _cache.putIfAbsent(url, () {
return new ImageResource(_fetchImage(url));
});
return loadProvider(new _UrlFetcher(url));
}
}

View File

@@ -2,7 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:typed_data';
import 'dart:ui' as ui;
import 'package:flutter/rendering.dart';
@@ -10,7 +9,6 @@ import 'package:flutter/services.dart';
import 'framework.dart';
export 'dart:typed_data' show Uint8List;
export 'package:flutter/rendering.dart' show
BackgroundImage,
BlockDirection,
@@ -1170,10 +1168,10 @@ class DefaultAssetBundle extends InheritedWidget {
bool updateShouldNotify(DefaultAssetBundle old) => bundle != old.bundle;
}
class RawImage extends StatelessComponent {
RawImage({
class AsyncImage extends StatelessComponent {
AsyncImage({
Key key,
this.bytes,
this.provider,
this.width,
this.height,
this.colorFilter,
@@ -1182,7 +1180,7 @@ class RawImage extends StatelessComponent {
this.centerSlice
}) : super(key: key);
final Uint8List bytes;
final ImageProvider provider;
final double width;
final double height;
final ColorFilter colorFilter;
@@ -1191,9 +1189,8 @@ class RawImage extends StatelessComponent {
final Rect centerSlice;
Widget build(BuildContext context) {
ImageResource image = new ImageResource(decodeImageFromList(bytes));
return new ImageListener(
image: image,
image: imageCache.loadProvider(provider),
width: width,
height: height,
colorFilter: colorFilter,