BoxDecoration:DecorationImage全屏背景图片 [英] BoxDecoration: DecorationImage Fullscreen background image
本文介绍了BoxDecoration:DecorationImage全屏背景图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
根据 Flutter文档,我正在尝试使用DecoratedBox加载全屏图像作为容器的背景图像.
As per the Flutter docs I'm trying to use the DecoratedBox to load a fullscreen image as the background image for a Container.
我的pubspec.yaml包含嵌入式资产的相关定义:
my pubspec.yaml contains the relevant definition for an embedded asset:
flutter:
uses-material-design: true
assets:
- assets/background.png
和widget.dart尝试按规定填充新Container的背景:
and the widget.dart tries to fill the background of a new Container as prescribed:
@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)
很显然,捆绑软件无法正确解析.有人知道我在这里到底在做什么错吗?
Clearly the bundle is not resolving correctly. Does anyone have an idea of what exactly I'm doing wrong here?
推荐答案
它对我有用.需要仔细检查的几件事:
It works for me. A few things to double check:
- 热重装/重启不会更改资产,因此请确保您要进行常规构建.
- 尝试从设备上卸载应用并执行干净的构建,有时构建系统很难替换旧的安装(例如,如果调试密钥更改了)
- 确保实际资产及其所有引用都指向资产/background.png,而不是images/background.png(默认的pubspec.yaml建议将图像放入名为images的文件夹中,但不会只要一切都匹配就可以了.
- 尝试使用
AssetImage
代替ExactAssetImage
- 确保容器有一个孩子或位于其父级将为其指定大小的位置(例如
Stack
可以,但是Column
会使它为0x0)没有内在的大小或孩子)
- Hot reloading/restarting won't change assets, so make sure you're doing a regular build.
- Try uninstalling the app from the device and doing a clean build, sometimes the build system has a hard time replacing the old install (e.g. if your debug key changes)
- Make sure that the actual asset as well as all the references to it are pointing to assets/background.png and not images/background.png (the default pubspec.yaml suggests putting images into a folder called images, but it doesn't matter as long as everything matches).
- Try using
AssetImage
instead ofExactAssetImage
- Make sure the Container either has a child or is in a place where it will be given a size by its parent (e.g. a
Stack
would be fine, but aColumn
would cause it to be 0x0 if it has no intrinsic size or child)
这篇关于BoxDecoration:DecorationImage全屏背景图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文