当前位置: 首页 > news >正文

Flutter自定义通用防抖的实现

在前端项目开发中,点击事件的防抖是一个永远无法错开的点,特别是针对一些复杂的业务场景,如果不做好防抖操作,就会导致页面或功能触发多次,引发异常或闪退。

在Flutter中可以通过扩展函数的特性 对Function增加全局扩展函数,实现防抖效果。

具体如下:

extension FunctionExt on Function {VoidCallback throttle() {return FunctionProxy(this).throttle;}// 立即执行目标操作,同时给出一个延迟的时间,// 在该时间范围内如果再次触发了事件,该次事件会被忽略,直到超过该时间范围后触发事件才会被处理。VoidCallback throttleWithTimeout({int? timeout}) {return FunctionProxy(this, timeout: timeout).throttleWithTimeout;}// 在触发事件时,不立即执行目标操作,而是给出一个延迟的时间,// 在该时间范围内如果再次触发了事件,则重置延迟时间,直到延迟时间结束才会执行目标操作。VoidCallback debounce({int? timeout}) {return FunctionProxy(this, timeout: timeout).debounce;}
}class FunctionProxy {static final Map<String, bool> _funcThrottle = {};static final Map<String, Timer> _funcDebounce = {};final Function? target;final int timeout;FunctionProxy(this.target, {int? timeout}) : timeout = timeout ?? 500;// 节流(默认延迟)void throttle() async {String key = hashCode.toString();bool enable = _funcThrottle[key] ?? true;if (enable) {_funcThrottle[key] = false;try {await target?.call();} catch (e) {rethrow;} finally {_funcThrottle.remove(key);}}}// 节流(自定义延迟)void throttleWithTimeout() {String key = hashCode.toString();bool enable = _funcThrottle[key] ?? true;if (enable) {_funcThrottle[key] = false;Timer(Duration(milliseconds: timeout), () {_funcThrottle.remove(key);});target?.call();}}//延迟顺序执行的防抖void debounce() {String key = hashCode.toString();Timer? timer = _funcDebounce[key];timer?.cancel();timer = Timer(Duration(milliseconds: timeout), () {Timer? t = _funcDebounce.remove(key);t?.cancel();target?.call();});_funcDebounce[key] = timer;}
}

在使用的地方

              onBackPressed: () {}.throttleWithTimeout(timeout: 500)

这种方案只是提供了一种防抖的实现,当然也可以自定义通用的Button,通过点击事件触发的时间自行判断处理。

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • c语言-数组(3)
  • onlyoffice用nginx反向代理
  • 《零散知识点 · SpringBoot Starter》
  • 花几千上万学习Java,真没必要!(三十七)
  • 不得不安利的程序员开发神器,太赞了!!
  • 2、Flink 在 DataStream 和 Table 之间进行转换
  • SQL进阶技巧:用户浏览日志分析【访问量、活跃用户、新增用户、留存用户、流失用户、沉默用户、回流用户】
  • 【初阶数据结构篇】单链表的实现(赋源码)
  • 正在等待缓存锁:无法获得锁 /var/lib/dpkg/lock。锁正由进程 36430(dpkg)持有。遇到这个问题怎么解决
  • ipvlan: operation not supported 导致的POD不断重启
  • 【Spring】——Spring概述、IOC、IOC创建对象的方式、Spring配置、依赖注入(DI)以及自动装配知识
  • 测桃花运(算姻缘)的网站系统源码
  • 全平台|全球最受欢迎的20个黑客工具 小白也可以学会 网络安全必备
  • DjangoRF-10-过滤-django-filter
  • 支付革新:探索WebKit中的Payment Request API
  • 【159天】尚学堂高琪Java300集视频精华笔记(128)
  • 0基础学习移动端适配
  • 230. Kth Smallest Element in a BST
  • Android开源项目规范总结
  • CSS实用技巧
  • Docker 笔记(1):介绍、镜像、容器及其基本操作
  • electron原来这么简单----打包你的react、VUE桌面应用程序
  • ES6系统学习----从Apollo Client看解构赋值
  • Nginx 通过 Lua + Redis 实现动态封禁 IP
  • Otto开发初探——微服务依赖管理新利器
  • React中的“虫洞”——Context
  • SQL 难点解决:记录的引用
  • 搞机器学习要哪些技能
  • 聊聊spring cloud的LoadBalancerAutoConfiguration
  • 前端每日实战:70# 视频演示如何用纯 CSS 创作一只徘徊的果冻怪兽
  • 深度学习中的信息论知识详解
  • 用 Swift 编写面向协议的视图
  • 再谈express与koa的对比
  • 怎么将电脑中的声音录制成WAV格式
  • mysql 慢查询分析工具:pt-query-digest 在mac 上的安装使用 ...
  • ​​​​​​​STM32通过SPI硬件读写W25Q64
  • ​ssh免密码登录设置及问题总结
  • ‌U盘闪一下就没了?‌如何有效恢复数据
  • (一)appium-desktop定位元素原理
  • (一)搭建springboot+vue前后端分离项目--前端vue搭建
  • (转)shell中括号的特殊用法 linux if多条件判断
  • ..回顾17,展望18
  • ./mysql.server: 没有那个文件或目录_Linux下安装MySQL出现“ls: /var/lib/mysql/*.pid: 没有那个文件或目录”...
  • .Net Core webapi RestFul 统一接口数据返回格式
  • .net开源工作流引擎ccflow表单数据返回值Pop分组模式和表格模式对比
  • .NET框架设计—常被忽视的C#设计技巧
  • .sh 的运行
  • @Autowired标签与 @Resource标签 的区别
  • @ModelAttribute注解使用
  • @private @protected @public
  • [ACTF2020 新生赛]Include
  • [Android]Android开发入门之HelloWorld
  • [Android]How to use FFmpeg to decode Android f...
  • [C++]: 模板进阶
  • [CSS]CSS 的背景