加入图片的几种方式
- Image.asset:加载资源图片,就是加载项目资源目录中的图片,加入图片后会增大打包的包体体积,用的是相对路径。
- Image.network:网络资源图片,意思就是你需要加入一段http://xxxx.xxx的这样的网络路径地址。
- Image.file:加载本地图片,就是加载本地文件中的图片,这个是一个绝对路径,跟包体无关。
- Image.memory: 加载Uint8List资源图片
1.Image.asset方法:
首先在项目跟目录新建文件夹assets/images,在images文件夹下添加一张图片,然后在pubspec.yaml文件中配置asset资源,在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
assets:
- assets/images/你的图片名
配置完成,package get
然后,在代码中使用:
body: Center(
child: Container(
child: Image.asset(
'assets/images/ic_launcher72.png',
width: 200.0,
height: 200.0,
scale: 1.0,
color: Colors.black,
),
width: 400.0,