flutter实现图片拉伸点9图

在flutter中,如果相对一张图片进行中间部分拉伸或压缩,实现类似原生点9图的效果,

通过使用DecorationImagecenterSlice属性进行设置可以实现

Container(
      width: 100.w,
      height: 150.w,
      decoration: BoxDecoration(
          image: DecorationImage(
              image: AssetImage(
                   Assets.ItemBg,
              ),
               centerSlice: const Rect.fromLTRB(25, 25, 75, 125),
              fit: BoxFit.fill,
          ),
       ),
       child: Text("111"),
   )

会根据centerSlice对图片设置的对应中间部分进行拉伸,这里设置的是拉伸区域

注意centerSlice的设置需要满足条件,图片的不可拉伸部分需要小于容器大小
例如设置的centerSlice为Rect.fromLTRB(left, top, right, bottom),
图片实际尺寸为width*height,容器尺寸为containerW*containerH,
需满足

containerW > (width - (right - left) )
containerH > (height - (bottom - top) )

否则会出现如下错误

The following assertion was thrown during paint():
centerSlice was used with a BoxFit that does not guarantee that the image is fully visible.
'package:flutter/src/painting/decoration_image.dart':
Failed assertion: line 535 pos 12: 'sourceSize == inputSize'
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容