在flutter开发中,当app项目内存吃紧时,页面中如果图片很多时需要及时清理,如果不能及时清理,会造成内存泄漏,导致app卡顿甚至强制退出或手机死机。
这里我使用extended_image封装了一个全局可用的widget,亲测ios和安卓好用。
首先在pubspec.yaml文件中配置插件并下载,执行pub get。
dependencies: extended_image: ^6.2.0
我使用的是6.2.0版本,基于extendedimage我对network网络图片组件做了如下封装,内有注释与备注,就不做过多的解释了,完全都能看的懂!
class judgenetworkimage extends statefulwidget { const judgenetworkimage( this.url, { key? key, this.width, this.height, this.isavatar = false, this.fit = boxfit.cover, this.borderradius, this.shape, this.borderwidth = 0.0, this.bordercolor, this.opacity = 1.0, this.scale = 1.0, this.maxbytes, this.filterquality = filterquality.low, this.repeat = imagerepeat.norepeat, this.replaceimgplayback = false, this.enablememorycache = true, this.clearmemorycacheiffailed = true, this.clearmemorycachewhendispose = true, this.cacherawdata = false, this.placeholder, this.errorwidget, this.loadingwidget, this.defaultimagepath, }) : super(key: key); final string url; // 图片url final double? width; // 宽度 final double? height; // 高度 final bool isavatar; // 是否为头像 final boxfit fit; // 适配方式 final borderradius? borderradius; // 圆角 final boxshape? shape; // 形状(圆形/矩形) final double borderwidth; // 边框宽度 final color? bordercolor; // 边框颜色 final double opacity; // 透明度 final double scale; // 缩放比例 final int? maxbytes; // 控制图片加载时的字节大小限制 final filterquality filterquality; // 缩放质量 final imagerepeat repeat; // 重复方式 final bool replaceimgplayback; // 更换图片时是否保留旧图 final bool enablememorycache; // 启用内存缓存 final bool clearmemorycacheiffailed; // 加载失败时清除缓存 final bool clearmemorycachewhendispose; // 组件销毁时清除缓存 final bool cacherawdata; // 缓存原始数据 final widget? placeholder; // 加载中占位图 final widget? errorwidget; // 加载失败组件 final widget? loadingwidget; // 加载中组件 final string? defaultimagepath; // 默认图片路径 @override state<judgenetworkimage> createstate() => _judgenetworkimagestate(); } class _judgenetworkimagestate extends state<judgenetworkimage> { late string _url; @override void initstate() { super.initstate(); _url = widget.url; } @override void dispose() { super.dispose(); if (widget.clearmemorycachewhendispose) { // 主动清除图片缓存 if (_url.isnotempty && _url != (widget.defaultimagepath ?? '')) { final provider = extendednetworkimageprovider(_url); provider.evict(); } } } @override widget build(buildcontext context) { widget child = container( width: widget.width ?? double.maxfinite, height: widget.height ?? double.maxfinite, decoration: boxdecoration( // 边框部分 border: widget.borderwidth > 0 ? border.all( color: widget.bordercolor ?? colors.transparent, width: widget.borderwidth, ) : null, ), child: extendedimage.network( _url, width: widget.width, height: widget.height, fit: widget.fit, scale: widget.scale, filterquality: widget.filterquality, repeat: widget.repeat, gaplessplayback: widget.replaceimgplayback, enablememorycache: widget.enablememorycache, clearmemorycacheiffailed: widget.clearmemorycacheiffailed, clearmemorycachewhendispose: widget.clearmemorycachewhendispose, cacherawdata: widget.cacherawdata, maxbytes: widget.maxbytes, // 应用透时度 color: widget.opacity < 1.0 ? colors.white.withopacity(widget.opacity) : null, colorblendmode: widget.opacity < 1.0 ? blendmode.srcin : null, clipbehavior: clip.none, // 加载状态回调 loadstatechanged: (state) { return _handleloadstate(state); }, ), ); if (widget.shape == boxshape.circle) { return clipoval( child: child, ); } else if (widget.borderradius != null) { return cliprrect( borderradius: widget.borderradius, child: child, ); } else { return child; } } /// 处理加载状态 widget _handleloadstate(extendedimagestate state) { switch (state.extendedimageloadstate) { case loadstate.loading: // 加载中显示指示器 return sizedbox( width: widget.width != null ? widget.width! * 0.4 : 42.rpx, child: center( child: circularprogressindicator( valuecolor: const alwaysstoppedanimation<color>(appcolors.success), strokewidth: 4.rpx, ), ), ); case loadstate.completed: return state.completedwidget; case loadstate.failed: // 加载失败 - 显示默认图片并清除缓存 _clearcacheonerror(state); return container( color: const color(0xfff2f2f2), alignment: alignment.center, child: widget.isavatar ? image.asset( '${appglobal.imgurl}man_presuppose.png', fit: boxfit.fill, ) : image.asset( '${appglobal.imgurl}not_pic.png', width: widget.width != null ? widget.width! * 0.6 : 64.rpx, fit: boxfit.fitwidth, ), ); } } void _clearcacheonerror(extendedimagestate state) { if (widget.clearmemorycacheiffailed && mounted) { // 获取图片提供者并清除缓存 if (state.imageprovider is extendednetworkimageprovider) { final provider = state.imageprovider as extendednetworkimageprovider; provider.evict(); } } } }
以上就是一个完整的网络图片widget的封装,对性能提升有相当大的帮助!
到此这篇关于基于flutter开发一个图片缓存清理插件的文章就介绍到这了,更多相关flutter图片缓存清理内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论