搜索插件、代码仓与 APP 分类

插件详情v4.0.3

duration

用于简化与 'Duration' 交互的工具。将持续时间格式化为可读的文本形式,并将可读的文本形式解析为 Dart 的 Duration。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
10.3万
喜欢数
172
Pub 分数
150

安装插件

flutter pub add duration

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

duration 包提供了一些工具,可简化与 Dart 的 Duration 类一起使用。它能够将持续时间格式化为人类可读的字符串(例如 '2h 30m'),并将此类字符串解析回 Duration 对象。这在用户界面中显示时间间隔或以自然格式接受用户输入的时间时特别有用。该包支持所有主要平台,包括移动、Web 和桌面,非常适合处理时间相关数据的跨平台应用程序。

适合场景

  • 以可读格式显示经过的时间
  • 解析用户输入的时间表达式
  • 在持续时间表示之间进行转换
  • 应用中的时间跟踪
  • 计划任务或倒计时功能

主要特点

  • 可读性高的持续时间格式化
  • 字符串转持续时间解析
  • 跨平台兼容性
  • 简单的 API,设置简单
  • 支持小时、分钟、秒和毫秒

适合对象

  • 需要时间显示格式化的应用
  • 面向用户的时长输入字段
  • 任务或计时器应用
  • 跨平台时间工具
  • 寻求轻量级持续时间帮助程序的开发人员

使用建议

  • 不支持复杂的时区或日历计算
  • 仅限基本持续时间单位
  • 需要手动验证边缘情况
  • 不适用于日期计算
  • 无内置本地化支持

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

duration pub package

Utilities to make working with 'Duration's easier.

Format duration

Use Duration.pretty() method or prettyDuration function to format a duration.

main() {
  final dur = Duration(
    days: 5,
    hours: 23,
    minutes: 59,
    seconds: 59,
    milliseconds: 999,
    microseconds: 999,
  );

  // => 5d, 23h, 59m, 59s
  print(dur.pretty());

  // => 3 seconds
  print((aMillisecond * 3000).pretty());

  // => 2 seconds 250 milliseconds
  print((aMillisecond * 2250).pretty);

  // => 1 day 3 hours 2 minutes
  print((aMillisecond * 97320000).pretty());
}

With desired locale

Use locale parameter to format with desired locale.

main() {
  // => 5 días 9 horas
  final dur = aDay * 5 + anHour * 9;
  print(
      dur.pretty(
        abbreviated: false,
        locale: DurationLocale.fromLanguageCode('ru'),
      ));
}

Abbreviate units

Use abbreviated parameter to use abbreviated units.

main() {
  final dur = Duration(
    days: 5,
    hours: 23,
    minutes: 59,
    seconds: 59,
    milliseconds: 999,
    microseconds: 999,
  );

  // => 5d, 23h, 59m, 59s, 999ms, 999us
  print(dur.pretty(abbreviated: true, tersity: DurationTersity.all));
}

Spacer

Use spacer to add a string between amount and unit.

main() {
  // => 5 whole days 9 whole hours
  print((aDay * 5 + anHour * 9).pretty(spacer: ' whole '));
}

Delimiter

Use delimiter to separate each individual part with a string.

main() {
  // => 5 days, 9 hours and 10 minute
  print((aDay * 5 + anHour * 9 + aMinute * 10).pretty(delimiter: ', '));
}

Conjugation

Use conjugation to add a string before the final unit. Use it in conjunction with delimiter to add ',' and 'and' to separate individual parts.

main() {
  // => 5 days, 9 hours and 10 minutes
  print(
      (aDay * 5 + anHour * 9 + aMinute * 10).pretty(
        delimiter: ', ',
        conjugation: ' and ',
      ));
}

Parse duration

Parse duration

main() {
  final Duration dur = parseDuration('245:09:08.007006');
  print(dur);
}

Parse time

main() {
  final Duration dur = parseTime('245:09:08.007006');
  print(dur);
}

所属分类

源码仓库

使用此插件的项目