Commit 7436dfd1 authored by Misha Dynin's avatar Misha Dynin

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.
parent 5734821f
......@@ -11,25 +11,45 @@ import 'fetch.dart';
import 'image_decoder.dart';
import 'image_resource.dart';
Future<ui.Image> _fetchImage(String url) async {
UrlResponse response = await fetchUrl(url);
/// 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");
print("Failed (${response.statusCode}) to load image $_url");
return null;
}
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 load(String url) {
return _cache.putIfAbsent(url, () {
return new ImageResource(_fetchImage(url));
ImageResource loadProvider(ImageProvider provider) {
return _cache.putIfAbsent(provider, () {
return new ImageResource(provider.loadImage());
});
}
ImageResource load(String url) {
return loadProvider(new _UrlFetcher(url));
}
}
final _ImageCache imageCache = new _ImageCache._();
......@@ -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,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment