Skip to content

Commit b90cc87

Browse files
committed
Add more documentation on asset management
1 parent b35226d commit b90cc87

File tree

3 files changed

+116
-71
lines changed

3 files changed

+116
-71
lines changed

assets-and-images.md

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
---
2+
layout: page
3+
title: Assets and Images
4+
permalink: /assets-and-images/
5+
---
6+
7+
Flutter applications include both code and assets (sometimes called resources).
8+
Assets can include static data, configuration files, or anything else an
9+
application needs to function; in particular, assets include icons and other
10+
images displayed in the UI.
11+
12+
## Specifying assets
13+
14+
The flutter build process uses the `flutter.yaml` file to identify assets
15+
required by an application, as shown in the [Tutorial](/tutorial/). There are
16+
two sections that specify assets: the general `assets` section and the
17+
`material-design-icons` section, which includes any
18+
[Material Design icons](https://design.google.com/icons/) the application uses.
19+
Both kinds of assets are included in the Flutter application's _asset bundle_,
20+
which is a special archive that Flutter applications can read from at runtime.
21+
22+
### Asset variants
23+
24+
The build process supports the notion of asset variants: different versions of
25+
an asset that might be displayed in different contexts. When an asset's path is
26+
specified in the `assets` section of `flutter.yaml`, the build process looks for
27+
any files with the same name in subdirectories next to the main asset. Such
28+
files are then included in the asset bundle along with the main asset.
29+
30+
The key use of asset variants is in choosing resolution appropriate images; see
31+
below. In the future, this mechanism may be extended to include variants for
32+
different locales or regions, reading directions, etc.
33+
34+
## Working with assets
35+
36+
Your application can access its assets through the
37+
[AssetBundle](http://docs.flutter.io/flutter/services/AssetBundle-class.html)
38+
interface. The two main methods on an asset bundle allow you to load a string
39+
or an image out of the bundle, given a logical key. The logical key maps to
40+
the path to the asset specified in the `flutter.yaml` file at build time. The
41+
application's asset bundle is accessible through the
42+
[DefaultAssetBundle](http://docs.flutter.io/flutter/widgets/DefaultAssetBundle-class.html)
43+
widget.
44+
45+
As a shortcut, the
46+
[AssetImage](http://docs.flutter.io/flutter/widgets/AssetImage-class.html)
47+
widget provides a simple way to display an image in the UI. Images may also be
48+
specified as the background of a
49+
[BoxDecoration](http://docs.flutter.io/flutter/painting/BoxDecoration-class.html).
50+
51+
## Resolution awareness
52+
53+
Flutter can load resolution-appropriate images for the current device
54+
pixel ratio.
55+
56+
[AssetImage](http://docs.flutter.io/flutter/widgets/AssetImage-class.html)
57+
understands how to map a logical requested asset onto one that most
58+
closely matches the current device pixel ratio. In order for this mapping to
59+
work, assets should be arranged according to a particular directory structure:
60+
61+
.../image.png
62+
.../Mx/image.png
63+
.../Nx/image.png
64+
...etc.
65+
66+
Where M and N are numeric identifiers that correspond to the nominal resolution
67+
of the images contained within. The main asset is assumed to correspond to a
68+
resolution of 1.0. For example, consider the following asset layout for an
69+
image named `my_icon.png`:
70+
71+
.../my_icon.png
72+
.../2.0x/my_icon.png
73+
.../3.0x/my_icon.png
74+
75+
On devices with a device pixel ratio of 1.8, the asset `.../2.0x/my_icon.png`
76+
would be chosen. For a device pixel ratio of 2.7, the asset
77+
`.../3.0x/my_icon.png` would be chosen.
78+
79+
If the width and height of the rendered image are not specified, the nominal
80+
resolution is used to scale the asset so that it will occupy the same amount
81+
of screen space as the main asset would have, just with a higher resolution.
82+
That is, if `.../my_icon.png` is 72px by 72px, then `.../3.0x/my_icon.png`
83+
should be 216px by 216px; but they both will render into 72px by 72px
84+
(in logical pixels) if width and height are not specified.
85+
86+
The way this works is through an object called
87+
[AssetVendor](http://docs.flutter.io/flutter/widgets/AssetVendor-class.html)
88+
established at the top of the build tree. AssetVendor replaces the default asset
89+
bundle, so anything using the default asset bundle will inherit resolution
90+
awareness when loading images. (If you work with some of the lower level
91+
classes, like
92+
[ImageResource](http://docs.flutter.io/flutter/services/ImageResource-class.html)
93+
or
94+
[ImageCache](http://docs.flutter.io/flutter/services/ImageCache-class.html),
95+
you'll also notice parameters related to scale.)
96+
97+
Some caveats:
98+
99+
* If you're not using
100+
[MaterialApp](http://docs.flutter.io/flutter/material/MaterialApp-class.html)
101+
in your application, and you want to use resolution awareness, you'll need to
102+
establish your own AssetVendor in your build logic. (This may change, please see
103+
[this issue](https://github.com/flutter/flutter/issues/1346).)
104+
* If you want establish a your own
105+
[MediaQuery](http://docs.flutter.io/flutter/widgets/MediaQuery-class.html) or
106+
[DefaultAssetBundle](http://docs.flutter.io/flutter/widgets/DefaultAssetBundle-class.html)
107+
below the root of the widget hierarchy, the root-level AssetVendor won't be
108+
aware of the change. If you want resolution awareness with the new MediaQuery
109+
or DefaultAssetBundle you specify, you'll need to create an AssetVendor at
110+
that point in the tree as well.
111+
112+
You can see an example
113+
([examples/widgets.dart](https://github.com/flutter/flutter/tree/master/examples/widgets))
114+
from the flutter repo.
115+
Run `flutter start -t resolution_awareness.dart` to see it in action.

index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ to install Flutter and run your first app on Android and iOS.
3333
- [Animations in Flutter](animations)
3434
- [Architecture diagram](https://docs.google.com/presentation/d/1cw7A4HbvM_Abv320rVgPVGiUP2msVs7tfGbkgdrTy0I/edit?usp=sharing)
3535
- [Upgrading Flutter](upgrading)
36-
- [Resolution-aware images](/resolution-aware-images)
36+
- [Assets and images](assets-and-images)
3737

3838
## See also
3939

resolution-aware-images.md

Lines changed: 0 additions & 70 deletions
This file was deleted.

0 commit comments

Comments
 (0)