diff --git a/lib/src/date_time_picker.dart b/lib/src/date_time_picker.dart index fd9d5a1..3b22eee 100644 --- a/lib/src/date_time_picker.dart +++ b/lib/src/date_time_picker.dart @@ -2,6 +2,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_date_time_picker/src/utils/date_time_picker_controller.dart'; import 'package:flutter_date_time_picker/src/widgets/month_date_time_picker.dart/month_date_time_picker_sheet.dart'; import 'package:flutter_date_time_picker/src/widgets/week_date_time_picker/week_date_time_picker_sheet.dart'; +import 'package:intl/date_symbol_data_local.dart'; /// A widget that displays a date picker from a sheet form the top of the screen. /// This sheet displays initialy displays a week of days but can be dragged down to show full months. @@ -134,6 +135,7 @@ class _DateTimePickerState extends State { @override void initState() { super.initState(); + initializeDateFormatting(); _dateTimePickerController = DateTimePickerController( highlightToday: widget.highlightToday, diff --git a/lib/src/extensions/date_time.dart b/lib/src/extensions/date_time.dart index 3b05a2b..d48f756 100644 --- a/lib/src/extensions/date_time.dart +++ b/lib/src/extensions/date_time.dart @@ -1,46 +1,54 @@ extension DatePickerUtil on DateTime { - bool isSameDayAs(DateTime selectedDate) { + // Check if the current date is the same as the given date + bool sameDayAs(DateTime selectedDate) { return selectedDate.day == day && selectedDate.month == month && selectedDate.year == year; } - bool isDayPartOf(List dates) { - return dates.any((element) => element.isSameDayAs(this)); + // Check if the current date is contained in the given list + bool isContainedIn(List dates) { + return dates.any((element) => element.sameDayAs(this)); } - List getDaysOfWeek() { + List daysOfWeek() { var startFrom = subtract(Duration(days: weekday)); return List.generate( 7, (i) => startFrom.add( Duration(days: i + 1), ), + growable: false, ); } - int getDaysInMonth() { - if (month == DateTime.february) { - final bool isLeapYear = - (year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0); - return isLeapYear ? 29 : 28; + bool get isLeapYear => + (year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0); + + int daysInMonth() { + late int amountOfDays; + + switch (month) { + case DateTime.january: + case DateTime.march: + case DateTime.may: + case DateTime.july: + case DateTime.august: + case DateTime.october: + case DateTime.december: + amountOfDays = 31; + break; + case DateTime.april: + case DateTime.june: + case DateTime.september: + case DateTime.november: + amountOfDays = 30; + break; + case DateTime.february: + amountOfDays = isLeapYear ? 29 : 28; + break; } - const List daysInMonth = [ - 31, - -1, - 31, - 30, - 31, - 30, - 31, - 31, - 30, - 31, - 30, - 31 - ]; - - return daysInMonth[month - 1]; + return amountOfDays; } -} \ No newline at end of file +} diff --git a/lib/src/widgets/month_date_time_picker.dart/month_date_time_picker.dart b/lib/src/widgets/month_date_time_picker.dart/month_date_time_picker.dart index f50b488..b548487 100644 --- a/lib/src/widgets/month_date_time_picker.dart/month_date_time_picker.dart +++ b/lib/src/widgets/month_date_time_picker.dart/month_date_time_picker.dart @@ -21,21 +21,24 @@ class MonthDateTimePicker extends StatelessWidget { 12, ).weekday; - return SizedBox( + return Container( width: MediaQuery.of(context).size.width, - height: 300, + margin: const EdgeInsets.symmetric(horizontal: 30), child: Center( - child: Wrap( - spacing: 10, + child: GridView.count( + crossAxisSpacing: 5, + crossAxisCount: 7, children: List.generate( - DateTime(date.year, date.month).getDaysInMonth() + daysToSkip, + DateTime(date.year, date.month).daysInMonth() + daysToSkip, (index) { if (index < daysToSkip) { return Container( decoration: BoxDecoration( - color: Colors.red.withOpacity(0.4), - border: Border.all(color: Colors.black, width: 1.5)), - margin: const EdgeInsets.symmetric(vertical: 5), + color: Colors.red.withOpacity(0.4), + border: Border.all(color: Colors.black, width: 1.5), + ), + margin: + const EdgeInsets.symmetric(vertical: 5, horizontal: 5), height: 45, width: 45, child: Center( @@ -50,6 +53,7 @@ class MonthDateTimePicker extends StatelessWidget { } return GestureDetector( + onTap: () async { // await dateTimePickerController.getDragController().animateTo( // 0.26, @@ -69,10 +73,16 @@ class MonthDateTimePicker extends StatelessWidget { )); }, child: Container( - margin: const EdgeInsets.symmetric(vertical: 5), + margin: + const EdgeInsets.symmetric(vertical: 5, horizontal: 5), decoration: BoxDecoration( color: + // isDisabled() + // ? Theme.of(context).disabledColor + // : Colors.transparent, // isSelected(index, daysToSkip) + // ? Theme.of(context).primaryColor.withOpacity(0.2) + // : Colors.transparent, shouldHighlight(index, daysToSkip) ? Theme.of(context).primaryColor : Colors.transparent, @@ -90,7 +100,12 @@ class MonthDateTimePicker extends StatelessWidget { style: Theme.of(context).textTheme.bodyText1!.copyWith( color: + // isDisabled() + // ? Colors.white + // : Colors.transparent, // isSelected(index, daysToSkip) + // ? Theme.of(context).primaryColor + // : Colors.black, shouldHighlight(index, daysToSkip) ? Colors.white : Colors.black, @@ -126,19 +141,23 @@ class MonthDateTimePicker extends StatelessWidget { date.year, date.month, index + 1 - daysToSkip, - ).isSameDayAs( + ).sameDayAs( dateTimePickerController.highlightToday ? DateTime.now() : dateTimePickerController.selectedDate, ); } + bool isDisabled() { + return true; + } + bool isSelected(int index, int daysToSkip) { return DateTime( date.year, date.month, index + 1 - daysToSkip, - ).isSameDayAs(dateTimePickerController.selectedDate); + ).sameDayAs(dateTimePickerController.selectedDate); } bool shouldMark(int index, int daysToSkip) { @@ -146,7 +165,7 @@ class MonthDateTimePicker extends StatelessWidget { date.year, date.month, index + 1 - daysToSkip, - ).isSameDayAs( + ).sameDayAs( dateTimePickerController.highlightToday ? DateTime.now() : dateTimePickerController.selectedDate, @@ -155,7 +174,7 @@ class MonthDateTimePicker extends StatelessWidget { date.year, date.month, index + 1 - daysToSkip, - ).isDayPartOf( + ).isContainedIn( dateTimePickerController.markedDates ?? [], ); } diff --git a/lib/src/widgets/month_date_time_picker.dart/month_date_time_picker_sheet.dart b/lib/src/widgets/month_date_time_picker.dart/month_date_time_picker_sheet.dart index 44b91e4..6a08cd7 100644 --- a/lib/src/widgets/month_date_time_picker.dart/month_date_time_picker_sheet.dart +++ b/lib/src/widgets/month_date_time_picker.dart/month_date_time_picker_sheet.dart @@ -25,7 +25,7 @@ class MonthDateTimePickerSheet extends StatelessWidget { height: 10, ), Text( - DateFormat("MMMM yyyy").format( + DateFormat.yMMMM().format( dateTimePickerController.browsingDate, ), style: Theme.of(context) @@ -45,7 +45,7 @@ class MonthDateTimePickerSheet extends StatelessWidget { Duration( days: DateTime(dateTimePickerController.browsingDate.year, dateTimePickerController.browsingDate.month) - .getDaysInMonth(), + .daysInMonth(), ), ), ); @@ -55,7 +55,7 @@ class MonthDateTimePickerSheet extends StatelessWidget { Duration( days: DateTime(dateTimePickerController.browsingDate.year, dateTimePickerController.browsingDate.month) - .getDaysInMonth(), + .daysInMonth(), ), ), ); diff --git a/lib/src/widgets/week_date_time_picker/week_date_time_picker.dart b/lib/src/widgets/week_date_time_picker/week_date_time_picker.dart index a9ff3e5..dd61730 100644 --- a/lib/src/widgets/week_date_time_picker/week_date_time_picker.dart +++ b/lib/src/widgets/week_date_time_picker/week_date_time_picker.dart @@ -19,10 +19,10 @@ class WeekDateTimePicker extends StatelessWidget { return Row( mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: List.generate( - date.getDaysOfWeek().length, + date.daysOfWeek().length, (index) => GestureDetector( onTap: () { - dateTimePickerController.onTapDay(date.getDaysOfWeek()[index]); + dateTimePickerController.onTapDay(date.daysOfWeek()[index]); }, child: SizedBox( width: 40, @@ -31,9 +31,11 @@ class WeekDateTimePicker extends StatelessWidget { children: [ const Spacer(), Text( - DateFormat('EEEE').format( - date.getDaysOfWeek().elementAt(index), - )[0], + DateFormat.E() + .format( + date.daysOfWeek().elementAt(index), + ) + .toUpperCase()[0], style: Theme.of(context).textTheme.titleSmall, ), const Spacer(), @@ -52,7 +54,7 @@ class WeekDateTimePicker extends StatelessWidget { children: [ Center( child: Text( - date.getDaysOfWeek().elementAt(index).day.toString(), + date.daysOfWeek().elementAt(index).day.toString(), style: Theme.of(context).textTheme.bodyMedium!.copyWith( color: shouldHighlight(index) @@ -87,7 +89,7 @@ class WeekDateTimePicker extends StatelessWidget { } bool shouldHighlight(int index) { - return date.getDaysOfWeek().elementAt(index).isSameDayAs( + return date.daysOfWeek().elementAt(index).sameDayAs( dateTimePickerController.highlightToday ? DateTime.now() : dateTimePickerController.selectedDate, @@ -96,20 +98,20 @@ class WeekDateTimePicker extends StatelessWidget { bool isSelected(int index) { return date - .getDaysOfWeek() + .daysOfWeek() .elementAt(index) - .isSameDayAs(dateTimePickerController.selectedDate); + .sameDayAs(dateTimePickerController.selectedDate); } bool shouldMark(int index) { - return !date.getDaysOfWeek().elementAt(index).isSameDayAs( + return !date.daysOfWeek().elementAt(index).sameDayAs( dateTimePickerController.highlightToday ? DateTime.now() : dateTimePickerController.selectedDate, ) && date - .getDaysOfWeek() + .daysOfWeek() .elementAt(index) - .isDayPartOf(dateTimePickerController.markedDates ?? []); + .isContainedIn(dateTimePickerController.markedDates ?? []); } } diff --git a/lib/src/widgets/week_date_time_picker/week_date_time_picker_sheet.dart b/lib/src/widgets/week_date_time_picker/week_date_time_picker_sheet.dart index 3d40ed1..d6c5b48 100644 --- a/lib/src/widgets/week_date_time_picker/week_date_time_picker_sheet.dart +++ b/lib/src/widgets/week_date_time_picker/week_date_time_picker_sheet.dart @@ -16,7 +16,7 @@ class WeekDateTimePickerSheet extends StatelessWidget { String getDateHeader() { List weekDays = - dateTimePickerController.browsingDate.getDaysOfWeek(); + dateTimePickerController.browsingDate.daysOfWeek(); String firstDay = weekDays.first.day.toString();