Flutter的资源类型
Flutter可以添加代码以及assets到APP中。而每个Asset都是被打包在发布的APP中的,并且在APP运行时可以访问这些资源。
通用的Asset类型包括:
- JSON文件
 - 配置文件
 - 图标
 - 位图(JPEG、Webp、Gif、Animated Gif/Webp、PNG)
 
指定Asset
Flutter使用项目根目录下的pubspec.yaml文件来指定APP所需要的资源。
- 单个文件指定:
 
flutter:
  assets:
    - assets/my_icon.png
    - assets/background.png
- 文件夹指定:
通过指定目录名+/字符即可,只有在该目录下的所有文件可以被包括,如果该目录还有子目录的话,则需要添加一个新的Entry。 
flutter:
  assets:
    - assets/
    - assets/image/
Asset Variants
构建系统支持Asset Variants的概念:
在不同的环境下,需要显示不同版本的资源。
当一个资源的路径在pubspec.yaml文件的assets Section中指定的时候,构建系统就会在相邻的子目录中查找相同的名称的资源文件。而查找到的这些文件也会被打到Asset Bundle中。
例如:有一个background.png文件,在日夜间都需要使用,graphics中存放日间资源,而dark中存放夜间资源。
  .../pubspec.yaml
  .../graphics/my_icon.png
  .../graphics/background.png
  .../graphics/dark/background.png
  ...etc.
而在pubspec.yaml文件中,将background.png添加到assets的Section中。
flutter:
  assets:
    -  graphics/background.png
最终,在打包的时候会把.../graphics/background.png和.../graphics/dark/background.png都打到Bundle中。而前一个被认为是Main Bundle,而后一个则认为是Variant Bundle。
而如果使用:
flutter:
  assets:
    - graphics/
则myicon.png,background.png,/dark/background.png也都会打到Bundle中。
Flutter目前使用Asset Variant来解决图片适配的问题,而未来这种机制也会应用在不同的语言等其他地方。
加载Assets
APP可以通过AssetBundle对象来访问资源。
- 加载String/Text:通过
loadString方法 - 加载图片/二进制文件:通过
load方法 
而在Build的阶段,逻辑Key会根据pubspec.yaml文件中的路径来进行映射。
每个Flutter App都有一个rootBundle对象来方便的访问主资源Bundle。可以通过package:flutter/services.dard中的全局静态变量rootBundle来直接访问资源。不过还是推荐使用当前的BuildContext来获取DefaultAssetBundle,通过调用DefaultAssetBundle.of(context)来获取。
而当没有Context的Widget,则需要通过rootBundle来获取。
import 'dart:async' show Future;
import 'package:flutter/services.dart' show rootBundle;
Future<String> loadAsset() async {
  return await rootBundle.loadString('assets/config.json');
}
图片加载的适配
Flutter会根据当前设备的设备像素比(device Pixel Ratio)来选择图片。
AssetImage知道如何映射到最相近的设备像素比的图片,为了让Mapping能够更好的工作,Assets应该有这种结构:
  .../my_icon.png
  .../2.0x/my_icon.png
  .../3.0x/my_icon.png
默认主资源Bundle中是1.0x的图片。
如果Image控件的宽高都没有指定的话,通常的解决方案是进行资源压缩,然后和主资源Bundle中的图占据相同的像素空间。
加载图片
在Widget的build函数中使用AssetImage类来加载图片。
Widget build(BuildContext context) {
  // ...
  return DecoratedBox(
    decoration: BoxDecoration(
      image: DecorationImage(
        image: AssetImage('graphics/background.png'),
        // ...
      ),
      // ...
    ),
  );
  // ...
}
与平台共享资源
Flutter的Asset资源也可以与Android/Ios共享。例如,flutter中有heart.png这张图
flutter:
  assets:
    - icons/heart.png
Android:
Android上通过AssetManager来获取。
- 通过
PluginRegistry.Registrar的lookupKeyForAsset来获取文件路径 - 通过
FlutterView的getLookupKeyForAsset来获取文件路径 - 通过
AssetManager的openFd来得到文件描述符 
AssetManager assetManager = registrar.context().getAssets();
String key = registrar.lookupKeyForAsset("icons/heart.png");
AssetFileDescriptor fd = assetManager.openFd(key);
IOS:
IOS上通过NSBundle来获取。
- 通过
FlutterPluginRegistrar的lookupKeyForAsset来获取文件路径 - 通过
FlutterViewController.FlutterPluginRegistrar同样也可以获取文件路径 - 通过
NSBundle的pathForResource来获取文件路径 
NSString* key = [registrar lookupKeyForAsset:@"icons/heart.png"];
NSString* path = [[NSBundle mainBundle] pathForResource:key ofType:nil];
资料
Adding assets and images










