relative static method

String relative(
  1. int timestamp, {
  2. DateTime? now,
})

Coarse "how fresh is this" label. Deliberately recomputed on rebuild rather than driven by a timer — a slightly stale label costs nothing, a permanently-ticking Timer in an inspector overlay does.

Implementation

static String relative(int timestamp, {DateTime? now}) {
  final then = DateTime.fromMillisecondsSinceEpoch(timestamp);
  final diff = (now ?? DateTime.now()).difference(then);
  if (diff.isNegative || diff.inSeconds < 5) return '刚刚';
  if (diff.inSeconds < 60) return '${diff.inSeconds} 秒前';
  if (diff.inMinutes < 60) return '${diff.inMinutes} 分钟前';
  if (diff.inHours < 24) return '${diff.inHours} 小时前';
  return '${diff.inDays} 天前';
}