パッケージ、リポジトリ、アプリカテゴリを検索

パッケージ詳細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 を含むすべての主要プラットフォームでシンプルさとパフォーマンスを重視して設計されています。フィード、通知、チャットインターフェースなどで動的かつユーザーに親しみやすい時刻表示が必要なアプリに最適です。

用途

  • 投稿日からの経過時間を表示
  • カウントダウンまたはリマインダーを表示
  • チャットメッセージのタイムスタンプの強化
  • リアルタイム活動インジケーターの更新
  • ソーシャルメディアアプリにおけるUXの向上

主な機能

  • 最小限のセットアップで簡単な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 件

ソースリポジトリ

このパッケージを使用しているプロジェクト