fix: show selected days outside of the current month with the same color

This commit is contained in:
Freek van de Ven 2024-07-09 21:23:32 +02:00 committed by Bart Ribbers
parent ff7ab2bc42
commit 4a9bd72eca

View file

@ -76,7 +76,7 @@ class CalendarGrid extends StatelessWidget {
), ),
itemBuilder: (context, index) { itemBuilder: (context, index) {
var day = calendarDays[index]; var day = calendarDays[index];
var textColor = day.outsideMonth var textColor = day.outsideMonth && !day.isSelected
? colors.outsideMonthTextColor ?? colorScheme.onSurface ? colors.outsideMonthTextColor ?? colorScheme.onSurface
: _getTextColor( : _getTextColor(
day.color, day.color,
@ -89,10 +89,10 @@ class CalendarGrid extends StatelessWidget {
onTap: () => onDayTap(day.date), onTap: () => onDayTap(day.date),
child: DecoratedBox( child: DecoratedBox(
decoration: BoxDecoration( decoration: BoxDecoration(
color: day.outsideMonth ? Colors.transparent : day.color, color: day.color,
borderRadius: BorderRadius.circular(5), borderRadius: BorderRadius.circular(5),
border: Border.all( border: Border.all(
color: day.isSelected color: day.isSelected && !day.outsideMonth
? colorScheme.primary ? colorScheme.primary
: Colors.transparent, : Colors.transparent,
), ),
@ -178,21 +178,35 @@ List<CalendarDay> _generateCalendarDays(
var calendarDays = <CalendarDay>[]; var calendarDays = <CalendarDay>[];
// Add days from the previous month void addOutsideMonthDays(
for (var i = 0; i < startWeekday - 1; i++) { DateTime startDay,
var prevDay = int count, {
firstDayOfMonth.subtract(Duration(days: startWeekday - 1 - i)); required bool isNextMonth,
calendarDays.add( }) {
CalendarDay( for (var i = 0; i < count; i++) {
date: prevDay, var day = isNextMonth
isSelected: false, ? startDay.add(Duration(days: i + 1))
color: Colors.transparent, : startDay.subtract(Duration(days: count - i));
templateDeviation: false, var isSelected = selectedRange != null &&
outsideMonth: true, !day.isBefore(selectedRange.start) &&
), !day.isAfter(selectedRange.end);
); calendarDays.add(
CalendarDay(
date: day,
isSelected: isSelected,
color: isSelected
? colors.selectedDayColor ?? colorScheme.primaryFixedDim
: Colors.transparent,
templateDeviation: false,
outsideMonth: true,
),
);
}
} }
// Add days from the previous month
addOutsideMonthDays(firstDayOfMonth, startWeekday - 1, isNextMonth: false);
// Add days of the current month // Add days of the current month
for (var i = 1; i <= daysInMonth; i++) { for (var i = 1; i <= daysInMonth; i++) {
var day = DateTime(month.year, month.month, i); var day = DateTime(month.year, month.month, i);
@ -211,29 +225,18 @@ List<CalendarDay> _generateCalendarDays(
var dayIsSelected = selectedRange != null && var dayIsSelected = selectedRange != null &&
!day.isBefore(selectedRange.start) && !day.isBefore(selectedRange.start) &&
!day.isAfter(selectedRange.end); !day.isAfter(selectedRange.end);
// if the day is selected we need to change the color and remove the marking
specialDay = specialDay.copyWith( specialDay = specialDay.copyWith(
color: dayIsSelected color: dayIsSelected
? colors.selectedDayColor ?? colorScheme.primaryFixedDim ? colors.selectedDayColor ?? colorScheme.primaryFixedDim
: null, : specialDay.color,
templateDeviation: dayIsSelected ? false : null, isSelected: dayIsSelected,
templateDeviation: !dayIsSelected && specialDay.templateDeviation,
); );
calendarDays.add(specialDay); calendarDays.add(specialDay);
} }
// Add days from the next month // Add days from the next month
for (var i = endWeekday; i < 7; i++) { addOutsideMonthDays(lastDayOfMonth, 7 - endWeekday, isNextMonth: true);
var nextDay = lastDayOfMonth.add(Duration(days: i - endWeekday + 1));
calendarDays.add(
CalendarDay(
date: nextDay,
isSelected: false,
color: Colors.transparent,
templateDeviation: false,
outsideMonth: true,
),
);
}
return calendarDays; return calendarDays;
} }