我的Flutter开源库:
Flutter 汉字转拼音库lpinyin
Flutter 常用工具类库common_utils
Flutter 城市列表 索引&悬停
Flutter 时间轴工具类TimelineUtil
[TimelineUtil]
时间轴工具类。例如,(小于10s->刚刚)、x分钟、x小时、x天、[今年: (昨天/1天前)、(2天前)、MM-dd]、[往年: yyyy-MM-dd]。TimelineUtil支持中英文格式输出,支持自定义配置信息,丰富的输出格式。
以下是主流app时间轴示例:
微信
微博
支付宝
GitHub:TimelineUtil.
Pub: TimelineUtil
Demo: flutter_demos.
APK:点击下载 v1.0.2
Android扫码下载APK
Add dependency
dependencies:
common_utils: x.x.x #latest version
APIs
-
TimelineUtil
///(xx)为可配置输出
enum DayFormat {
///(小于10s->刚刚)、x分钟、x小时、(昨天)、x天.
Simple,
///(小于10s->刚刚)、x分钟、x小时、[今年: (昨天/1天前)、(2天前)、MM-dd],[往年: yyyy-MM-dd].
Common,
///日期 + HH:mm
///小于10s->刚刚)、x分钟、x小时、[今年: (昨天 HH:mm/1天前)、(2天前)、MM-dd HH:mm],[往年: yyyy-MM-dd HH:mm].
Full,
}
///Timeline信息配置.
abstract class TimelineInfo {
String suffixAgo(); //suffix ago(后缀 后).
String suffixAfter(); //suffix after(后缀 前).
String lessThanTenSecond() => ''; //just now(刚刚).
String customYesterday() => ''; //Yesterday(昨天).优先级高于keepOneDay
bool keepOneDay(); //保持1天,example: true -> 1天前, false -> MM-dd.
bool keepTwoDays(); //保持2天,example: true -> 2天前, false -> MM-dd.
String oneMinute(int minutes); //a minute(1分钟).
String minutes(int minutes); //x minutes(x分钟).
String anHour(int hours); //an hour(1小时).
String hours(int hours); //x hours(x小时).
String oneDay(int days); //a day(1天).
String days(int days); //x days(x天).
}
setLocaleInfo : 自定义设置配置信息.
formatByDateTime : 格式输出时间轴信息 by DateTime .
format : 格式输出时间轴信息.
Example
// Import package
import 'package:common_utils/common_utils.dart';
class ZHAliPayTimelineInfo implements TimelineInfo {
String suffixAgo() => '前';
String suffixAfter() => '后';
String lessThanTenSecond() => '刚刚';
String customYesterday() => '昨天';
bool keepOneDay() => true;
bool keepTwoDays() => false;
String oneMinute(int minutes) => '$minutes分';
String minutes(int minutes) => '$minutes分';
String anHour(int hours) => '$hours小时';
String hours(int hours) => '$hours小时';
String oneDay(int days) => '$days天';
String days(int days) => '$days天';
}
class ENAliPayTimelineInfo implements TimelineInfo {
String suffixAgo() => ' ago';
String suffixAfter() => ' after';
String lessThanTenSecond() => 'just now';
String customYesterday() => 'Yesterday';
bool keepOneDay() => true;
bool keepTwoDays() => false;
String oneMinute(int minutes) => 'a minute';
String minutes(int minutes) => '$minutes minutes';
String anHour(int hours) => 'an hour';
String hours(int hours) => '$hours hours';
String oneDay(int days) => 'a day';
String days(int days) => '$days days';
}
setLocaleInfo('zh_alipay',ZHAliPayTimelineInfo());
setLocaleInfo('en_alipay', ENAliPayTimelineInfo());
Stringtimeline=TimelineUtil.format(timeMillis,locale:(isChinese ? 'zh_alipay' : 'en_alipay'));