我需要在我的 flutter 应用程序中使用一些特定于密度的图像,但不幸的是我在加载它们时遇到了问题。
我根据官方文档创建了几个特定于密度的目录来存储图像https://flutter.dev/docs/development/ui/assets-and-images,编辑了 pubscpec.yaml 并编写了一些代码。
资产:
/assets
/images
/1.0x
/qr_logo.png
/1.5x
/qr_logo.png
/2.0x
/qr_logo.png
/3.0x
/qr_logo.png
/4.0x
/qr_logo.png
pubspec.yaml:
flutter:
uses-material-design: true
assets:
- assets/images/
- assets/strings/uk.json
- assets/strings/ru.json
代码:
GestureDetector(
child: Container(
width:120,
height: 40,
decoration: BoxDecoration(
color: Colors.black,
image: DecorationImage(
image:AssetImage('assets/images/qr_logo.png'),
fit:BoxFit.cover
),
)
),onTap:(){
print("CLICKED!");
})
这会导致: 解析图像编解码器时抛出以下断言: 无法加载资源:assets/images/qr_logo.png
抛出异常时,这是堆栈:
0 PlatformAssetBundle.load(包:flutter/src/services/asset_bundle.dart:221:7)
1 AssetBundleImageProvider._loadAsync (包:flutter/src/painting/image_provider.dart:464:44)
2 AssetBundleImageProvider.load(包:flutter/src/painting/image_provider.dart:449:14) ...
图片提供者:AssetImage(bundle: null, name: "assets/images/qr_logo.png") 图片键:AssetBundleImageKey(bundle: PlatformAssetBundle#5e64b(),名称:“assets/images/qr_logo.png”,比例:1.0)