首页 > 解决方案 > 为什么不显示图像资产

问题描述

我想使用 Android Studio 在颤动中显示图像资产,但它没有显示在应用程序中,即使它占用空间

当我选择图像时,我使用了颤振检查器,它向我显示图像应该是白色方块。我已经在 pubsec.yaml 中添加了资产

图像资产代码

class CarsImage extends StatelessWidget{

  @override
  Widget build(BuildContext context) {
    AssetImage carAsset = AssetImage('ímages/buga.png');

    Image im = Image(image: carAsset,height: 250.0,width: 250.0,);


    return im;
  }
}



class Home extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return CarsImage()
    ;
  }

pubsec.yaml

flutter:

  # The following line ensures that the Material Icons font is
  # included with your application, so that you can use the icons in
  # the material Icons class.
  uses-material-design: true

  # To add assets to your application, add an assets section, like this:
  assets:
    - images/

我希望图像会出现。

标签: android-studioflutter

解决方案


如果您在项目中名为“assets”的文件夹中添加了图像,并且在该文件夹中创建了名为“images”的文件夹,请尝试pubspec.yaml按照以下方式在您的文件夹中定义它:

    - assets/images/buga.png

之后,您可以按以下方式使用资产中的图像:

Image.asset('assets/images/buga.png')

推荐阅读