플러터로 위젯을 빌드할때, 현재시간과 비교해서 몇분전 혹은 몇시간전/며칠 전 인지에 대한 시간 정보를 보여줘야 할 때 사용할 수 있는 유용한 패키지
- pubspec.yaml에 아래 코드 추가
timeago: ^3.7.0
import
import 'package:timeago/timeago.dart' as timeago;
코드
static Widget requestInfo(PostModel post) {
// locale 설정
timeago.setLocaleMessages('ko', timeago.KoMessages());
DateTime createdDate = DateTime.fromMillisecondsSinceEpoch(post.createdAt);
return Row(
children: [
const SizedBox(width: 5),
Container(
width: 4,
height: 4,
decoration: BoxDecoration(
color: const Color(0xFFFF0000),
borderRadius: BorderRadius.circular(100),
),
),
const SizedBox(width: 12),
Expanded(
child: Text(
post.title,
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
),
const SizedBox(width: 25),
Text(timeago.format(createdDate, locale: 'ko')),
const SizedBox(width: 5),
],
);
}```