Flutter Image图片显示

目录

参数详解

代码示例

效果图

完整代码


使用资源图片前必做两个步骤:

1、在根目录下创建子目录,子目录中创建2.0x和3.0x(也可以创建4.0x、5.0x... 但是2.0和3.0是必须的)目录,在对应目录中添加对应分辨率图片。(图1)

2、打开pubspec.yaml文件, 配置图片源。(图2)

                                        

                                       (图1)                                                                          (图2)

参数详解

属性说明
imageImage.asset:加载资源图片       不支持 热加载
Image.file:加载本地图片
Image.network:加载网络图片
Image.memory:加载Uint8List资源图片
semanticLabel图像的语义描述。
excludeFromSemantics默认false  是否启用图像的语义描述
width

宽度 一般结合 ClipOval 才能看到效果

height

高度 一般结合 ClipOval 才能看到效果

color背景色
colorBlendMode

使图像与背景色混合 模式

混合模式很多,这里就不多介绍了,具体看官网BlendMode

fitBoxFit.fill:全图显示,图片会被拉伸,并充满父容器。
BoxFit.contain:全图显示,显示原比例,可能会有空隙。
BoxFit.cover:显示可能拉伸,可能裁切,充满(充满容器不变形)。
BoxFit.fitWidth:宽度充满(横向充满),显示可能拉伸,可能裁切。
BoxFit.fitHeight :高度充满(竖向充满),显示可能拉伸,可能裁切。
BoxFit.scaleDown:效果和 contain 差不多,但是此属性不允许显示超过源图片大小,可小不可大。
alignment对齐方式
repeat

平铺

ImageRepeat.repeat : 横向和纵向都进行重复,直到铺满整个画布。
ImageRepeat.repeatX: 横向重复,纵向不重复。
ImageRepeat.repeatY:纵向重复,横向不重复。

centerSlice

设定拉伸部位,不能和fit一同使用

图片大于等于容器时 属性无效

matchTextDirection默认false  

官方翻译:是否在TextDirection的方向上绘制图像。

gaplessPlayback

默认false

官方翻译:当图像提供者发生变化时,是继续显示旧图像,默认不显示!

filterQuality

默认FilterQuality.low

官方翻译:图像过滤器的质量级别。(渲染模式的质量)

代码示例

child: Container(
        //设置容器大小
        width: 320.0, 
        height: 203.0, 
        decoration: BoxDecoration(
          //背景色
          color: Colors.yellow
        ),
        /*
        加载资源图片
        child: Image.asset('images/aa.jpg'),
        */

        /*
        加载网络图片
        child: Image.network(
          'https://raw.githubusercontent.com/think-ing/flutter_demo/master/images/ba.jpg',
        ),
        */
        
        /*
        图片混合蓝色
        child: Image.network(
          'https://raw.githubusercontent.com/think-ing/flutter_demo/master/images/ba.jpg',
          color: Colors.blue,
          colorBlendMode: BlendMode.colorDodge,
        ),
        */

        /*
        图片平铺
        child: Image.network(
          'https://raw.githubusercontent.com/think-ing/flutter_demo/master/images/bc.jpg',
          repeat: ImageRepeat.repeat,
        ),
        */

        /*
        设定拉伸部位
        child: Image.network(
          'https://raw.githubusercontent.com/think-ing/flutter_demo/master/images/bc.jpg',
          centerSlice:Rect.fromLTRB(10, 11, 12, 13),
        ),
        */
         
        // 圆形图片
        child: ClipOval(
              child:Image.network(
                'https://raw.githubusercontent.com/think-ing/flutter_demo/master/images/a.jpg',
                height: 100,
                width: 100,
                fit: BoxFit.cover,
              ),
            ),
      ), 

效果图

          

                 图片混合蓝色                                           图片平铺                                             设定拉伸部位

完整代码

查看完整代码

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

马志武

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值