Skip to content

Commit

Permalink
complete rowspan
Browse files Browse the repository at this point in the history
  • Loading branch information
snehmehta committed Oct 24, 2023
1 parent cbbfb65 commit cf19495
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 12 deletions.
39 changes: 36 additions & 3 deletions assets/schema/ensemble_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -3907,7 +3907,9 @@
"updateBadgeCount": {
"type": "object",
"description": "Update the app's badge count (applicable on iOS, MacOS, and some Android devices only). Notifications must also be enabled for this to work properly.",
"required": ["count"],
"required": [
"count"
],
"properties": {
"count": {
"type": "integer",
Expand Down Expand Up @@ -5236,6 +5238,37 @@
"type": "boolean",
"description": "Set visibility of calendar header. Default true."
},
"rowSpans": {
"type": "array",
"items": {
"type": "object",
"properties": {
"span": {
"type": "object",
"properties": {
"start": {
"type": "string",
"description": "date in ISO format."
},
"end": {
"type": "string",
"description": "date in ISO format."
},
"widget": {
"$ref": "#/$defs/Widget"
}
},
"required": ["start", "end", "widget"]
}
},
"required": ["span"]
}
},
"headerTextStyle": {
"type": "object",
"description": "Styling the header text.",
"$ref": "#/$defs/TextStyle"
},
"cell": {
"allOf": [
{
Expand All @@ -5247,7 +5280,7 @@
"properties": {
"onTap": {
"$ref": "#/$defs/Action-payload",
"description": "Execute an Action when the map's bound has changed. The bound data is available using `event.data.bounds.<southwest/northeast>.<lat/lng>`."
"description": "Execute an Action when the map's bound has changed. The bound data is available using `event.data.bounds.<southwest/northeast>.<lat/lng>`."
}
}
}
Expand All @@ -5271,7 +5304,7 @@
},
"range": {
"$ref": "#/$defs/Range-payload"
}
}
}
}
}
Expand Down
80 changes: 72 additions & 8 deletions lib/widget/calendar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,17 @@ class EnsembleCalendar extends StatefulWidget
_controller.headerVisible = Utils.getBool(value, fallback: true),
'firstDay': (value) => _controller.firstDay = Utils.getDate(value),
'lastDay': (value) => _controller.lastDay = Utils.getDate(value),
'rowSpan': (value) => setRowSpan(value),
'rowSpans': (value) {
if (value is List) {
for(var data in value) {
setRowSpan(data['span']);
}
} else {
setRowSpan(value);
}
},
'headerTextStyle': (value) =>
_controller.headerTextStyle = Utils.getTextStyle(value),
};
}

Expand Down Expand Up @@ -189,12 +199,11 @@ class EnsembleCalendar extends StatefulWidget
void setRowSpan(dynamic data) {
if (data is YamlMap) {
final rowSpan = RowSpanConfig(
startDay: Utils.getDate(data['startDay']),
endDay: Utils.getDate(data['endDay']),
backgroundColor: Utils.getColor(data['backgroundColor']),
widget: data['child'],
startDay: Utils.getDate(data['start']),
endDay: Utils.getDate(data['end']),
widget: data['widget'],
);
_controller.rowSpanConfig = rowSpan;
_controller.rowSpans.add(rowSpan);
}
}

