text stringlengths 1 474 |
|---|
information, so you only need it if you’re writing your own controls from |
scratch. You can use addHighlightModeListener callback to listen for changes |
in the highlight mode. |
<topic_end> |
<topic_start>Adding assets and images |
Flutter apps can include both code and assets |
(sometimes called resources). An asset is a file |
that is bundled and deployed with your app, |
and is accessible at runtime. Common types of assets include |
static data (for example, JSON files), |
configuration files, icons, and images |
(JPEG, WebP, GIF, animated WebP/GIF, PNG, BMP, and WBMP).<topic_end> |
<topic_start> |
Specifying assets |
Flutter uses the pubspec.yaml file, |
located at the root of your project, |
to identify assets required by an app.Here is an example:To include all assets under a directory, |
specify the directory name with the / character at the end:info Note |
Only files located directly in the directory are included. |
Resolution-aware asset image variants are the only exception. |
To add files located in subdirectories, create an entry per directory.<topic_end> |
<topic_start> |
Asset bundling |
The assets subsection of the flutter section |
specifies files that should be included with the app. |
Each asset is identified by an explicit path |
(relative to the pubspec.yaml file) where the asset |
file is located. The order in which the assets are |
declared doesn’t matter. The actual directory name used |
(assets in first example or directory in the above |
example) doesn’t matter.During a build, Flutter places assets into a special |
archive called the asset bundle that apps read |
from at runtime.<topic_end> |
<topic_start> |
Loading assets |
Your app can access its assets through an |
AssetBundle object.The two main methods on an asset bundle allow you to load a |
string/text asset (loadString()) or an image/binary asset (load()) |
out of the bundle, given a logical key. The logical key maps to the path |
to the asset specified in the pubspec.yaml file at build time.<topic_end> |
<topic_start> |
Loading text assets |
Each Flutter app has a rootBundle |
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. |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.