搜尋套件、儲存庫與 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 物件。這在使用者介面中顯示時間間隔或以自然格式接受使用者輸入時間時特別有用。此套件支援所有主要平台,包括行動裝置、網頁和桌面,非常適合處理時間相關資料的跨平台應用程式。

適用情境

  • 以可讀格式顯示經過的時間
  • 解析用戶輸入的時間表達式
  • 在持續時間表示之間進行轉換
  • 應用中的時間追蹤
  • 排程或倒數計時功能

主要特色

  • 可讀性高的持續時間格式化
  • 字串轉持續時間解析
  • 跨平台相容性
  • 簡單的 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);
}

所屬分類

原始碼儲存庫

使用此套件的專案