Flutter之Resource file configurat

2021-05-31  本文已影响0人  小言聊编程

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');
}
上一篇 下一篇

猜你喜欢

热点阅读