cutils

通用 Flutter 工具库:金额高精度计算、字节解析、日期时间、防抖节流、正则、加解密、存储、网络、日志收集等 17 个互相独立的工具模块。

一行导入全部可用,也可只 import 需要的单个模块。所有静态工具类统一 *Utils 后缀;有状态工具用预置顶层单例(spUtil、logger、eventBus、netUtil、debounceUtil、logCollector 等),无需自己 new。

安装

dependencies:
  cutils: ^0.6.0

或直接引 git:

dependencies:
  cutils:
    git:
      url: https://github.com/BIG-BEARC/cutils.git

快速上手

import 'package:cutils/cutils.dart';

// 金额:分 → 元(基于 Decimal,不丢精度)
MoneyUtils.changeF2Y(1999); // → '19.99'

// 日期格式化
DateTimeUtils.formatDate(DateTime.now(), format: 'yyyy-MM-dd'); // → '2026-11-26'

// 字符串扩展:手机号脱敏
'13812345678'.maskMobile; // → '138****5678'

按需导入单个模块(推荐,不引入无关依赖):

import 'package:cutils/num/money_utils.dart';

工具速查

以下示例均可在导入 barrel(或对应单模块)后直接运行;注释里的返回值均经实测或源码核对。

金额与数值(lib/num/)

工具 用途 示例
MoneyUtils 分↔元换算(Decimal 高精度) MoneyUtils.changeF2Y(1999) → '19.99';MoneyUtils.changeY2F('19.99') → 1999
NumUtils 字符串↔数值、高精度四则 NumUtils.getIntByValueString('42') → 42;NumUtils.addNum(0.1, 0.2) → 0.3

类型扩展(lib/ext/,挂在内置类型上)

扩展 用途 示例
String 扩展 脱敏 / 格式化 / 判校 '13812345678'.maskMobile → '138****5678';'1234567'.thousandSeparated → '1,234,567'
int / double 扩展 金额与百分比格式化 12345.currencyFormat → '123.45'(分→元);0.25.percentFormat() → '25%'

更多成员(hideNumber、maskMiddle、formatDigitPattern 等)见各 *_ext.dart 文件 Dartdoc。

字节(lib/byte/)

工具 用途 示例
ByteUtils 字节↔可读串、字节比较 ByteUtils.toReadable(bytes) → '01 ab'(hex + 空格分隔)
ByteWord / ByteDoubleWord 16 / 32 位整数端序拆装 协议解析中按字节拆装数值,见 Dartdoc

日期时间(lib/datetime/)

工具 用途 示例
DateTimeUtils 格式化 / 解析 / 时长拆解 / 上下午 / 生肖星座 / 营业日 DateTimeUtils.formatFitTimeSpan(90061000) → '1天1小时1分钟1秒';getChineseZodiacByYear(2026) → '马';getZodiacByMonthDay(11, 26) → '射手座'
TimelineUtils 时间轴相对文案 TimelineUtils.formatA(ms) → 今天的毫秒输出 'HH:mm'
TimerUtils 轮询 / 倒计时(可注入时钟,测试友好) TimerUtils()..setOnTimerTickCallback((ms) => print(ms))..startCountDown()
DebounceUtils 防抖节流(顶层单例 debounceUtil) debounceUtil.canRun('submit') 防连点;debounceUtil.debounce('search', () => doSearch()) 输入防抖

正则(lib/regex/)

工具 用途 示例
RegexUtils 手机号 / 身份证 / 邮箱 / URL 等校验、取匹配 RegexUtils.isMobileExact('13812345678') → true;RegexUtils.getMatches(r'\d+', 'a1b22c333') → [1, 22, 333]
RegexConstants 常用正则常量(SCREAMING_CASE 命名) 配合 RegexUtils.matches(RegexConstants.xxx, input) 使用

JSON(lib/json/)

工具 用途 示例
JsonUtils 编解码 / 对象与列表互转 / 美化打印 JsonUtils.encodeObj({'a': 1}) → '{"a":1}';JsonUtils.toMap('{"a":1}') → {'a': 1}

加解密(lib/crypto/)

工具 用途 示例
CryptoUtils md5 / sha256 / hmac / base64 / xor CryptoUtils.encodeMd5('abc') → '900150983cd24fb0d6963f7d28e17f72';xorBase64Encode / xorBase64Decode 可逆混淆

文件(lib/file/)

工具 用途 示例
FileUtils 常用目录定位 / 读写 / 压缩 / 过期清理 await FileUtils().getLocalDocumentFile(fileName: 'a.txt')

存储(lib/storage/)

