From 99ee7a294021f65397cde5d8b7beba93afcc95ff Mon Sep 17 00:00:00 2001 From: TheMedo Date: Tue, 19 Nov 2024 17:13:10 +0100 Subject: [PATCH] Format due date --- lib/data/util/format.dart | 30 +++++++++++++++++++++++++ lib/screen/details/details_screen.dart | 11 ++++++++- lib/screen/home/widgets/todos_list.dart | 11 ++++++++- pubspec.lock | 8 +++++++ pubspec.yaml | 1 + 5 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 lib/data/util/format.dart diff --git a/lib/data/util/format.dart b/lib/data/util/format.dart new file mode 100644 index 0000000..cd2cfad --- /dev/null +++ b/lib/data/util/format.dart @@ -0,0 +1,30 @@ +import 'package:intl/intl.dart'; + +String formatDateTime(DateTime? dateTime) { + if (dateTime == null) return ''; + final now = DateTime.now(); + final today = DateTime(now.year, now.month, now.day); + final tomorrow = today.add(Duration(days: 1)); + final yesterday = today.subtract(Duration(days: 1)); + final weekStart = today.subtract(Duration(days: today.weekday - 1)); + final weekEnd = weekStart.add(Duration(days: 6)); + + final timeFormat = DateFormat('h:mm a'); + final dateFormat = DateFormat('MMM d, yyyy'); + final dayMonthTimeFormat = DateFormat('EEE h:mm a'); + final monthDayTimeFormat = DateFormat('MMM d, h:mm a'); + + if (dateTime.isAfter(today) && dateTime.isBefore(tomorrow)) { + return 'Today ${timeFormat.format(dateTime)}'; + } else if (dateTime.isAfter(yesterday) && dateTime.isBefore(today)) { + return 'Yesterday ${timeFormat.format(dateTime)}'; + } else if (dateTime.isAfter(today) && dateTime.isBefore(tomorrow.add(Duration(days: 1)))) { + return 'Tomorrow ${timeFormat.format(dateTime)}'; + } else if (dateTime.isAfter(tomorrow) && dateTime.isBefore(weekEnd)) { + return dayMonthTimeFormat.format(dateTime); + } else if (dateTime.isBefore(weekStart)) { + return '${dateFormat.format(dateTime)} ${timeFormat.format(dateTime)}'; + } else { + return monthDayTimeFormat.format(dateTime); + } +} diff --git a/lib/screen/details/details_screen.dart b/lib/screen/details/details_screen.dart index 9cddab3..db40cf3 100644 --- a/lib/screen/details/details_screen.dart +++ b/lib/screen/details/details_screen.dart @@ -6,6 +6,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_local_notifications/flutter_local_notifications.dart'; import 'package:timezone/timezone.dart' as tz; import 'package:todo_fall_2024_0/data/model/todo.dart'; +import 'package:todo_fall_2024_0/data/util/format.dart'; import 'package:todo_fall_2024_0/screen/home/home_screen.dart'; final flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin(); @@ -84,7 +85,15 @@ class _DetailsScreenState extends State { SizedBox(height: 16), ListTile( title: Text('Due date'), - subtitle: dueDate == null ? null : Text(dueDate?.toString() ?? ''), + subtitle: dueDate == null + ? null + : Text( + formatDateTime(dueDate), + style: Theme.of(context) + .textTheme + .bodyMedium + ?.copyWith(color: DateTime.now().isAfter(dueDate!) ? Colors.redAccent : null), + ), trailing: dueDate == null ? IconButton( onPressed: () async { diff --git a/lib/screen/home/widgets/todos_list.dart b/lib/screen/home/widgets/todos_list.dart index 48d0879..a1e5867 100644 --- a/lib/screen/home/widgets/todos_list.dart +++ b/lib/screen/home/widgets/todos_list.dart @@ -1,4 +1,5 @@ import 'package:flutter/material.dart'; +import 'package:todo_fall_2024_0/data/util/format.dart'; import 'package:todo_fall_2024_0/screen/details/details_screen.dart'; import '../../../data/model/todo.dart'; @@ -34,7 +35,15 @@ class TodosList extends StatelessWidget { decoration: isCompleted ? TextDecoration.lineThrough : null, ), ), - subtitle: todo.dueDate == null ? null : Text(todo.dueDate.toString()), + subtitle: todo.dueDate == null + ? null + : Text( + formatDateTime(todo.dueDate), + style: Theme.of(context) + .textTheme + .bodyMedium + ?.copyWith(color: DateTime.now().isAfter(todo.dueDate!) ? Colors.redAccent : null), + ), trailing: IconButton( icon: Icon(Icons.chevron_right), onPressed: () { diff --git a/pubspec.lock b/pubspec.lock index 1615713..80dca3d 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -280,6 +280,14 @@ packages: url: "https://pub.dev" source: hosted version: "4.0.2" + intl: + dependency: "direct main" + description: + name: intl + sha256: d6f56758b7d3014a48af9701c085700aac781a92a87a62b1333b46d8879661cf + url: "https://pub.dev" + source: hosted + version: "0.19.0" leak_tracker: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 00dac0c..9e11ba7 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -18,6 +18,7 @@ dependencies: flutter_local_notifications: ^18.0.1 app_settings: ^5.1.1 timezone: ^0.10.0 + intl: ^0.19.0 dev_dependencies: