From c01613acb9adf6d23eeda00397ac1307c11e6fe9 Mon Sep 17 00:00:00 2001 From: mateusz-bak <32651935+mateusz-bak@users.noreply.github.com> Date: Fri, 24 Nov 2023 15:32:39 +0000 Subject: [PATCH] fix: Displayed dates format will follow device locale --- lib/ui/add_book_screen/widgets/date_row.dart | 8 ++++++-- lib/ui/book_screen/book_screen.dart | 11 ++--------- .../book_screen/widgets/book_status_detail.dart | 14 +++++++++----- lib/ui/books_screen/widgets/book_card.dart | 15 ++++++--------- 4 files changed, 23 insertions(+), 25 deletions(-) diff --git a/lib/ui/add_book_screen/widgets/date_row.dart b/lib/ui/add_book_screen/widgets/date_row.dart index 80dc89ed..b9fbf2f9 100644 --- a/lib/ui/add_book_screen/widgets/date_row.dart +++ b/lib/ui/add_book_screen/widgets/date_row.dart @@ -28,6 +28,10 @@ class DateRow extends StatelessWidget { @override Widget build(BuildContext context) { + final dateFormat = DateFormat.yMd( + '${context.locale.languageCode}-${context.locale.countryCode}', + ); + return Padding( padding: const EdgeInsets.symmetric(horizontal: 10), child: BlocBuilder( @@ -52,7 +56,7 @@ class DateRow extends StatelessWidget { icon: FontAwesomeIcons.play, text: (state.startDate == null) ? LocaleKeys.start_date.tr() - : '${state.startDate?.day}/${state.startDate?.month}/${state.startDate?.year}', + : dateFormat.format(state.startDate!), onPressed: showStartDatePicker, onClearPressed: clearStartDate, showClearButton: (state.startDate == null) ? false : true, @@ -74,7 +78,7 @@ class DateRow extends StatelessWidget { icon: FontAwesomeIcons.flagCheckered, text: (state.finishDate == null) ? LocaleKeys.finish_date.tr() - : '${state.finishDate?.day}/${state.finishDate?.month}/${state.finishDate?.year}', + : dateFormat.format(state.finishDate!), onPressed: showFinishDatePicker, onClearPressed: clearFinishDate, showClearButton: diff --git a/lib/ui/book_screen/book_screen.dart b/lib/ui/book_screen/book_screen.dart index 3d2cc925..83428e60 100644 --- a/lib/ui/book_screen/book_screen.dart +++ b/lib/ui/book_screen/book_screen.dart @@ -141,13 +141,6 @@ class BookScreen extends StatelessWidget { context.read().setBook(book); } - String? _generateDate(DateTime? date) { - if (date == null) return null; - - final DateFormat formatter = DateFormat('dd/MM/yyyy'); - return formatter.format(date); - } - String _generateReadingTime({ DateTime? startDate, DateTime? finishDate, @@ -208,8 +201,8 @@ class BookScreen extends StatelessWidget { context, ), rating: state.rating, - startDate: _generateDate(state.startDate), - finishDate: _generateDate(state.finishDate), + startDate: state.startDate, + finishDate: state.finishDate, onLikeTap: () => _onLikeTap(context, state), isLiked: state.favourite, showChangeStatus: (state.status == 1 || diff --git a/lib/ui/book_screen/widgets/book_status_detail.dart b/lib/ui/book_screen/widgets/book_status_detail.dart index f8bc247d..6fd67e45 100644 --- a/lib/ui/book_screen/widgets/book_status_detail.dart +++ b/lib/ui/book_screen/widgets/book_status_detail.dart @@ -25,8 +25,8 @@ class BookStatusDetail extends StatelessWidget { final IconData? statusIcon; final String statusText; - final String? startDate; - final String? finishDate; + final DateTime? startDate; + final DateTime? finishDate; final int? rating; final Function() onLikeTap; final bool isLiked; @@ -36,9 +36,13 @@ class BookStatusDetail extends StatelessWidget { final bool showRatingAndLike; Widget _buildStartAndFinishDate(BuildContext context) { + final dateFormat = DateFormat.yMd( + '${context.locale.languageCode}-${context.locale.countryCode}', + ); + if (startDate != null && finishDate != null) { return Text( - '$startDate - $finishDate', + '${dateFormat.format(startDate!)} - ${dateFormat.format(finishDate!)}', style: const TextStyle( fontSize: 14, fontWeight: FontWeight.normal, @@ -48,7 +52,7 @@ class BookStatusDetail extends StatelessWidget { if (startDate == null && finishDate != null) { return Text( - '${LocaleKeys.finished_on_date.tr()} $finishDate', + '${LocaleKeys.finished_on_date.tr()} ${dateFormat.format(finishDate!)}', style: const TextStyle( fontSize: 14, fontWeight: FontWeight.normal, @@ -58,7 +62,7 @@ class BookStatusDetail extends StatelessWidget { if (startDate != null && finishDate == null) { return Text( - '${LocaleKeys.started_on_date.tr()} $startDate', + '${LocaleKeys.started_on_date.tr()} ${dateFormat.format(startDate!)}', style: const TextStyle( fontSize: 14, fontWeight: FontWeight.normal, diff --git a/lib/ui/books_screen/widgets/book_card.dart b/lib/ui/books_screen/widgets/book_card.dart index 55a3bfe1..e72c5167 100644 --- a/lib/ui/books_screen/widgets/book_card.dart +++ b/lib/ui/books_screen/widgets/book_card.dart @@ -31,6 +31,10 @@ class BookCard extends StatelessWidget { Widget _buildSortAttribute() { return BlocBuilder( builder: (context, state) { + final dateFormat = DateFormat.yMd( + '${context.locale.languageCode}-${context.locale.countryCode}', + ); + if (state is SetSortState) { if (state.sortType == SortType.byPages) { return (book.pages != null) @@ -46,7 +50,7 @@ class BookCard extends StatelessWidget { style: const TextStyle(fontSize: 12), ), Text( - '${_generateDate(book.startDate)}', + dateFormat.format(book.startDate!), style: const TextStyle( fontSize: 13, fontWeight: FontWeight.bold, @@ -65,7 +69,7 @@ class BookCard extends StatelessWidget { style: const TextStyle(fontSize: 12), ), Text( - '${_generateDate(book.finishDate)}', + dateFormat.format(book.finishDate!), style: const TextStyle( fontSize: 13, fontWeight: FontWeight.bold, @@ -148,13 +152,6 @@ class BookCard extends StatelessWidget { return chips; } - String? _generateDate(DateTime? date) { - if (date == null) return null; - - final DateFormat formatter = DateFormat('dd/MM/yyyy'); - return formatter.format(date); - } - @override Widget build(BuildContext context) { final coverFile = book.getCoverFile();