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

Allow the use of generic type with Interface #149

Merged
merged 2 commits into from
Sep 24, 2019
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
32 changes: 29 additions & 3 deletions lib/classes/event.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:flutter/material.dart';

class Event {
class Event implements EventInterface {
final DateTime date;
final String title;
final Widget icon;
Expand All @@ -11,8 +11,7 @@ class Event {
this.title,
this.icon,
this.dot,
})
: assert(date != null);
}) : assert(date != null);

@override
bool operator ==(dynamic other) {
Expand All @@ -24,4 +23,31 @@ class Event {

@override
int get hashCode => hashValues(date, title, icon);

@override
DateTime getDate() {
return date;
}

@override
Widget getDot() {
return dot;
}

@override
Widget getIcon() {
return icon;
}

@override
String getTitle() {
return title;
}
}

abstract class EventInterface {
DateTime getDate();
String getTitle();
Widget getIcon();
Widget getDot();
}
16 changes: 8 additions & 8 deletions lib/flutter_calendar_carousel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import 'package:intl/date_symbol_data_local.dart';
import 'package:intl/intl.dart' show DateFormat;
export 'package:flutter_calendar_carousel/classes/event_list.dart';

typedef MarkedDateIconBuilder<Event> = Widget Function(Event event);
typedef MarkedDateIconBuilder<T> = Widget Function(T event);
typedef void OnDayLongPressed(DateTime day);


Expand Down Expand Up @@ -45,7 +45,7 @@ typedef Widget DayBuilder(
DateTime day
);

class CalendarCarousel<T> extends StatefulWidget {
class CalendarCarousel<T extends EventInterface> extends StatefulWidget {
final double viewportFraction;
final TextStyle prevDaysTextStyle;
final TextStyle daysTextStyle;
Expand All @@ -71,7 +71,7 @@ class CalendarCarousel<T> extends StatefulWidget {
final TextStyle headerTextStyle;
final String headerText;
final TextStyle weekendTextStyle;
final EventList<Event> markedDatesMap;
final EventList<T> markedDatesMap;
/// Change `makredDateWidget` when `markedDateShowIcon` is set to false.
final Widget markedDateWidget;
/// Change `ShapeBorder` when `markedDateShowIcon` is set to false.
Expand All @@ -86,7 +86,7 @@ class CalendarCarousel<T> extends StatefulWidget {
final int markedDateIconMaxShown;
final double markedDateIconMargin;
final double markedDateIconOffset;
final MarkedDateIconBuilder<Event> markedDateIconBuilder;
final MarkedDateIconBuilder<T> markedDateIconBuilder;
/// null - no indicator, true - show the total events, false - show the total of hidden events
final bool markedDateMoreShowTotal;
final Decoration markedDateMoreCustomDecoration;
Expand Down Expand Up @@ -210,7 +210,7 @@ enum WeekdayFormat {
standaloneNarrow,
}

class _CalendarState<T> extends State<CalendarCarousel<T>> {
class _CalendarState<T extends EventInterface> extends State<CalendarCarousel<T>> {
PageController _controller;
List<DateTime> _dates = List(3);
List<List<DateTime>> _weeks = List(3);
Expand Down Expand Up @@ -856,7 +856,7 @@ class _CalendarState<T> extends State<CalendarCarousel<T>> {
int eventIndex = 0;
double offset = 0.0;
double padding = widget.markedDateIconMargin;
widget.markedDatesMap.getEvents(now).forEach((Event event) {
widget.markedDatesMap.getEvents(now).forEach((T event) {
if (widget.markedDateShowIcon) {
if (tmp.length > 0 && tmp.length < widget.markedDateIconMaxShown) {
offset += widget.markedDateIconOffset;
Expand Down Expand Up @@ -920,8 +920,8 @@ class _CalendarState<T> extends State<CalendarCarousel<T>> {
if (widget.markedDateIconBuilder != null) {
tmp.add(widget.markedDateIconBuilder(event));
} else {
if (event.dot != null) {
tmp.add(event.dot);
if (event.getDot() != null) {
tmp.add(event.getDot());
}
else if (widget.markedDateWidget != null) {
tmp.add(widget.markedDateWidget);
Expand Down