Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
F
Front-End
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
abdullh.alsoleman
Front-End
Commits
04dfa0bf
Commit
04dfa0bf
authored
Nov 19, 2015
by
Misha Dynin
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #483 from mishadynin/imagecache
Introduced ImageProvider for asynchronously loading images.
parents
fd1dd7fa
7436dfd1
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
35 additions
and
18 deletions
+35
-18
image_cache.dart
packages/flutter/lib/src/services/image_cache.dart
+30
-10
basic.dart
packages/flutter/lib/src/widgets/basic.dart
+5
-8
No files found.
packages/flutter/lib/src/services/image_cache.dart
View file @
04dfa0bf
...
@@ -11,25 +11,45 @@ import 'fetch.dart';
...
@@ -11,25 +11,45 @@ import 'fetch.dart';
import
'image_decoder.dart'
;
import
'image_decoder.dart'
;
import
'image_resource.dart'
;
import
'image_resource.dart'
;
Future
<
ui
.
Image
>
_fetchImage
(
String
url
)
async
{
/// Implements a way to retrieve an image, for example by fetching it from the network.
UrlResponse
response
=
await
fetchUrl
(
url
);
/// Also used as a key in the image cache.
if
(
response
.
statusCode
>=
400
)
{
abstract
class
ImageProvider
{
print
(
"Failed (
${response.statusCode}
) to load image
$url
"
);
Future
<
ui
.
Image
>
loadImage
();
return
null
;
}
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
{
class
_ImageCache
{
_ImageCache
.
_
();
_ImageCache
.
_
();
final
Map
<
String
,
ImageResource
>
_cache
=
new
Map
<
String
,
ImageResource
>();
final
Map
<
ImageProvider
,
ImageResource
>
_cache
=
new
Map
<
ImageProvider
,
ImageResource
>();
ImageResource
load
(
String
url
)
{
ImageResource
load
Provider
(
ImageProvider
provider
)
{
return
_cache
.
putIfAbsent
(
url
,
()
{
return
_cache
.
putIfAbsent
(
provider
,
()
{
return
new
ImageResource
(
_fetchImage
(
url
));
return
new
ImageResource
(
provider
.
loadImage
(
));
});
});
}
}
ImageResource
load
(
String
url
)
{
return
loadProvider
(
new
_UrlFetcher
(
url
));
}
}
}
final
_ImageCache
imageCache
=
new
_ImageCache
.
_
();
final
_ImageCache
imageCache
=
new
_ImageCache
.
_
();
packages/flutter/lib/src/widgets/basic.dart
View file @
04dfa0bf
...
@@ -2,7 +2,6 @@
...
@@ -2,7 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// found in the LICENSE file.
import
'dart:typed_data'
;
import
'dart:ui'
as
ui
;
import
'dart:ui'
as
ui
;
import
'package:flutter/rendering.dart'
;
import
'package:flutter/rendering.dart'
;
...
@@ -10,7 +9,6 @@ import 'package:flutter/services.dart';
...
@@ -10,7 +9,6 @@ import 'package:flutter/services.dart';
import
'framework.dart'
;
import
'framework.dart'
;
export
'dart:typed_data'
show
Uint8List
;
export
'package:flutter/rendering.dart'
show
export
'package:flutter/rendering.dart'
show
BackgroundImage
,
BackgroundImage
,
BlockDirection
,
BlockDirection
,
...
@@ -1180,10 +1178,10 @@ class DefaultAssetBundle extends InheritedWidget {
...
@@ -1180,10 +1178,10 @@ class DefaultAssetBundle extends InheritedWidget {
bool
updateShouldNotify
(
DefaultAssetBundle
old
)
=>
bundle
!=
old
.
bundle
;
bool
updateShouldNotify
(
DefaultAssetBundle
old
)
=>
bundle
!=
old
.
bundle
;
}
}
class
Raw
Image
extends
StatelessComponent
{
class
Async
Image
extends
StatelessComponent
{
Raw
Image
({
Async
Image
({
Key
key
,
Key
key
,
this
.
bytes
,
this
.
provider
,
this
.
width
,
this
.
width
,
this
.
height
,
this
.
height
,
this
.
colorFilter
,
this
.
colorFilter
,
...
@@ -1193,7 +1191,7 @@ class RawImage extends StatelessComponent {
...
@@ -1193,7 +1191,7 @@ class RawImage extends StatelessComponent {
this
.
centerSlice
this
.
centerSlice
})
:
super
(
key:
key
);
})
:
super
(
key:
key
);
final
Uint8List
bytes
;
final
ImageProvider
provider
;
final
double
width
;
final
double
width
;
final
double
height
;
final
double
height
;
final
ColorFilter
colorFilter
;
final
ColorFilter
colorFilter
;
...
@@ -1203,9 +1201,8 @@ class RawImage extends StatelessComponent {
...
@@ -1203,9 +1201,8 @@ class RawImage extends StatelessComponent {
final
Rect
centerSlice
;
final
Rect
centerSlice
;
Widget
build
(
BuildContext
context
)
{
Widget
build
(
BuildContext
context
)
{
ImageResource
image
=
new
ImageResource
(
decodeImageFromList
(
bytes
));
return
new
ImageListener
(
return
new
ImageListener
(
image:
image
,
image:
image
Cache
.
loadProvider
(
provider
)
,
width:
width
,
width:
width
,
height:
height
,
height:
height
,
colorFilter:
colorFilter
,
colorFilter:
colorFilter
,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment