【问题标题】:BoxDecoration: DecorationImage Fullscreen background imageBoxDecoration:DecorationImage 全屏背景图片
【发布时间】:2017-10-07 20:56:53
【问题描述】:

根据Flutter docs,我正在尝试使用 DecoratedBox 加载全屏图像作为容器的背景图像。

我的 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)

显然捆绑包没有正确解析。有谁知道我在这里到底做错了什么?

【问题讨论】:

    标签: dart flutter


    【解决方案1】:

    它对我有用。需要仔细检查的几件事:

    • 热重新加载/重新启动不会更改资产,因此请确保您正在进行常规构建。
    • 尝试从设备上卸载应用并进行干净的构建,有时构建系统很难替换旧安装(例如,如果您的调试密钥发生更改)
    • 确保实际资产以及对它的所有引用都指向 assets/background.png 而不是 images/background.png(默认的 pubspec.yaml 建议将图像放入名为 images 的文件夹中,但它不会'只要一切都匹配就无关紧要)。
    • 尝试使用AssetImage 而不是ExactAssetImage
    • 确保 Container 要么有一个子容器,要么位于它的父级将为其指定大小的位置(例如,Stack 可以,但如果有,Column 会导致它为 0x0没有内在大小或子级)

    【讨论】:

    • 从控制台运行“flutter build clean”并从 intellij 运行 main.dart 解决了这个问题。当我运行项目时,intellij 似乎没有重建/更新构建文件夹。谢谢!
    • 您是在 iOS 还是 Android 上运行?如果是 iOS,它可能与 github.com/flutter/flutter/issues/9846 是相同的问题
    • 在安卓设备上运行。
    • 我将在哪里获得 AssetImage 文件夹?我已将所有图像添加到可绘制文件夹中,但我不知道如何在屏幕上显示。
    猜你喜欢
    • 1970-01-01
    • 2021-09-29
    • 2019-12-30
    • 2012-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-29
    相关资源
    最近更新 更多