Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple days selection #284

Merged
merged 5 commits into from
Jul 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions lib/classes/marked_date.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import 'package:flutter/material.dart';

class MarkedDate implements MarkedDateInterface {
final Color color;
final int? id;
final TextStyle? textStyle;
final DateTime date;

MarkedDate({
required this.color,
this.id,
this.textStyle,
required this.date,
});

@override
bool operator ==(dynamic other) {
return this.date == other.date &&
this.color == other.color &&
this.textStyle == other.textStyle &&
this.id == other.id;
}

@override
DateTime getDate() => this.date;

@override
int? getId() => this.id;

@override
Color getColor() => this.color;


@override
TextStyle? getTextStyle() => this.textStyle;
}




abstract class MarkedDateInterface {
DateTime getDate();
Color getColor();
int? getId();
TextStyle? getTextStyle();

}
48 changes: 48 additions & 0 deletions lib/classes/multiple_marked_dates.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

import 'marked_date.dart';
import 'package:flutter/material.dart';

class MultipleMarkedDates{
List<MarkedDate> markedDates;

MultipleMarkedDates({required this.markedDates});

void add(MarkedDate markedDate){
markedDates.add(markedDate);
}

void addAll(List<MarkedDate> markedDates){
this.markedDates.addAll(markedDates);
}

bool remove(MarkedDate markedDate){
return markedDates.remove(markedDate);
}

void clear() {
markedDates.clear();
}

bool isMarked(DateTime date){
final results = markedDates.firstWhere((element) => element.date == date, orElse: () => MarkedDate(color: Colors.black, date: DateTime(0)));
return results.date.year == date.year;
}

Color getColor(DateTime date){
final results = markedDates.firstWhere((element) => element.date == date, orElse: () => MarkedDate(color: Colors.black, date: DateTime(0)));
return results.color;
}

DateTime getDate(DateTime date){
final results = markedDates.firstWhere((element) => element.date == date, orElse: () => MarkedDate(color: Colors.black, date: DateTime(0)));
return results.date;
}

TextStyle? getTextStyle(DateTime date){
final results = markedDates.firstWhere((element) => element.date == date, orElse: () => MarkedDate(color: Colors.black, date: DateTime(0)));
return results.textStyle;
}



}
75 changes: 55 additions & 20 deletions lib/flutter_calendar_carousel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import 'package:flutter_calendar_carousel/src/weekday_row.dart';
import 'package:intl/date_symbol_data_local.dart';
import 'package:intl/intl.dart' show DateFormat;

import 'classes/multiple_marked_dates.dart';

export 'package:flutter_calendar_carousel/classes/event_list.dart';

