text
stringlengths
1
372
object for easy access to the main asset bundle.
it is possible to load assets directly using the
rootBundle global static from
package:flutter/services.dart.
however, it’s recommended to obtain the AssetBundle
for the current BuildContext using
DefaultAssetBundle, rather than the default
asset bundle that was built with the app; this
approach enables a parent widget to substitute a
different AssetBundle at run time,
which can be useful for localization or testing
scenarios.
typically, you’ll use DefaultAssetBundle.of()
to indirectly load an asset, for example a JSON file,
from the app’s runtime rootBundle.
outside of a widget context, or when a handle
to an AssetBundle is not available,
you can use rootBundle to directly load such assets.
for example:
<code_start>
import 'package:flutter/services.dart' show rootBundle;
Future<String> loadAsset() async {
return await rootBundle.loadString('assets/config.json');
}
<code_end>
<topic_end>
<topic_start>
loading images
to load an image, use the AssetImage
class in a widget’s build() method.
for example, your app can load the background
image from the asset declarations in the previous example:
<code_start>
return const image(image: AssetImage('assets/background.png'));
<code_end>
<topic_end>
<topic_start>
resolution-aware image assets
flutter can load resolution-appropriate images for
the current device pixel ratio.
AssetImage will map a logical requested
asset onto one that most closely matches the current
device pixel ratio.
for this mapping to work, assets should be arranged
according to a particular directory structure:
where m and n are numeric identifiers that correspond
to the nominal resolution of the images contained within.
in other words, they specify the device pixel ratio that
the images are intended for.
in this example, image.png is considered the main asset,
while mx/image.png and nx/image.png are considered to be
variants.
the main asset is assumed to correspond to a resolution of 1.0.
for example, consider the following asset layout for an
image named my_icon.png:
on devices with a device pixel ratio of 1.8, the asset
.../2.0x/my_icon.png is chosen.
for a device pixel ratio of 2.7, the asset
.../3.0x/my_icon.png is chosen.
if the width and height of the rendered image are not specified
on the image widget, the nominal resolution is used to scale
the asset so that it occupies the same amount of screen space
as the main asset would have, just with a higher resolution.
that is, if .../my_icon.png is 72px by 72px, then
.../3.0x/my_icon.png should be 216px by 216px;
but they both render into 72px by 72px (in logical pixels),
if width and height are not specified.
info note
device pixel ratio depends on MediaQueryData.size, which requires having either
MaterialApp or CupertinoApp as an ancestor of your AssetImage.
<topic_end>
<topic_start>
bundling of resolution-aware image assets
you only need to specify the main asset or its parent directory
in the assets section of pubspec.yaml.
flutter bundles the variants for you.
each entry should correspond to a real file, with the exception of
the main asset entry. if the main asset entry doesn’t correspond
to a real file, then the asset with the lowest resolution
is used as the fallback for devices with device pixel
ratios below that resolution. the entry should still
be included in the pubspec.yaml manifest, however.
anything using the default asset bundle inherits resolution
awareness when loading images. (if you work with some of the lower
level classes, like ImageStream or ImageCache,
you’ll also notice parameters related to scale.)
<topic_end>
<topic_start>
asset images in package dependencies
to load an image from a package dependency,
the package argument must be provided to AssetImage.
for instance, suppose your application depends on a package
called my_icons, which has the following directory structure:
to load the image, use:
<code_start>
return const AssetImage('icons/heart.png', package: 'my_icons');
<code_end>
assets used by the package itself should also be fetched
using the package argument as above.
<topic_end>