feat: change calendar cell rendering with new design

This commit is contained in:
Freek van de Ven 2024-07-17 11:57:35 +02:00 committed by FlutterJoey
parent 70f8ac1db0
commit 62a130e3d6
2 changed files with 84 additions and 51 deletions

View file

@ -183,6 +183,7 @@ List<CalendarDay> _mapAvailabilitiesToCalendarDays(
: null, : null,
templateDeviation: templateIsDeviated, templateDeviation: templateIsDeviated,
isSelected: false, isSelected: false,
hasAvailability: true,
); );
}, },
).toList() ?? ).toList() ??

View file

@ -69,52 +69,87 @@ class CalendarGrid extends StatelessWidget {
mainAxisSpacing: 12, mainAxisSpacing: 12,
), ),
itemBuilder: (context, index) { itemBuilder: (context, index) {
// TODO(Joey): Extract all this as a widget
var day = calendarDays[index]; var day = calendarDays[index];
var dayColor = day.color ?? return _CalendarDayTile(
colors.customAvailabilityColor ?? day: day,
colorScheme.secondary; onDayTap: onDayTap,
var textColor = day.outsideMonth && !day.isSelected
? colors.outsideMonthTextColor ?? colorScheme.onSurface
: _getTextColor(
dayColor,
colors.textLightColor ?? Colors.white,
colors.textDarkColor,
); );
var textStyle = textTheme.bodyLarge?.copyWith(color: textColor); },
),
],
);
}
}
// TODO(Joey): Watch out for using gesture detectors class _CalendarDayTile extends StatelessWidget {
return GestureDetector( const _CalendarDayTile({
required this.day,
required this.onDayTap,
});
final CalendarDay day;
final void Function(DateTime) onDayTap;
@override
Widget build(BuildContext context) {
var theme = Theme.of(context);
var textTheme = theme.textTheme;
var colorScheme = theme.colorScheme;
var availabilityScope = AvailabilityScope.of(context);
var options = availabilityScope.options;
var colors = options.colors;
var dayColor =
day.color ?? colors.customAvailabilityColor ?? colorScheme.secondary;
Color? textColor;
TextStyle? textStyle;
if (day.outsideMonth) {
textColor = colors.outsideMonthTextColor ?? colorScheme.onSurface;
textStyle = textTheme.bodyMedium?.copyWith(color: textColor);
} else if (day.hasAvailability) {
textColor = dayColor;
textStyle = textTheme.titleMedium?.copyWith(color: textColor);
}
var decoration = day.outsideMonth
? null
: BoxDecoration(
border: Border(
bottom: BorderSide(
color: textColor ?? Colors.transparent,
width: 1,
),
),
);
return InkWell(
onTap: () => onDayTap(day.date), onTap: () => onDayTap(day.date),
child: DecoratedBox( child: DecoratedBox(
decoration: BoxDecoration( decoration: BoxDecoration(
color: dayColor,
borderRadius: BorderRadius.circular(5), borderRadius: BorderRadius.circular(5),
border: Border.all( border: Border.all(
color: day.isSelected && !day.outsideMonth color: day.isSelected ? theme.dividerColor : Colors.transparent,
? colorScheme.primary width: 1.5,
: Colors.transparent,
), ),
), ),
child: Stack( child: Stack(
children: [ children: [
Center( Center(
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 4),
decoration: decoration,
child: Text(day.date.day.toString(), style: textStyle), child: Text(day.date.day.toString(), style: textStyle),
), ),
if (day.templateDeviation) ...[ ),
if (day.templateDeviation)
Positioned( Positioned(
right: 4, right: 4,
child: Text("*", style: textStyle), child: Text("*", style: textStyle),
), ),
], ],
],
), ),
), ),
); );
},
),
],
);
} }
} }
@ -151,6 +186,7 @@ class CalendarDay {
required this.isSelected, required this.isSelected,
required this.color, required this.color,
required this.templateDeviation, required this.templateDeviation,
required this.hasAvailability,
this.outsideMonth = false, this.outsideMonth = false,
}); });
@ -171,6 +207,9 @@ class CalendarDay {
/// Whether the day is outside of the current month /// Whether the day is outside of the current month
final bool outsideMonth; final bool outsideMonth;
/// Whether the day has availability
final bool hasAvailability;
/// Creates a copy of the current day with the provided values /// Creates a copy of the current day with the provided values
CalendarDay copyWith({ CalendarDay copyWith({
DateTime? date, DateTime? date,
@ -178,6 +217,7 @@ class CalendarDay {
Color? color, Color? color,
bool? templateDeviation, bool? templateDeviation,
bool? outsideMonth, bool? outsideMonth,
bool? hasAvailability,
}) => }) =>
CalendarDay( CalendarDay(
date: date ?? this.date, date: date ?? this.date,
@ -185,6 +225,7 @@ class CalendarDay {
color: color ?? this.color, color: color ?? this.color,
templateDeviation: templateDeviation ?? this.templateDeviation, templateDeviation: templateDeviation ?? this.templateDeviation,
outsideMonth: outsideMonth ?? this.outsideMonth, outsideMonth: outsideMonth ?? this.outsideMonth,
hasAvailability: hasAvailability ?? this.hasAvailability,
); );
} }
@ -226,6 +267,7 @@ List<CalendarDay> _generateCalendarDays(
: Colors.transparent, : Colors.transparent,
templateDeviation: false, templateDeviation: false,
outsideMonth: true, outsideMonth: true,
hasAvailability: false,
), ),
); );
} }
@ -247,17 +289,14 @@ List<CalendarDay> _generateCalendarDays(
isSelected: false, isSelected: false,
color: colors.blankDayColor ?? colorScheme.surfaceDim, color: colors.blankDayColor ?? colorScheme.surfaceDim,
templateDeviation: false, templateDeviation: false,
hasAvailability: false,
), ),
); );
var dayIsSelected = selectedRange != null && var dayIsSelected = selectedRange != null &&
!day.isBefore(selectedRange.start) && !day.isBefore(selectedRange.start) &&
!day.isAfter(selectedRange.end); !day.isAfter(selectedRange.end);
specialDay = specialDay.copyWith( specialDay = specialDay.copyWith(
color: dayIsSelected
? colors.selectedDayColor ?? colorScheme.primaryFixedDim
: specialDay.color,
isSelected: dayIsSelected, isSelected: dayIsSelected,
templateDeviation: !dayIsSelected && specialDay.templateDeviation,
); );
calendarDays.add(specialDay); calendarDays.add(specialDay);
} }
@ -267,10 +306,3 @@ List<CalendarDay> _generateCalendarDays(
return calendarDays; return calendarDays;
} }
Color? _getTextColor(
Color backgroundColor,
Color lightColor,
Color? darkColor,
) =>
backgroundColor.computeLuminance() > 0.5 ? darkColor : lightColor;