diff --git a/CHANGELOG.md b/CHANGELOG.md index 9efeb7c..9ca0e86 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## 2.2.1 +- Added marking to datetimepicker + ## 2.2.0 - Added the abilty to add the weekday letters above the months diff --git a/example/lib/main.dart b/example/lib/main.dart index 500a1b5..e402d6e 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -84,12 +84,14 @@ class DatePickerDemo extends StatelessWidget { mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [ OverlayDateTimePicker( + markedDates: [DateTime.now().add(const Duration(days: 1))], theme: dateTimePickerTheme, alignment: Alignment.bottomCenter, child: const Text("Select Day"), onTapDay: (date) {}, ), OverlayDateTimePicker( + markedDates: [DateTime.now().add(const Duration(days: 3))], theme: dateTimePickerTheme, alignment: Alignment.center, buttonBuilder: (key, onPressed) => TextButton( @@ -160,7 +162,7 @@ class DatePickerDemo extends StatelessWidget { barOpacity: 1, ), ), - markedDates: [DateTime(2022, 9, 6)], + markedDates: [DateTime.now().subtract(const Duration(days: 1))], ), ], ), diff --git a/example/pubspec.lock b/example/pubspec.lock index 83eeb68..8eaa6be 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -61,7 +61,7 @@ packages: path: ".." relative: true source: path - version: "2.1.0" + version: "2.2.0" flutter_lints: dependency: "direct dev" description: diff --git a/lib/src/widgets/marked_icon.dart b/lib/src/widgets/marked_icon.dart new file mode 100644 index 0000000..a00a763 --- /dev/null +++ b/lib/src/widgets/marked_icon.dart @@ -0,0 +1,31 @@ +import 'package:flutter/material.dart'; + +class MarkedIcon extends StatelessWidget { + const MarkedIcon({ + super.key, + this.color, + required this.width, + required this.height, + }); + + final double width; + final double height; + final Color? color; + + @override + Widget build(BuildContext context) { + return Align( + alignment: Alignment.topRight, + child: IgnorePointer( + child: Container( + width: width, + height: height, + decoration: BoxDecoration( + color: color ?? Theme.of(context).indicatorColor, + borderRadius: BorderRadius.circular((width) * 2), + ), + ), + ), + ); + } +} diff --git a/lib/src/widgets/month_date_time_picker/month_date_time_picker.dart b/lib/src/widgets/month_date_time_picker/month_date_time_picker.dart index e3c223e..262e672 100644 --- a/lib/src/widgets/month_date_time_picker/month_date_time_picker.dart +++ b/lib/src/widgets/month_date_time_picker/month_date_time_picker.dart @@ -9,6 +9,7 @@ import 'package:flutter_date_time_picker/src/extensions/time_of_day.dart'; import 'package:flutter_date_time_picker/src/models/date_box_current_theme.dart'; import 'package:flutter_date_time_picker/src/models/date_time_picker_theme.dart'; import 'package:flutter_date_time_picker/src/utils/date_time_picker_controller.dart'; +import 'package:flutter_date_time_picker/src/widgets/marked_icon.dart'; import 'package:intl/intl.dart'; class MonthDateTimePicker extends StatelessWidget { @@ -150,21 +151,11 @@ class MonthDateTimePicker extends StatelessWidget { addedIndex, daysToSkip, )) ...[ - Align( - alignment: Alignment.bottomRight, - child: IgnorePointer( - child: Container( - width: monthDateBoxSize / 4, - height: monthDateBoxSize / 4, - decoration: BoxDecoration( - color: dateTimePickerController - .theme.markedIndicatorColor ?? - Theme.of(context).indicatorColor, - borderRadius: BorderRadius.circular( - (monthDateBoxSize / 4) * 2), - ), - ), - ), + MarkedIcon( + width: monthDateBoxSize / 4, + height: monthDateBoxSize / 4, + color: dateTimePickerController + .theme.markedIndicatorColor, ), ], ], diff --git a/lib/src/widgets/overlay_date_time_picker/date_picker.dart b/lib/src/widgets/overlay_date_time_picker/date_picker.dart index 9bb4fbd..ab12fab 100644 --- a/lib/src/widgets/overlay_date_time_picker/date_picker.dart +++ b/lib/src/widgets/overlay_date_time_picker/date_picker.dart @@ -86,6 +86,10 @@ class DatePicker extends StatelessWidget { return Padding( padding: const EdgeInsets.all(2.0), child: PickableDate( + isMarked: controller.markedDates?.any( + (e) => isSameDay(e, todayDate), + ) ?? + false, isOffMonth: date.month != todayDate.month, isDisabled: isDisabled(addedIndex + index, daysToSkip, todayDate), @@ -106,11 +110,12 @@ class DatePicker extends StatelessWidget { bool isToday(DateTime date) { DateTime now = DateTime.now(); - return date.year == now.year && - date.month == now.month && - date.day == now.day; + return isSameDay(date, now); } + bool isSameDay(DateTime a, DateTime b) => + a.year == b.year && a.month == b.month && a.day == b.day; + bool isDisabled(int index, int daysToSkip, DateTime date) { return DateTime( date.year, diff --git a/lib/src/widgets/overlay_date_time_picker/pickable_date.dart b/lib/src/widgets/overlay_date_time_picker/pickable_date.dart index e40a432..217e453 100644 --- a/lib/src/widgets/overlay_date_time_picker/pickable_date.dart +++ b/lib/src/widgets/overlay_date_time_picker/pickable_date.dart @@ -4,10 +4,12 @@ import 'package:flutter/material.dart'; import 'package:flutter_date_time_picker/flutter_date_time_picker.dart'; +import 'package:flutter_date_time_picker/src/widgets/marked_icon.dart'; class PickableDate extends StatelessWidget { const PickableDate({ super.key, + required this.isMarked, required this.isSelected, required this.isDisabled, required this.theme, @@ -17,6 +19,7 @@ class PickableDate extends StatelessWidget { required this.isToday, }); + final bool isMarked; final bool isSelected; final bool isDisabled; final bool isToday; @@ -33,26 +36,37 @@ class PickableDate extends StatelessWidget { if (isDisabled) return; onPressed.call(date); }, - child: Container( - decoration: BoxDecoration( - color: getColor( - isToday, - isSelected, - ), - borderRadius: getBorder(theme.dateBoxShape), - ), - child: Center( - child: Opacity( - opacity: (isDisabled || isOffMonth) ? 0.5 : 1, - child: Text( - date.day.toString(), - style: getStyle( + child: Stack( + children: [ + Container( + decoration: BoxDecoration( + color: getColor( isToday, isSelected, ), + borderRadius: getBorder(theme.dateBoxShape), + ), + child: Center( + child: Opacity( + opacity: (isDisabled || isOffMonth) ? 0.5 : 1, + child: Text( + date.day.toString(), + style: getStyle( + isToday, + isSelected, + ), + ), + ), ), ), - ), + if (isMarked) ...[ + MarkedIcon( + color: theme.markedIndicatorColor, + width: theme.monthDateBoxSize / 4, + height: theme.monthDateBoxSize / 4, + ), + ], + ], ), ); } 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 a5b9101..fb34fc5 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 @@ -8,6 +8,7 @@ import 'package:flutter_date_time_picker/src/extensions/date_time.dart'; import 'package:flutter_date_time_picker/src/extensions/time_of_day.dart'; import 'package:flutter_date_time_picker/src/models/date_box_current_theme.dart'; import 'package:flutter_date_time_picker/src/utils/date_time_picker_controller.dart'; +import 'package:flutter_date_time_picker/src/widgets/marked_icon.dart'; import 'package:intl/intl.dart'; class WeekDateTimePicker extends StatelessWidget { @@ -107,20 +108,12 @@ class WeekDateTimePicker extends StatelessWidget { ), ), if (shouldMark(index)) ...[ - Align( - alignment: Alignment.bottomRight, - child: Container( - width: weekDateBoxSize / 3, - height: weekDateBoxSize / 3, - decoration: BoxDecoration( - color: dateTimePickerController - .theme.markedIndicatorColor ?? - Theme.of(context).indicatorColor, - borderRadius: - BorderRadius.circular(weekDateBoxSize * 2), - ), - ), - ), + MarkedIcon( + width: weekDateBoxSize / 3, + height: weekDateBoxSize / 3, + color: dateTimePickerController + .theme.markedIndicatorColor, + ) ], ], ), diff --git a/pubspec.yaml b/pubspec.yaml index c3d6981..3484419 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: flutter_date_time_picker description: A Flutter package for date and time picker. -version: 2.2.0 +version: 2.2.1 homepage: https://iconica.nl/ environment: