Flutter之Resource file configuration

Resource file configuration

In Flutter, the path of the resource needs to be configured in the pubspec.yaml file and then resources can be packaged for use.

Add image resource file

Add local image resources

flutter:
    assets:
        // Import all the resource files under the images folder
        - images/
        // Only import pci.png which is under the images folder.
        - images/pci.png

Use:

Image.asset("images/pic.png")

Add a image resource of plug-in

Add dependency plug-in

dependencies:
    flutter_gallery_assets: 0.1.6

Register the resources in the plug-in

flutter:
    assets:
        - packages/flutter_gallery_assets/places/india_chennai_flower_market.png

To load an image in a dependent package, you must supply the package parameter to AssetImage.

new AssetImage('places/india_chennai_flower_market.png', package: 'flutter_gallery_assets')

Resolution-related image resources

../image.png
../1.0x/image.png
../2.0x/image.png

Add font resources

flutter:
    fonts:
      // The name of font
      - family: Schyler
        fonts:
          // font resourcce file
          - asset: fonts/Schyler-Regular.ttf
          - asset: fonts/Schyler-Italic.ttf
            style: italic
      - family: Trajan Pro
        fonts:
          - asset: fonts/TrajanPro.ttf
          - asset: fonts/TrajanPro_Bold.ttf
            weight: 700
      - family: Raleway
        fonts:
          - asset: packages/flutter_gallery_assets/fonts/raleway/Raleway-Regular.ttf
          - asset: packages/flutter_gallery_assets/fonts/raleway/Raleway-Medium.ttf
            weight: 500
          - asset: packages/flutter_gallery_assets/fonts/raleway/Raleway-SemiBold.ttf
            weight: 600

Use:

TextStyle(
    fontFamily: 'Raleway',
    inherit: false,
    fontSize: 24.0,
    fontWeight: FontWeight.w500,
    color: Colors.white,
    textBaseline: TextBaseline.alphabetic,
  )

Loading static data

flutter:
  assets:
    - assets/config.json

Use:

DefaultAssetBundle.of(context).loadString("assets/config.json")

Or

Future<String> loadAsset() async {
  return await rootBundle.loadString('assets/config.json');
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容