资源文件的使用

资源文件的使用

一、Flutter 导入资源图片

Flutter 资源路径配置 : 资源路径在根目录中的 pubspec.yaml 配置文件中配置 ; 将 flutter 节点下的 assets 节点的注释打开 , 即删除前面的 # 注释符号 ; res

然后在 flutter 项目根目录创建 images 目录 , 将图片 test.png 拷贝到该 images 目录中 ; 并在 pubspec.yaml 配置文件的 assets 节点下配置 - images/test.png 信息 ;

1
2
3
4
5
6
7
8
9
# The following section is specific to Flutter.
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/test.png
之后就可以在 flutter 项目中使用该文件了 ;

二、Flutter 使用资源图片

Image 组件中使用资源图片 , 在其 image 字段使用 AssetImage 类型的图片即可 ; 代码示例 : 设置一个 200 x 200 大小的 Image 组件 , 显示 images/hunter.png 资源图片 ;

1
2
3
4
5
Image(
  width: 200,
  height: 200,
  image: AssetImage("images/test.png"),
)