搜尋套件、儲存庫與 APP 分類

套件詳情v5.0.0

relative_time

以文字形式表示相對於目前時間的 DateTime 物件。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
8230
喜歡數
22
Pub 分數
160

安裝套件

flutter pub add relative_time

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

relative_time 套件將 DateTime 物件轉換為相對於目前時刻的人類可讀字串。例如,它可以顯示「2分鐘前」或「3小時後」,而不是原始時間戳。它支援多種語言,並設計簡單且效能優異,適用於所有主要平台,包括 Android、iOS、Web、macOS、Windows 和 Linux。非常適合需要在動態、使用者友善的時間顯示功能之應用程式,如資訊流、通知或聊天介面。

適用情境

  • 顯示文章發布以來的時間
  • 顯示倒計時或提醒
  • 增強聊天訊息時間戳
  • 更新即時活動指標
  • 改善社交媒體應用的使用者體驗

主要特色

  • 簡單的 API,設定簡單
  • 支援在地化
  • 跨平台相容性
  • 即時準確性自動更新
  • 輕量且高效

適合對象

  • 具有即時資料流的應用程式
  • 社交網路應用
  • 任務和提醒工具
  • 聊天和訊息平台
  • 需要動態時間標籤的使用者介面元件

使用建議

  • 需要手動重新整理以取得即時更新
  • 僅限相對時間格式化
  • 不支援自訂時間單位
  • 依賴系統時鐘的準確性
  • 可能需要額外處理時區差異

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

relative_time

A Flutter package to express a DateTime object relative to the current time in text form. For instance, it may transform DateTimes into 3 years ago, or in 42 minutes, or now.

Features

  • Available in more languages than you can name.
  • Provides localization delegate for easy internationalization.
  • Usable as class instance method call or extension method.
  • Can be expressed in different, customizable time units.
  • Supports numeric and natural representations.

Getting started

The package may optionally retrieve the appropriate localization from the BuildContext. The standard way to setup localization is to make use of a WidgetApp's (such as a MaterialApp) localizationsDelegates and supportedLocales. If you're already using these to localize your app, it should suffice to add RelativeTimeLocalizations.delegate to your list of localizationsDelegates, roughly as follows.

MaterialApp(
  localizationsDelegates: const <LocalizationsDelegate<dynamic>>[
    AppLocalizations.delegate,
    RelativeTimeLocalizations.delegate,
    GlobalMaterialLocalizations.delegate,
    GlobalCupertinoLocalizations.delegate,
    GlobalWidgetsLocalizations.delegate,
  ],
  supportedLocales: AppLocalizations.supportedLocales,
);

If your app does not make use of (this form of) localization yet but you would like it to, reference the Flutter documentation on internationalization for more information on how to set this up.

Usage

The relative time may be accessed by passing the DateTime source object to format on an instance of RelativeTime with either a BuildContext (if configured for localization) or a specific Locale.

RelativeTime(context).format(time);
RelativeTime.locale(const Locale('en')).format(time);

Alternatively, the provided DateTime extension methods are a bit more concise.

time.relativeTime(context);
time.relativeTimeLocale(const Locale('en'));

You may specify a list of time units that will be considered. This can be any of the following: year, month, week, day, hour, minute and second. By default, all time units are potentially used. The following limits the relative time to being expressed in years and weeks. No judgement here. Because weeks are the most granular time unit specified, anything less than one week would be represented as this week.

RelativeTime(
  context, 
  timeUnits: const <TimeUnit>[
    TimeUnit.year,
    TimeUnit.week,
  ],
).format(time);

By default, results are described using natural language as much as possible. That is, one day in the future will be transformed into tomorrow. If you'd rather see this as in 1 day, set the numeric parameter to true.

RelativeTime(
  context,
  numeric: true,
).format(time);

Testing

The current time may be mocked with help from the clock package.

withClock(
  Clock.fixed(DateTime.fromMicrosecondsSinceEpoch(0)),
  () => RelativeTime(context).format(time),
);

Additional information

Translations have been extracted from the Unicode CLDR Project. The extraction tool for this purpose is available in the dev folder. Note that due to the automated nature of this process, PRs for language updates will not be considered.

所屬分類

依賴

依賴清單 2 項

原始碼儲存庫

使用此套件的專案