Skip to content

Commit

Permalink
Format due date
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMedo committed Nov 19, 2024
1 parent ec51cba commit 99ee7a2
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 2 deletions.
30 changes: 30 additions & 0 deletions lib/data/util/format.dart
Original file line number Diff line number Diff line change
@@ -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);
}
}
11 changes: 10 additions & 1 deletion lib/screen/details/details_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -84,7 +85,15 @@ class _DetailsScreenState extends State<DetailsScreen> {
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 {
Expand Down
11 changes: 10 additions & 1 deletion lib/screen/home/widgets/todos_list.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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: () {
Expand Down
8 changes: 8 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 99ee7a2

Please sign in to comment.