Skip to content

Commit

Permalink
Allow the use of generic type with Interface (#149)
Browse files Browse the repository at this point in the history
* created interface EventInterface
   ```
   created EventInterface with methods: DateTime getDate()
   String getTitle()
   Widget getIcon()
   Widget getDot()
   ```
   Overrided method in class Event, now implements EventInterface
* Replaced Event with T extends EventInterface
   ```
   replaced all class Event with T
   now we can use any class that implements the interface
   ```
  • Loading branch information
Valitutto authored and hyochan committed Sep 24, 2019
1 parent 916310f commit 0d61cc1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
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

0 comments on commit 0d61cc1

Please sign in to comment.