Expand Down Expand Up @@ -303,7 +312,8 @@ class CalendarController extends WidgetController {
DateTime? firstDay;
DateTime? lastDay;
final ValueNotifier<DateTime> focusedDay = ValueNotifier(DateTime.now());
RowSpanConfig? rowSpanConfig;
List<RowSpanConfig> rowSpans = [];
TextStyle? headerTextStyle;

final ValueNotifier<Set<DateTime>> markedDays = ValueNotifier(
LinkedHashSet<DateTime>(
Expand Down Expand Up @@ -420,6 +430,7 @@ class CalendarState extends WidgetState<EnsembleCalendar> {
builder: (context, value, _) {
return _CalendarHeader(
focusedDay: value,
headerStyle: widget._controller.headerTextStyle,
onLeftArrowTap: () {
widget._controller.pageController?.previousPage(
duration: const Duration(milliseconds: 300),
Expand Down Expand Up @@ -457,7 +468,24 @@ class CalendarState extends WidgetState<EnsembleCalendar> {
widget._controller.focusedDay.value = focusedDay,
rowHeight: widget._controller.rowHeight,
daysOfWeekVisible: true,
overlayRanges: getOverlayRange(),
calendarBuilders: CalendarBuilders(
overlayBuilder: widget._controller.rowSpans.isEmpty
? null
: (context, range) {
final spans = widget._controller.rowSpans;
for (var span in spans) {
if (span.startDay == null || span.endDay == null) {
return const SizedBox.shrink();
}
if (DateTimeRange(
start: span.startDay!, end: span.endDay!) ==
range) {
return widgetBuilder(context, span.widget, {});
}
}
return const SizedBox.shrink();
},
disabledBuilder: (context, day, focusedDay) {
return cellBuilder(
context, day, focusedDay, widget._controller.disableCell);
Expand Down Expand Up @@ -537,6 +565,17 @@ class CalendarState extends WidgetState<EnsembleCalendar> {
return null;
},
todayBuilder: (context, day, focusedDay) {
if (widget._controller.rangeSelectionMode ==
RangeSelectionMode.toggledOn) {
final isWithinRange = widget._controller.rangeStart != null &&
widget._controller.rangeEnd != null &&
_isWithinRange(day, widget._controller.rangeStart!,
widget._controller.rangeEnd!);
if (isWithinRange) {
return cellBuilder(context, day, focusedDay,
widget._controller.rangeBetweenCell);
}
}
return cellBuilder(
context, day, focusedDay, widget._controller.todayCell);
},
Expand All @@ -554,6 +593,29 @@ class CalendarState extends WidgetState<EnsembleCalendar> {
);
}

List<DateTimeRange> getOverlayRange() {
final overlayRange = <DateTimeRange>[];
for (var span in widget._controller.rowSpans) {
if (span.endDay != null && span.startDay != null) {
overlayRange
.add(DateTimeRange(start: span.startDay!, end: span.endDay!));
}
}
return overlayRange;
}

bool _isWithinRange(DateTime day, DateTime start, DateTime end) {
if (isSameDay(day, start) || isSameDay(day, end)) {
return true;
}

if (day.isAfter(start) && day.isBefore(end)) {
return true;
}

return false;
}

Widget? widgetBuilder(
BuildContext context, dynamic item, Map<String, dynamic> data) {
ScopeManager? parentScope = DataScopeWidget.getScope(context);
Expand Down Expand Up @@ -607,6 +669,7 @@ class CalendarState extends WidgetState<EnsembleCalendar> {
}

class _CalendarHeader extends StatelessWidget {
final TextStyle? headerStyle;
final DateTime focusedDay;
final VoidCallback onLeftArrowTap;
final VoidCallback onRightArrowTap;
Expand All @@ -616,6 +679,7 @@ class _CalendarHeader extends StatelessWidget {
required this.focusedDay,
required this.onLeftArrowTap,
required this.onRightArrowTap,
this.headerStyle,
}) : super(key: key);

@override
Expand All @@ -629,7 +693,7 @@ class _CalendarHeader extends StatelessWidget {
const SizedBox(width: 16.0),
Text(
headerText,
style: const TextStyle(fontSize: 26.0),
style: headerStyle ?? const TextStyle(fontSize: 26.0),
),
const Spacer(),
IconButton(
Expand Down
6 changes: 5 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,11 @@ dependencies:
app_links: ^3.4.3
share_plus: ^6.2.0
rate_my_app: ^2.0.0
table_calendar: ^3.0.9
table_calendar:
git:
url: https://github.com/snehmehta/table_calendar
ref: master

flutter_app_badger: ^1.5.0

dev_dependencies:
Expand Down

0 comments on commit cf19495

Please sign in to comment.