简体   繁体   English

即使图像位于资产文件夹中,图像也不会显示在应用程序上

[英]image doesn't display on the app even though it was in assets folder

I added the image file on assets folder.我在资产文件夹中添加了图像文件。 but when I run the app, image doesn't display at all.但是当我运行应用程序时,图像根本不显示。 and there isn't any error message.并且没有任何错误信息。 I don't know the reason why I cannot see the picture I added.我不知道为什么我看不到我添加的图片。 (of course, it isn't the white picture. It isthe normal picture.) Please let me know the reason. (当然不是白图,是正常图。)请告诉我原因。

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Title'),
      ),
      body: Center(
        child: GestureDetector(
          onTap: (){
            Navigator.push(
                context,
                MaterialPageRoute(builder: (context) => HeroDetailPage()),
            );
          },
          child: Hero(
            tag: 'image',
            child: Image.asset(
                'assets/sample.jpg',
              width: 100,
              height: 100,
            ),
          ),
        ),
      ),
    );
  }
}

class HeroDetailPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Hero Detail'),
      ),
      body: Hero(
        tag: 'image',
        child: Image.asset('assets/sample.jpg'),
      ),
    );
  }
}

sample.jpg 在资产文件夹中

To add assets to your application, add an assets section, like this in your pubspec.yaml file:要将资产添加到您的应用程序,请在pubspec.yaml文件中添加资产部分,如下所示:

assets:
- assets/

Please check: https://flutter.dev/docs/development/ui/assets-and-images first.请先检查: https : //flutter.dev/docs/development/ui/assets-and-images

There you can find this:在那里你可以找到这个:

Flutter uses the pubspec.yaml file, located at the root of your project, to identify assets required by an app. Flutter 使用位于项目根目录的 pubspec.yaml 文件来识别应用程序所需的资产。

Here is an example:下面是一个例子:

flutter:
  assets:
    - assets/my_icon.png
    - assets/background.png

To include all assets under a directory, specify the directory name with the / character at the end:要包含目录下的所有资产,请在结尾处使用 / 字符指定目录名称:

flutter:
  assets:
    - directory/
    - directory/subdirectory/

First add your image file in pubspec.yml file首先在pubspec.yml文件中添加您的图像文件

assets:
    - assets/sample.jpg

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将图像复制到真实设备颤振的资产文件夹 - Can't copy image to assets folder on real device flutter 字符串格式化时从资产加载图像不起作用 - Loading image from assets doesn't work when string formatting 如何将资产文件夹中的图像添加到本地 HTML 文件并在 Flutter Z3E7D16622A52391BD7BF68CAE 中显示 HTML 文件 - How to add an image from assets folder to local HTML file and display the HTML file in Flutter WebView 即使在同一个小部件中,数据也不会立即加载到抽屉中 - data doesn't load instantly in drawer even though it's in same widget 如果请求的图像不在我的资产中,有没有办法显示图像? - Is there a way to display an image if the requested image is not in my assets? Flutter 如何在资产文件夹上共享图像? - Flutter how to share image on assets folder? 即使我添加了必要的代码,我的 Flutter 应用程序也无法保持 PortraitUp - My Flutter app can't stay portraitUp even though I have added the necessary codes 即使安装了包,它也没有显示在 dart 包文件夹中 - Package is not showing up in the dart packages folder even though it is installed 我正在尝试清理安装 flutter,即使此错误不是 go。 我该如何解决? - I am trying to clean install flutter and even though, This error doesn't go. How can I resolve it? 在浏览器的 assets 文件夹中显示本地存储的 html 文件 Flutter Web - Display locally stored html file in assets folder on browser in Flutter Web
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM