搜索插件、代码仓与 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 项

源码仓库

使用此插件的项目