Specifying assets
pubspec.yaml
flutter:
assets:
- assets/my_icon.png
- assets/background.png
flutter:
assets:
- assets/
Asset bundling
Asset variants
.../pubspec.yaml
.../graphics/my_icon.png
.../graphics/background.png
.../graphics/dark/background.png
...etc.
flutter:
assets:
- graphics/background.png
flutter:
assets:
- graphics/
Loading assets
AssetBudle
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.
import 'dart:async' show Future;
import 'package:flutter/services.dart' show rootBundle;
Future<String> loadAsset() async {
return await rootBundle.loadString('assets/config.json');
}
Loading images
Declaring resolution-aware image assets
.../image.png
.../Mx/image.png
.../Nx/image.png
...etc.
.../my_icon.png
.../2.0x/my_icon.png
.../3.0x/my_icon.png
Loading images
Widget build(BuildContext context) {
// ...
return DecoratedBox(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('graphics/background.png'),
// ...
),
// ...
),
);
// ...
}
Asset images in package in package dependencies
.../pubspec.yaml
.../icons/heart.png
.../icons/1.5x/heart.png
.../icons/2.0x/heart.png
...etc.
AssetImage('icons/heart.png', package: 'my_icons')
Bunding of package assets
.../lib/backgrounds/background1.png
.../lib/backgrounds/background2.png
.../lib/backgrounds/background3.png
flutter:
assets:
- packages/fancy_backgrounds/backgrounds/background1.png
Sharing assets with the underlying platform
Android
flutter:
assets:
- icons/heart.png
.../pubspec.yaml
.../icons/heart.png
...etc.
AssetManager assetManager = registrar.context().getAssets();
String key = registrar.lookupKeyForAsset("icons/heart.png");
AssetFileDescriptor fd = assetManager.openFd(key);
IOS
NSString* key = [registrar lookupKeyForAsset:@"icons/heart.png"];
NSString* path = [[NSBundle mainBundle] pathForResource:key ofType:nil];
Platform assets
Updating the app icon
Android
iOS
Updating the launch screen