[Flutter] timeago

euni·2025년 4월 10일

Flutter

목록 보기
7/12

플러터로 위젯을 빌드할때, 현재시간과 비교해서 몇분전 혹은 몇시간전/며칠 전 인지에 대한 시간 정보를 보여줘야 할 때 사용할 수 있는 유용한 패키지

https://pub.dev/packages/timeago

사용 방법

1. 의존성 추가

- pubspec.yaml에 아래 코드 추가

timeago: ^3.7.0

2. 예시

  • 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),
      ],
    );
  }```
profile
플러터 개발자 👩🏻‍💻

0개의 댓글