Flutter加载图片/资源

Flutter 应用程序包含代码和 assets(也为资源)。资源是被打包到应用程序安装包中,可以在运行时访问的一种文件。常见的资源类型包括静态数据(例如 JSON 文件),配置文件,图标和图片(JPEG,WebP,GIF,动画 WebP / GIF,PNG,BMP 和 WBMP)。

加载本地图片

新建资源文件夹assets,将图片加入到文件夹中

新建资源文件夹.png

声明分辨率相关的图片assets

官方文档.png

大坑提示:若想Android Studio自动识别分辨率相关图片,必须给定1.0x图片,否则,需指定具体路径,如:assets/public/3.0x/xxx.png

插件推荐:flutter_add_image,可自动通过3x图片生成1x,2x图片。

pubspec.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:
    - assets/public/
    - assets/public/2.0x/
    - assets/public/3.0x/

代码调用

Image.asset('assets/public/xxx.png')
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容