七、assets资源图片

一、指定assets

Flutter也有asset这一概念,asset是打包到程序安装包中的,可在运行时访问。常见的asset类型包括静态数据(例如JSON文件)、配置文件、图标和图片(JPEG,WebP,GIF,动画WebP / GIF,PNG,BMP和WBMP)。

使用asset,需要先让asset被识别,在项目根目录中的pubspec.yaml中定义

flutter:
  assets:
    - images/user/login.png
    - images/user/no_login.png
    - images/swiper/photo1.jpg
    - images/swiper/photo2.jpg
    - images/record/record_start.png
    - images/record/record_end.png
    - assets/test.json


二、加载文本

DefaultAssetBundle.of(context).loadString("assets/test.json")


三、加载图片

image: DecorationImage(
  fit: BoxFit.fitWidth,
  image: AssetImage("images/user/splash.jpg"),
)
3.1 加载不同分辨率的图片

假设主要资源对应的分辨率为1.0,比如是72px72px,如果还有144px144px和216px216px的图片,那么可以在images文件夹下创建两个文件夹:2.0x和3.0x,将对应分辨率的图片拷贝进去,2.0x对应的是144px144px,3.0x对应的是216px*216px。

…images/login.png
…images/2.0x/login.png
…images/3.0x/login.png

在pubspec.yaml配置对应图片资源:

flutter:
  assets:
    - images/login.png

Flutter可以为当前设备加载适合其分辨率的图像,如果在设备像素比率为1.8会选择…images/2.0x/decode.png ,如果设备像素比率为2.8,会选择…images/3.0x/decode.png,设备选择的是相近的图片资源。

3.2 加载依赖包中的图片
AssetImage('icons/phone.png', package: 'best_icons')


参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值