|
1 | 1 | import 'package:flutter/material.dart';
|
| 2 | +import 'package:intl/intl.dart'; |
2 | 3 | import 'package:timeago/timeago.dart' as timeago;
|
3 | 4 |
|
4 |
| -/// Formats the given [dateTime] into a relative time string |
5 |
| -/// (e.g., "5m ago", "Yesterday", "now"). |
6 |
| -/// |
7 |
| -/// Uses the current locale from [context] to format appropriately. |
8 |
| -/// Returns an empty string if [dateTime] is null. |
9 |
| -String formatRelativeTime(BuildContext context, DateTime? dateTime) { |
10 |
| - if (dateTime == null) { |
11 |
| - return ''; |
| 5 | +/// A utility class for formatting dates and times. |
| 6 | +abstract final class DateFormatter { |
| 7 | + /// Formats the given [dateTime] into a relative time string |
| 8 | + /// (e.g., "5m ago", "Yesterday", "now"). |
| 9 | + /// |
| 10 | + /// Uses the current locale from [context] to format appropriately. |
| 11 | + /// Returns an empty string if [dateTime] is null. |
| 12 | + static String formatRelativeTime(BuildContext context, DateTime? dateTime) { |
| 13 | + if (dateTime == null) { |
| 14 | + return ''; |
| 15 | + } |
| 16 | + final locale = Localizations.localeOf(context).languageCode; |
| 17 | + return timeago.format(dateTime, locale: locale); |
| 18 | + } |
| 19 | + |
| 20 | + /// Formats the given [dateTime] into a short date string (e.g., "Jul 1, 2025"). |
| 21 | + /// |
| 22 | + /// Returns an empty string if [dateTime] is null. |
| 23 | + static String formatDate(DateTime? dateTime) { |
| 24 | + if (dateTime == null) { |
| 25 | + return ''; |
| 26 | + } |
| 27 | + return DateFormat.yMMMd().format(dateTime); |
12 | 28 | }
|
13 |
| - final locale = Localizations.localeOf(context).languageCode; |
14 |
| - return timeago.format(dateTime, locale: locale); |
15 | 29 | }
|
0 commit comments