getRelativeFormat static method
获得相对时间
Implementation
static String getRelativeFormat(DateTime? dt, {bool withTime = false}) {
// 格式化最后一条消息的显示时间
// 如果是当天的信息,只显示具体时分;
// 否则,如果是当年的信息,显示年月;
// 其他情况显示年月日;
if (dt == null) return '';
if (dt.isToday) {
// 今天
return DateFormat('HH:mm').format(dt);
} else if (dt.year == DateTime.now().year) {
// 今年
var formatStr = 'MM月dd日';
if (withTime) formatStr += ' HH:mm';
return DateFormat(formatStr).format(dt);
} else {
// 今年以前
var formatStr = 'yyyy年MM月dd日';
if (withTime) formatStr += ' HH:mm';
return DateFormat(formatStr).format(dt);
}
}