typedef MarkedDateIconBuilder<T> = Widget? Function(T event);
Expand Down Expand Up @@ -112,6 +114,7 @@ class CalendarCarousel<T extends EventInterface> extends StatefulWidget {
final bool showWeekDays;
final bool showHeader;
final bool showHeaderButton;
final MultipleMarkedDates? multipleMarkedDates;
final Widget? leftButtonIcon;
final Widget? rightButtonIcon;
final ScrollPhysics? customGridViewPhysics;
Expand Down Expand Up @@ -181,6 +184,7 @@ class CalendarCarousel<T extends EventInterface> extends StatefulWidget {
this.markedDateCustomTextStyle,
this.markedDateMoreCustomTextStyle,
this.markedDateWidget,
this.multipleMarkedDates,
this.headerMargin = const EdgeInsets.symmetric(vertical: 16.0),
this.childAspectRatio = 1.0,
this.weekDayMargin = const EdgeInsets.only(bottom: 4.0),
Expand Down Expand Up @@ -415,6 +419,9 @@ class _CalendarState<T extends EventInterface>
bool isThisMonthDay,
DateTime now,
) {



return Container(
width: double.infinity,
height: double.infinity,
Expand All @@ -432,7 +439,8 @@ class _CalendarState<T extends EventInterface>
textStyle,
defaultTextStyle,
isNextMonthDay,
isThisMonthDay),
isThisMonthDay
),
child: Text(
'${now.day}',
semanticsLabel: now.day.toString(),
Expand All @@ -445,7 +453,8 @@ class _CalendarState<T extends EventInterface>
textStyle,
defaultTextStyle,
isNextMonthDay,
isThisMonthDay),
isThisMonthDay,
now),
maxLines: 1,
),
),
Expand All @@ -466,6 +475,13 @@ class _CalendarState<T extends EventInterface>
bool isThisMonthDay,
DateTime now,
) {

// If day is in Multiple selection mode, get its color
bool isMultipleMarked = widget.multipleMarkedDates?.isMarked(now) ?? false;
Color? multipleMarkedColor = widget.multipleMarkedDates?.getColor(now);



final markedDatesMap = widget.markedDatesMap;
return Container(
margin: EdgeInsets.all(widget.dayPadding),
Expand All @@ -476,7 +492,12 @@ class _CalendarState<T extends EventInterface>
? widget.selectedDayButtonColor
: isToday && widget.todayButtonColor != null
? widget.todayButtonColor
: widget.dayButtonColor,

// If day is in Multiple selection mode, apply a different color
: isMultipleMarked?
multipleMarkedColor
: widget.dayButtonColor,

onPressed: widget.disableDayPressed ? null : () => _onDayPressed(now),
padding: EdgeInsets.all(widget.dayPadding),
shape: widget.markedDateCustomShapeBorder != null &&
Expand Down Expand Up @@ -1080,23 +1101,25 @@ class _CalendarState<T extends EventInterface>
bool isThisMonthDay,
) {
return !isSelectable
? defaultInactiveDaysTextStyle
: (_localeDate.dateSymbols.WEEKENDRANGE
.contains((index - 1 + firstDayOfWeek) % 7)) &&
!isSelectedDay &&
!isToday
? (isPrevMonthDay
? defaultPrevDaysTextStyle
: isNextMonthDay
? defaultNextDaysTextStyle
: isSelectable
? defaultWeekendTextStyle
: defaultInactiveWeekendTextStyle)
: isToday
? defaultTodayTextStyle
: isSelectable && textStyle != null
? textStyle
: defaultTextStyle;
?
defaultInactiveDaysTextStyle
: (_localeDate.dateSymbols.WEEKENDRANGE
.contains((index - 1 + firstDayOfWeek) % 7)) &&
!isSelectedDay &&
!isToday
? (isPrevMonthDay
? defaultPrevDaysTextStyle
: isNextMonthDay
? defaultNextDaysTextStyle
: isSelectable
? defaultWeekendTextStyle
: defaultInactiveWeekendTextStyle)
:
isToday
? defaultTodayTextStyle
: isSelectable && textStyle != null
? textStyle
: defaultTextStyle;
}

TextStyle? getDayStyle(
Expand All @@ -1109,9 +1132,18 @@ class _CalendarState<T extends EventInterface>
TextStyle defaultTextStyle,
bool isNextMonthDay,
bool isThisMonthDay,
DateTime now
) {

// If day is in multiple selection get its style(if available)
bool isMultipleMarked = widget.multipleMarkedDates?.isMarked(now) ?? false;
TextStyle? mutipleMarkedTextStyle = widget.multipleMarkedDates?.getTextStyle(now);


return isSelectedDay && widget.selectedDayTextStyle != null
? widget.selectedDayTextStyle
: isMultipleMarked?
mutipleMarkedTextStyle
: (_localeDate.dateSymbols.WEEKENDRANGE
.contains((index - 1 + firstDayOfWeek) % 7)) &&
!isSelectedDay &&
Expand Down Expand Up @@ -1144,6 +1176,8 @@ class _CalendarState<T extends EventInterface>
DateTime now) {
final customDayBuilder = widget.customDayBuilder;



Widget? dayContainer;
if (customDayBuilder != null) {
final appTextStyle = DefaultTextStyle.of(context).style;
Expand All @@ -1158,6 +1192,7 @@ class _CalendarState<T extends EventInterface>
defaultTextStyle,
isNextMonthDay,
isThisMonthDay,
now,
);

final styleForBuilder = appTextStyle.merge(dayStyle);
Expand Down