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
9e7311fe
Commit
9e7311fe
authored
May 16, 2016
by
Adam Barth
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add dartdocs to asset_bundle.dart (#3922)
Fixes #3077
parent
c5ff156f
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
67 additions
and
2 deletions
+67
-2
asset_bundle.dart
packages/flutter/lib/src/services/asset_bundle.dart
+67
-2
No files found.
packages/flutter/lib/src/services/asset_bundle.dart
View file @
9e7311fe
...
...
@@ -15,16 +15,51 @@ import 'image_decoder.dart';
import
'image_resource.dart'
;
import
'shell.dart'
;
/// A collection of resources used by the application.
///
/// Asset bundles contain resources, such as images and strings, that can be
/// used by an application. Access to these resources is asynchronous so that
/// they can be transparently loaded over a network (e.g., from a
/// [NetworkAssetBundle]) or from the local file system without blocking the
/// application's user interface.
///
/// Applications have a [rootBundle], which contains the resources that were
/// packaged with the application when it was built. To add resources to the
/// [rootBundle] for your application, add them to the `assets` section of your
/// `flutter.yaml` manifest.
///
/// Rather than accessing the [rootBundle] global static directly, consider
/// obtaining the [AssetBundle] for the current [BuildContext] using
/// [DefaultAssetBundle.of]. This layer of indirection lets ancestor widgets
/// substitute a different [AssetBundle] (e.g., for testing or localization) at
/// runtime rather than directly replying upon the [rootBundle] created at build
/// time. For convenience, the [WidgetsApp] or [MaterialApp] widget at the top
/// of the widget hierarchy configures the [DefaultAssetBundle] to be the
/// [rootBundle].
///
/// See also:
///
/// * [DefaultAssetBundle]
/// * [NetworkAssetBundle]
/// * [rootBundle]
abstract
class
AssetBundle
{
/// Retrieve an image from the asset bundle.
ImageResource
loadImage
(
String
key
);
/// Retrieve string from the asset bundle.
Future
<
String
>
loadString
(
String
key
);
/// Retrieve a binary resource from the asset bundle as a data stream.
Future
<
core
.
MojoDataPipeConsumer
>
load
(
String
key
);
@override
String
toString
()
=>
'
$runtimeType
@
$hashCode
()'
;
}
/// An [AssetBundle] that loads resources over the network.
class
NetworkAssetBundle
extends
AssetBundle
{
/// Creates an network asset bundle that resolves asset keys as URLs relative
/// to the given base URL.
NetworkAssetBundle
(
Uri
baseUrl
)
:
_baseUrl
=
baseUrl
;
final
Uri
_baseUrl
;
...
...
@@ -36,6 +71,9 @@ class NetworkAssetBundle extends AssetBundle {
return
await
http
.
readDataPipe
(
_urlFromKey
(
key
));
}
/// Retrieve an image from the asset bundle.
///
/// Images are cached in the [imageCache].
@override
ImageResource
loadImage
(
String
key
)
=>
imageCache
.
load
(
_urlFromKey
(
key
));
...
...
@@ -48,19 +86,24 @@ class NetworkAssetBundle extends AssetBundle {
String
toString
()
=>
'
$runtimeType
@
$hashCode
(
$_baseUrl
)'
;
}
/// An [AssetBundle] that adds a layer of caching to an asset bundle.
abstract
class
CachingAssetBundle
extends
AssetBundle
{
final
Map
<
String
,
ImageResource
>
imageResourceCache
=
final
Map
<
String
,
ImageResource
>
_
imageResourceCache
=
<
String
,
ImageResource
>{};
final
Map
<
String
,
Future
<
String
>>
_stringCache
=
<
String
,
Future
<
String
>>{};
/// Override to alter how images are retrieved from the underlying [AssetBundle].
///
/// For example, the resolution-aware asset bundle created by [AssetVendor]
/// overrides this function to fetch an image with the appropriate resolution.
Future
<
ImageInfo
>
fetchImage
(
String
key
)
async
{
return
new
ImageInfo
(
image:
await
decodeImageFromDataPipe
(
await
load
(
key
)));
}
@override
ImageResource
loadImage
(
String
key
)
{
return
imageResourceCache
.
putIfAbsent
(
key
,
()
{
return
_
imageResourceCache
.
putIfAbsent
(
key
,
()
{
return
new
ImageResource
(
fetchImage
(
key
));
});
}
...
...
@@ -77,9 +120,12 @@ abstract class CachingAssetBundle extends AssetBundle {
}
}
/// An [AssetBundle] that loads resources from a Mojo service.
class
MojoAssetBundle
extends
CachingAssetBundle
{
/// Creates an [AssetBundle] interface around the given [AssetBundleProxy] Mojo service.
MojoAssetBundle
(
this
.
_bundle
);
/// Retrieves the asset bundle located at the given URL, unpacks it, and provides it contents.
factory
MojoAssetBundle
.
fromNetwork
(
String
relativeUrl
)
{
AssetBundleProxy
bundle
=
new
AssetBundleProxy
.
unbound
();
_fetchAndUnpackBundle
(
relativeUrl
,
bundle
);
...
...
@@ -110,4 +156,23 @@ AssetBundle _initRootBundle() {
return
new
MojoAssetBundle
(
new
AssetBundleProxy
.
fromHandle
(
handle
));
}
/// The [AssetBundle] from which this application was loaded.
///
/// The [rootBundle] contains the resources that were packaged with the
/// application when it was built. To add resources to the [rootBundle] for your
/// application, add them to the `assets` section of your `flutter.yaml`
/// manifest.
///
/// Rather than using [rootBundle] directly, consider obtaining the
/// [AssetBundle] for the current [BuildContext] using [DefaultAssetBundle.of].
/// This layer of indirection lets ancestor widgets substitute a different
/// [AssetBundle] (e.g., for testing or localization) at runtime rather than
/// directly replying upon the [rootBundle] created at build time. For
/// convenience, the [WidgetsApp] or [MaterialApp] widget at the top of the
/// widget hierarchy configures the [DefaultAssetBundle] to be the [rootBundle].
///
/// See also:
///
/// * [DefaultAssetBundle]
/// * [NetworkAssetBundle]
final
AssetBundle
rootBundle
=
_initRootBundle
();
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