Tuesday, December 4, 2018

Flutter : 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 does not matter. The actual directory used (assets in this case) does not matter

During a build, Flutter places assets into a special archive called the asset bundle, which apps can read from at runtime

Asset variants

The build process supports the notion of asset variants: different versions of an asset that might be displayed in different contexts. When an asset’s path is specified in the assets section of pubspec.yaml, the build process looks for any files with the same name in adjacent subdirectories. Such files are then included in the asset bundle along with the specified asset.

For example, if you have the following files in your application directory:

  .../pubspec.yaml
  .../graphics/my_icon.png
  .../graphics/background.png
  .../graphics/dark/background.png
  ...etc.

…and your pubspec.yaml file contains:

flutter:
  assets:
    - graphics/background.png

…then both graphics/background.png and graphics/dark/background.png will be included in your asset bundle. The former is considered the main asset, while the latter is considered a variant.

If on the other hand the graphics directory is specified:

flutter:
  assets:
    - graphics/

… then graphics/my_icon.png, graphics/background.png and graphics/dark/background.png will be included.

Flutter uses asset variants when choosing resolution appropriate images; see below. In the future, this mechanism may be extended to include variants for different locales or regions, reading directions, etc.

References:
https://flutter.io/docs/development/ui/assets-and-images

No comments:

Post a Comment