Commit 5161d120 authored by Kris Giesing's avatar Kris Giesing

Give scale parameter to ImageCache and NetworkImage

parent d4cc315c
...@@ -78,7 +78,7 @@ class RenderImage extends RenderBox { ...@@ -78,7 +78,7 @@ class RenderImage extends RenderBox {
markNeedsLayout(); markNeedsLayout();
} }
/// If non-null, specify the image's scale. /// Specifies the image's scale.
/// ///
/// Used when determining the best display size for the image. /// Used when determining the best display size for the image.
double get scale => _scale; double get scale => _scale;
......
...@@ -43,9 +43,9 @@ class NetworkAssetBundle extends AssetBundle { ...@@ -43,9 +43,9 @@ class NetworkAssetBundle extends AssetBundle {
abstract class CachingAssetBundle extends AssetBundle { abstract class CachingAssetBundle extends AssetBundle {
final Map<String, ImageResource> imageResourceCache = final Map<String, ImageResource> imageResourceCache =
new Map<String, ImageResource>(); <String, ImageResource>{};
final Map<String, Future<String>> _stringCache = final Map<String, Future<String>> _stringCache =
new Map<String, Future<String>>(); <String, Future<String>>{};
Future<ImageInfo> fetchImage(String key) async { Future<ImageInfo> fetchImage(String key) async {
return new ImageInfo(image: await decodeImageFromDataPipe(await load(key))); return new ImageInfo(image: await decodeImageFromDataPipe(await load(key)));
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
// found in the LICENSE file. // found in the LICENSE file.
import 'dart:async'; import 'dart:async';
import 'dart:ui' show hashValues;
import 'package:mojo/mojo/url_response.mojom.dart'; import 'package:mojo/mojo/url_response.mojom.dart';
import 'package:quiver/collection.dart'; import 'package:quiver/collection.dart';
...@@ -19,8 +20,9 @@ abstract class ImageProvider { ...@@ -19,8 +20,9 @@ abstract class ImageProvider {
class _UrlFetcher implements ImageProvider { class _UrlFetcher implements ImageProvider {
final String _url; final String _url;
final double _scale;
_UrlFetcher(this._url); _UrlFetcher(this._url, this._scale);
Future<ImageInfo> loadImage() async { Future<ImageInfo> loadImage() async {
UrlResponse response = await fetchUrl(_url); UrlResponse response = await fetchUrl(_url);
...@@ -28,11 +30,17 @@ class _UrlFetcher implements ImageProvider { ...@@ -28,11 +30,17 @@ class _UrlFetcher implements ImageProvider {
print("Failed (${response.statusCode}) to load image $_url"); print("Failed (${response.statusCode}) to load image $_url");
return null; return null;
} }
return new ImageInfo(image: await decodeImageFromDataPipe(response.body)); return new ImageInfo(
image: await decodeImageFromDataPipe(response.body),
scale: _scale
);
} }
bool operator ==(other) => other is _UrlFetcher && _url == other._url; bool operator ==(other) {
int get hashCode => _url.hashCode; return other is _UrlFetcher && _url == other._url && _scale == other._scale;
}
int get hashCode => hashValues(_url, _scale);
} }
const int _kDefaultSize = 1000; const int _kDefaultSize = 1000;
...@@ -52,8 +60,8 @@ class _ImageCache { ...@@ -52,8 +60,8 @@ class _ImageCache {
}); });
} }
ImageResource load(String url) { ImageResource load(String url, { double scale: 1.0 }) {
return loadProvider(new _UrlFetcher(url)); return loadProvider(new _UrlFetcher(url, scale));
} }
} }
......
...@@ -1531,7 +1531,7 @@ class RawImage extends LeafRenderObjectWidget { ...@@ -1531,7 +1531,7 @@ class RawImage extends LeafRenderObjectWidget {
/// aspect ratio. /// aspect ratio.
final double height; final double height;
/// If non-null, specify the image's scale. /// Specifies the image's scale.
/// ///
/// Used when determining the best display size for the image. /// Used when determining the best display size for the image.
final double scale; final double scale;
...@@ -1700,6 +1700,7 @@ class NetworkImage extends StatelessComponent { ...@@ -1700,6 +1700,7 @@ class NetworkImage extends StatelessComponent {
this.src, this.src,
this.width, this.width,
this.height, this.height,
this.scale : 1.0,
this.color, this.color,
this.fit, this.fit,
this.alignment, this.alignment,
...@@ -1722,6 +1723,11 @@ class NetworkImage extends StatelessComponent { ...@@ -1722,6 +1723,11 @@ class NetworkImage extends StatelessComponent {
/// aspect ratio. /// aspect ratio.
final double height; final double height;
/// Specifies the image's scale.
///
/// Used when determining the best display size for the image.
final double scale;
/// If non-null, apply this color filter to the image before painting. /// If non-null, apply this color filter to the image before painting.
final Color color; final Color color;
...@@ -1749,7 +1755,7 @@ class NetworkImage extends StatelessComponent { ...@@ -1749,7 +1755,7 @@ class NetworkImage extends StatelessComponent {
Widget build(BuildContext context) { Widget build(BuildContext context) {
return new RawImageResource( return new RawImageResource(
image: imageCache.load(src), image: imageCache.load(src, scale: scale),
width: width, width: width,
height: height, height: height,
color: color, color: color,
......
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