Skip to content

Commit 455336d

Browse files
authored
Merge pull request #1 from codewave-tech/feature/event-handling
Feature/event handling
2 parents b24a554 + 301feda commit 455336d

5 files changed

Lines changed: 103 additions & 16 deletions

File tree

lib/src/calendar_view/calendar_view.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
library calendar_view;
22

33
// import 'package:dotted_border/dotted_border.dart';
4+
import 'dart:math';
5+
46
import 'package:flutter/material.dart';
7+
import 'package:intl/intl.dart';
58

69
part 'controller.dart';
710
part 'data.dart';

lib/src/calendar_view/controller.dart

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
part of calendar_view;
22

33
class CalendarViewController {
4+
// 2D month array data
45
late List<List<CalendarElemet?>> _monthArr;
56

67
late DateTime _displayMonth;
@@ -124,6 +125,7 @@ class CalendarViewController {
124125
_calendarViewStateUpdator?.call(() {});
125126
}
126127

128+
// here we generate the 2D array which contains month elements
127129
void _generateMonthArr() {
128130
DateTime firstDayOfTheMonth =
129131
DateTime(_displayMonth.year, _displayMonth.month, 1);
@@ -151,6 +153,10 @@ class CalendarViewController {
151153
}
152154
}
153155

156+
// builds individual month elements goes from index 0 to 42
157+
// when idx < 7 := we need to build the day
158+
// and for the rest fill the elements of 2D array with monday day
159+
// and the leading and trailing is filled as well
154160
Widget _buildMonthElemet(int index) {
155161
if (index < 7) {
156162
return SizedBox(
@@ -187,6 +193,12 @@ class CalendarViewController {
187193
return CalendarViewWidgets.isLeadingOrTrailingElement(calendarElemet);
188194
}
189195

196+
// render event
197+
if (_eventExistsForDay(dateTime)) {
198+
RangeMasonCalendarEvent event = _getEvent(dateTime);
199+
return CalendarViewWidgets.event(event);
200+
}
201+
190202
if (!_rangeSelectionMode && _isEqual(dateTime, _singleDateSelectionDate)) {
191203
return CalendarViewWidgets.selectedDate(calendarElemet);
192204
}
@@ -198,6 +210,7 @@ class CalendarViewController {
198210
);
199211
}
200212

213+
// range selection mode check
201214
if (_rangeSelectionMode &&
202215
(_isEqual(dateTime, _rangeStartDate) ||
203216
_isEqual(dateTime, _rangeEndDate))) {
@@ -270,4 +283,31 @@ class CalendarViewController {
270283
return 1;
271284
}
272285
}
286+
287+
// calendar event functionality
288+
289+
// event map
290+
final Map<String, RangeMasonCalendarEvent> _eventMap = {};
291+
292+
bool _eventExistsForDay(DateTime dateTime) {
293+
return _eventMap.containsKey(_generateEventMapKey(dateTime));
294+
}
295+
296+
RangeMasonCalendarEvent _getEvent(DateTime dateTime) {
297+
return _eventMap[_generateEventMapKey(dateTime)]!;
298+
}
299+
300+
String _generateEventMapKey(DateTime dateTime) {
301+
return DateFormat("dd MMM yyyy").format(dateTime);
302+
}
303+
304+
void addEvent(RangeMasonCalendarEvent event) {
305+
_eventMap[_generateEventMapKey(event.dateTime)] = event;
306+
}
307+
308+
void addEvents(List<RangeMasonCalendarEvent> events) {
309+
for (int idx = 0; idx < events.length; idx++) {
310+
addEvent(events[idx]);
311+
}
312+
}
273313
}

lib/src/calendar_view/data_modelling.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,15 @@ typedef CalendarViewStateUpdator = void Function(void Function() fn);
2929
typedef CalendarClick = void Function(CalendarElemet calendarElemet);
3030
typedef CalendarHover = void Function(
3131
CalendarElemet calendarElemet, bool value);
32+
33+
class RangeMasonCalendarEvent {
34+
final DateTime dateTime;
35+
final TextStyle textStyle;
36+
final BoxDecoration boxDecoration;
37+
38+
RangeMasonCalendarEvent({
39+
required this.dateTime,
40+
required this.textStyle,
41+
required this.boxDecoration,
42+
});
43+
}

lib/src/calendar_view/widgets.dart

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,4 +188,27 @@ class CalendarViewWidgets {
188188
)),
189189
);
190190
}
191+
192+
// for events
193+
static Widget event(RangeMasonCalendarEvent event) {
194+
return Container(
195+
height: 24,
196+
width: 24,
197+
decoration: event.boxDecoration,
198+
// BoxDecoration(
199+
// borderRadius: BorderRadius.circular(4),
200+
// color: isSelectable
201+
// ? _colorScheme.possibleSelectionColor
202+
// : _colorScheme.selectedColor,
203+
// ),
204+
margin: const EdgeInsets.all(4),
205+
child: Center(
206+
child: Text(
207+
'${event.dateTime.day}',
208+
style: event.textStyle,
209+
maxLines: 1,
210+
overflow: TextOverflow.ellipsis,
211+
)),
212+
);
213+
}
191214
}

