BoxDecoration:DecorationImage全屏背景图片
问题内容:
根据Flutter文档,我正在尝试使用DecoratedBox加载全屏图像作为Container的背景图像。
我的pubspec.yaml包含嵌入式资产的相关定义:
flutter:
uses-material-design: true
assets:
- assets/background.png
然后widget.dart尝试按规定填充新Container的背景:
@override
Widget build(BuildContext context) {
return new Container(
decoration: new BoxDecoration(
color: Colors.purple,
image : new DecorationImage(
image: new ExactAssetImage('assets/background.png'),
fit: BoxFit.cover,
),
),
),
}
但我收到以下错误:
Unable to load asset: assets/background.png
Image provider: ExactAssetImage(name: "assets/background.png", scale: 1.0, bundle: null)
显然,捆绑包无法正确解析。有人知道我在这里到底在做什么错吗?
问题答案:
这个对我有用。需要仔细检查的几件事:
- 热装/重启不会改变资产,因此请确保您要进行常规构建。
- 尝试从设备上卸载应用程序并执行干净的构建,有时构建系统很难替换旧的安装(例如,如果调试密钥更改)
- 确保实际资产及其所有引用都指向assets / background.png,而不是images / background.png(默认的pubspec.yaml建议将图像放入名为images的文件夹中,但这没关系只要一切都匹配)。
- 尝试使用
AssetImage
代替ExactAssetImage
- 确保容器有一个子容器,或者位于其父容器将为其指定大小的位置(例如,a
Stack
可以,但是Column
如果没有内在大小或子元素,则a 将导致其为0x0)