text
stringlengths
1
474
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>
<topic_start>Bundling of package assets
If the desired asset is specified in the pubspec.yaml
file of the package, it’s bundled automatically with the
application. In particular, assets used by the package
itself must be specified in its pubspec.yaml.A package can also choose to have assets in its lib/
folder that are not specified in its pubspec.yaml file.
In this case, for those images to be bundled,
the application has to specify which ones to include in its
pubspec.yaml. For instance, a package named fancy_backgrounds
could have the following files:To include, say, the first image, the pubspec.yaml of the
application should specify it in the assets section:The lib/ is implied,
so it should not be included in the asset path.If you are developing a package, to load an asset within the package, specify it in the pubspec.yaml of the package:To load the image within your package, use:<topic_end>
<topic_start>
Sharing assets with the underlying platform
Flutter assets are readily available to platform code
using the AssetManager on Android and NSBundle on iOS.<topic_end>
<topic_start>
Loading Flutter assets in Android
On Android the assets are available through the
AssetManager API. The lookup key used in,
for instance openFd, is obtained from
lookupKeyForAsset on PluginRegistry.Registrar or
getLookupKeyForAsset on FlutterView.
PluginRegistry.Registrar is available when developing a plugin
while FlutterView would be the choice when developing an
app including a platform view.As an example, suppose you have specified the following
in your pubspec.yamlThis reflects the following structure in your Flutter app.To access icons/heart.png from your Java plugin code,
do the following:<topic_end>
<topic_start>
Loading Flutter assets in iOS
On iOS the assets are available through the mainBundle.
The lookup key used in, for instance pathForResource:ofType:,
is obtained from lookupKeyForAsset or lookupKeyForAsset:fromPackage:
on FlutterPluginRegistrar, or lookupKeyForAsset: or
lookupKeyForAsset:fromPackage: on FlutterViewController.
FlutterPluginRegistrar is available when developing
a plugin while FlutterViewController would be the choice
when developing an app including a platform view.As an example, suppose you have the Flutter setting from above.To access icons/heart.png from your Objective-C plugin code you
would do the following:To access icons/heart.png from your Swift app you
would do the following:For a more complete example, see the implementation of the
Flutter video_player plugin on pub.dev.The ios_platform_images plugin on pub.dev wraps
up this logic in a convenient category. You fetch
an image as follows:Objective-C:Swift:<topic_end>
<topic_start>
Loading iOS images in Flutter
When implementing Flutter by
adding it to an existing iOS app,
you might have images hosted in iOS that you
want to use in Flutter. To accomplish
that, use the ios_platform_images plugin
available on pub.dev.<topic_end>
<topic_start>
Platform assets
There are other occasions to work with assets in the
platform projects directly. Below are two common cases
where assets are used before the Flutter framework is
loaded and running.<topic_end>
<topic_start>
Updating the app icon
Updating a Flutter application’s launch icon works
the same way as updating launch icons in native
Android or iOS applications.<topic_end>
<topic_start>Android
In your Flutter project’s root directory, navigate to
.../android/app/src/main/res. The various bitmap resource
folders such as mipmap-hdpi already contain placeholder
images named ic_launcher.png. Replace them with your
desired assets respecting the recommended icon size per
screen density as indicated by the Android Developer Guide.info Note
If you rename the .png files, you must also update the
corresponding name in your AndroidManifest.xml’s
<application> tag’s android:icon attribute.<topic_end>
<topic_start>iOS
In your Flutter project’s root directory,
navigate to .../ios/Runner. The
Assets.xcassets/AppIcon.appiconset directory already contains
placeholder images. Replace them with the appropriately
sized images as indicated by their filename as dictated by the
Apple Human Interface Guidelines.
Keep the original file names.<topic_end>
<topic_start>
Updating the launch screen