lib/src/date_selector/date_selector.dart

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ import '../calendar_view/calendar_view.dart';
1111
// part 'junky_logic.dart';
1212
// part 'range_selector_logic.dart';
1313

14-
typedef BottomBarSelectorBuilder = Widget Function(
15-
CalendarViewController logic,
16-
BuildContext context,
17-
);
14+
typedef ControllerCreationCallBack = void Function(
15+
CalendarViewController controller);
16+
17+
typedef OnCalendarUpdate = void Function(int month, int year);
1818

1919
class DateSelector extends StatefulWidget {
2020
final double width;
@@ -23,16 +23,20 @@ class DateSelector extends StatefulWidget {
2323
final Color dividerColor;
2424
final TextStyle? headerTextStyle;
2525
final CalendarColorScheme? colorScheme;
26-
final BottomBarSelectorBuilder? bottomBar;
26+
final Widget? bottomBar;
27+
final CalendarViewController controller;
28+
final OnCalendarUpdate? onCalendarUpdate;
2729
const DateSelector({
2830
super.key,
31+
required this.controller,
2932
this.width = 250,
3033
this.padding,
3134
this.crossAxisPadding = 12,
3235
this.dividerColor = Colors.grey,
3336
this.headerTextStyle,
3437
this.colorScheme,
3538
this.bottomBar,
39+
this.onCalendarUpdate,
3640
});
3741

3842
@override
@@ -44,10 +48,10 @@ class _DateSelectorState extends State<DateSelector> {
4448
late double calendarViewWidth;
4549

4650
// final RangeSelectorLogic logic = RangeSelectorLogic();
47-
final CalendarViewController controller = CalendarViewController(
48-
id: "data_selector",
49-
enableRangeSelectionMode: false,
50-
);
51+
// final CalendarViewController controller = CalendarViewController(
52+
// id: "data_selector",
53+
// enableRangeSelectionMode: false,
54+
// );
5155

5256
late ValueNotifier<String> calendarNotifier;
5357

@@ -59,19 +63,25 @@ class _DateSelectorState extends State<DateSelector> {
5963
maxWidth = widget.width + widget.crossAxisPadding;
6064
calendarViewWidth = maxWidth - 40;
6165

62-
calendarNotifier = ValueNotifier<String>(_format(controller.displayDate!));
66+
calendarNotifier =
67+
ValueNotifier<String>(_format(widget.controller.displayDate!));
68+
69+
// widget.onControllerCreated?.call(controller);
70+
6371
super.initState();
6472
}
6573

6674
void _updateCalendarDateDisplay() {
67-
calendarNotifier.value = _format(controller.displayDate!);
75+
calendarNotifier.value = _format(widget.controller.displayDate!);
76+
DateTime dateTime = widget.controller.displayDate!;
77+
widget.onCalendarUpdate?.call(dateTime.month, dateTime.year);
6878
}
6979

7080
@override
7181
Widget build(BuildContext context) {
7282
return Container(
7383
decoration: BoxDecoration(
74-
borderRadius: BorderRadius.circular(4),
84+
borderRadius: BorderRadius.circular(8),
7585
color: Colors.white,
7686
),
7787
padding: widget.padding,
@@ -81,22 +91,21 @@ class _DateSelectorState extends State<DateSelector> {
8191
crossAxisAlignment: CrossAxisAlignment.center,
8292
children: [
8393
_calendarControls(
84-
controller: controller,
94+
controller: widget.controller,
8595
notifier: calendarNotifier,
8696
),
8797
Divider(
8898
color: widget.dividerColor,
8999
height: 0,
90100
),
91101
const SizedBox(height: 7),
92-
_calendarView(controller),
102+
_calendarView(widget.controller),
93103
const SizedBox(height: 7),
94104
Divider(
95105
color: widget.dividerColor,
96106
height: 0,
97107
),
98-
if (widget.bottomBar != null)
99-
widget.bottomBar!.call(controller, context),
108+
if (widget.bottomBar != null) widget.bottomBar!
100109
],
101110
),
102111
);

0 commit comments

Comments
 (0)