mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
Using ImageResource solves two problems: 1) Listeners can be notified synchronously when the sky.Image is already available. This change removes flash of 0x0 layout when moving an already-cached image around in the render tree. 2) In the future, when we support animated images, we can notify listeners multiple times whenever a new image is available.
29 lines
953 B
Dart
29 lines
953 B
Dart
// Copyright 2015 The Chromium Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file.
|
|
|
|
import 'dart:async';
|
|
import 'dart:collection';
|
|
import 'dart:sky' as sky;
|
|
|
|
import 'package:mojo/mojo/url_response.mojom.dart';
|
|
import 'package:sky/base/image_resource.dart';
|
|
import 'package:sky/mojo/net/fetch.dart';
|
|
|
|
final HashMap<String, ImageResource> _cache = new Map<String, ImageResource>();
|
|
|
|
ImageResource load(String url) {
|
|
return _cache.putIfAbsent(url, () {
|
|
Completer<sky.Image> completer = new Completer<sky.Image>();
|
|
fetchUrl(url).then((UrlResponse response) {
|
|
if (response.statusCode >= 400) {
|
|
print("Failed (${response.statusCode}) to load image ${url}");
|
|
completer.complete(null);
|
|
} else {
|
|
new sky.ImageDecoder(response.body.handle.h, completer.complete);
|
|
}
|
|
});
|
|
return new ImageResource(completer.future);
|
|
});
|
|
}
|