工具 用途 示例
spUtil SharedPreferences 封装;Windows 端文件损坏自动备份恢复 await spUtil.init(); 之后 await spUtil.putString('k', 'v');对象走 putJsonable / spUtil.getObject('k', User.fromJson)

网络(lib/net/)

工具 用途 示例
netUtil 在线状态判断 await netUtil.isConnectedNet() → true / false
LanUtils 局域网 IPv4 获取 await LanUtils.getPreferredLanIpv4()

系统(lib/system/)

工具 用途 示例
deviceInfo / packageInfoUtil 设备 / 包信息(先 init()) await deviceInfo.init();
KeyboardUtils 收起键盘、硬件按键解析 KeyboardUtils.closeKeyBoard(context)
SystemUtils 屏幕方向 / 沉浸式 / 剪贴板 / 软键盘 SystemUtils().setPortraitMode()
VersionUtils 版本号比较 VersionUtils.compare('1.2.0', '1.10.0') → -1

UI 测量 / 图片(lib/ui/)

工具 用途 示例
CalculateUtils 文本宽高测量 CalculateUtils.calculateTextWidth(context, '文本', 14, FontWeight.w400, 200, 1)
ImageUtils 资源图快捷构造 ImageUtils.assetImage('logo.png')

事件总线(lib/event/)

工具 用途 示例
eventBus 类型化事件(事件类继承 Event) class LoginEvt extends Event {} → eventBus.fire(LoginEvt()); / eventBus.listen<LoginEvt>((e) => ...)

随机 / UUID(lib/identifier/)

工具 用途 示例
RandomUtils 随机数 / 随机串 RandomUtils.randInt(10) → 0~9;RandomUtils.randomString(length: 8)
UuidUtils uuid v1 / v4 / v5(v5 结果带缓存) UuidUtils.generateV4();UuidUtils.generateV5(namespace, name)

日志(lib/log/)

工具 用途 示例
logger 控制台日志(logger 包薄封装) logger.i('ready');

日志收集子系统(lib/log_collector/,可选 UI)

工具 用途 示例
logCollector 日志收集 / 过滤 / 内存+文件存储 / 多输出 await LogCollectorHelper.quickInitialize(); 之后自动接管 debugPrint 与未捕获异常
日志查看器 悬浮球 + 面板 UI(默认关闭,显式启用) 启用方式见 lib/log_collector/README.md

示例工程

完整按领域分组的调用演示见 example/。

变更记录

见 CHANGELOG.md。

Libraries

byte/byte_utils
crypto/crypto_utils
cutils
cutils —— 通用 Flutter 工具库顶层 barrel。
datetime/abs_time_info
datetime/data_formats
datetime/date_utils
datetime/day_format
datetime/debounce_utils
datetime/en_info
datetime/en_normal_info
datetime/timeline_util
datetime/timer_util
datetime/zh_info
datetime/zh_normal_info
event/event_bus_util
ext/bool_ext
ext/double_ext
ext/ext_fun
  • @Author: chuxiong
  • @Created at: 2022/5/18 1:47 下午
  • @Email:
  • @Company: 嘉联支付
  • description 拓展函数
  • ext/int_ext
    ext/object_ext
    ext/string_ext
    ext/widget_ext
    file/file_utils
    identifier/random_utils
    identifier/uuid_utils
    json/json_utils
    log/log
    log_collector/log_collector
    log_collector/log_collector_config
    log_collector/log_collector_export
  • @Author: chuxiong
  • @Created at: 2025/01/XX
  • @Email:
  • @Company: 嘉联支付
  • description 日志收集模块导出文件
  • log_collector/log_collector_helper
    log_collector/log_entry
  • @Author: chuxiong
  • @Created at: 2025/01/XX
  • @Email:
  • @Company: 嘉联支付
  • description 日志条目
  • log_collector/log_interceptor
    log_collector/log_output
    log_collector/log_storage
    log_collector/route_log_observer
    log_collector/view/log_viewer
    log_collector/view/log_viewer_data
    log_collector/view/log_viewer_entry_tile
    log_collector/view/log_viewer_panel
    log_collector/view/log_viewer_theme
    log_collector/view/viewer_bridge_output
    net/lan_utils
    net/net_util
    num/money_unit
    num/money_utils
    num/num_utils
    regex/regex_constants
    regex/regex_utils
    storage/sp_backup_io
    storage/sp_backup_none
    Windows SP 备份恢复的 web / 无 dart:io 平台 no-op 实现。
    storage/sp_util
    system/device_info_util
    system/keyboard_util
    system/package_info_util
    system/system_utils
    system/version_utils
    ui/calculate_utils
    ui/image_utils