Skip to content

Commit

Permalink
fix: 🐛 change end time for day and week view #298
Browse files Browse the repository at this point in the history
- Added end time support for day and weeek view to set end time of day and week.
  • Loading branch information
apurva010 committed May 3, 2024
1 parent 1770d66 commit af8ab3a
Show file tree
Hide file tree
Showing 9 changed files with 167 additions and 96 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
- # [1.1.1] (UnReleased)
- Added support for double tapping gestures on any event in day, week, and month view. [#195](https://github.com/SimformSolutionsPvtLtd/flutter_calendar_view/issues/195)
- Added support to set end time of day and week view. [#298](https://github.com/SimformSolutionsPvtLtd/flutter_calendar_view/issues/298)
- Added support for horizontal scroll physics of week and month view page. [#314](https://github.com/SimformSolutionsPvtLtd/flutter_calendar_view/issues/314)
- Fixed issue related to the live time indicator is that it is not in the correct position when startHour is set for the week and day view. [#346](https://github.com/SimformSolutionsPvtLtd/flutter_calendar_view/issues/346)
- Fixed issue of onDateTap returns wrong date when startHour is set for week and day view. [#341](https://github.com/SimformSolutionsPvtLtd/flutter_calendar_view/issues/341)
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@ DayView(
onEventDoubleTap: (events, date) => print(events),
onEventLongTap: (events, date) => print(events),
onDateLongPress: (date) => print(date),
startHour: 5 // To set the first hour displayed (ex: 05:00)
startHour: 51 // To set the first hour displayed (ex: 05:00)
endHour:20, // To set the end hour displayed
hourLinePainter: (lineColor, lineHeight, offset, minuteHeight, showVerticalLine, verticalLineOffset) {
return //Your custom painter.
},
Expand Down Expand Up @@ -211,7 +212,8 @@ WeekView(
onEventDoubleTap: (events, date) => print(events),
onDateLongPress: (date) => print(date),
startDay: WeekDays.sunday, // To change the first day of the week.
startHour: 5 // To set the first hour displayed (ex: 05:00)
startHour: 5, // To set the first hour displayed (ex: 05:00)
endHour:20, // To set the end hour displayed
showVerticalLines: false, // Show the vertical line between days.
hourLinePainter: (lineColor, lineHeight, offset, minuteHeight, showVerticalLine, verticalLineOffset) {
return //Your custom painter.
Expand Down
18 changes: 15 additions & 3 deletions lib/src/components/_internal_components.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ class LiveTimeIndicator extends StatefulWidget {
/// First hour displayed in the layout, goes from 0 to 24
final int startHour;

/// This field will be used to set end hour for day and week view
final int endHour;

/// Widget to display tile line according to current time.
const LiveTimeIndicator({
Key? key,
Expand All @@ -47,6 +50,7 @@ class LiveTimeIndicator extends StatefulWidget {
required this.liveTimeIndicatorSettings,
required this.heightPerMinute,
required this.startHour,
required this.endHour,
}) : super(key: key);

@override
Expand Down Expand Up @@ -150,6 +154,9 @@ class TimeLine extends StatefulWidget {

double get _halfHourHeight => hourHeight / 2;

/// This field will be used to set end hour for day and week view
final int endHour;

/// Time line to display time at left side of day or week view.
const TimeLine({
Key? key,
Expand All @@ -162,6 +169,7 @@ class TimeLine extends StatefulWidget {
this.showHalfHours = false,
this.showQuarterHours = false,
required this.liveTimeIndicatorSettings,
required this.endHour,
}) : super(key: key);

@override
Expand Down Expand Up @@ -208,7 +216,7 @@ class _TimeLineState extends State<TimeLine> {
),
child: Stack(
children: [
for (int i = widget.startHour + 1; i < Constants.hoursADay; i++)
for (int i = widget.startHour + 1; i < widget.endHour; i++)
_timelinePositioned(
topPosition: widget.hourHeight * (i - widget.startHour) -
widget.timeLineOffset,
Expand All @@ -218,7 +226,7 @@ class _TimeLineState extends State<TimeLine> {
hour: i,
),
if (widget.showHalfHours)
for (int i = widget.startHour; i < Constants.hoursADay; i++)
for (int i = widget.startHour; i < widget.endHour; i++)
_timelinePositioned(
topPosition: widget.hourHeight * (i - widget.startHour) -
widget.timeLineOffset +
Expand All @@ -230,7 +238,7 @@ class _TimeLineState extends State<TimeLine> {
minutes: 30,
),
if (widget.showQuarterHours)
for (int i = 0; i < Constants.hoursADay; i++) ...[
for (int i = 0; i < widget.endHour; i++) ...[
/// this is for 15 minutes
_timelinePositioned(
topPosition: widget.hourHeight * i -
Expand Down Expand Up @@ -334,6 +342,9 @@ class EventGenerator<T extends Object?> extends StatelessWidget {

final EventScrollConfiguration scrollNotifier;

/// This field will be used to set end hour for day and week view
final int endHour;

/// A widget that display event tiles in day/week view.
const EventGenerator({
Key? key,
Expand All @@ -349,6 +360,7 @@ class EventGenerator<T extends Object?> extends StatelessWidget {
required this.onTileLongTap,
required this.scrollNotifier,
required this.onTileDoubleTap,
required this.endHour,
}) : super(key: key);

/// Arrange events and returns list of [Widget] that displays event
Expand Down
32 changes: 21 additions & 11 deletions lib/src/day_view/_internal_day_view_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ class InternalDayViewPage<T extends Object?> extends StatelessWidget {
/// Emulate vertical line offset from hour line starts.
final double emulateVerticalOffsetBy;

/// This field will be used to set end hour for day view
final int endHour;

/// Defines a single day page.
const InternalDayViewPage({
Key? key,
Expand Down Expand Up @@ -154,6 +157,7 @@ class InternalDayViewPage<T extends Object?> extends StatelessWidget {
required this.showQuarterHours,
required this.halfHourIndicatorSettings,
required this.startHour,
required this.endHour,
required this.quarterHourIndicatorSettings,
required this.emulateVerticalOffsetBy,
required this.onTileDoubleTap,
Expand Down Expand Up @@ -181,17 +185,19 @@ class InternalDayViewPage<T extends Object?> extends StatelessWidget {
CustomPaint(
size: Size(width, height),
painter: HourLinePainter(
lineColor: hourIndicatorSettings.color,
lineHeight: hourIndicatorSettings.height,
offset: timeLineWidth + hourIndicatorSettings.offset,
minuteHeight: heightPerMinute,
verticalLineOffset: verticalLineOffset,
showVerticalLine: showVerticalLine,
lineStyle: hourIndicatorSettings.lineStyle,
dashWidth: hourIndicatorSettings.dashWidth,
dashSpaceWidth: hourIndicatorSettings.dashSpaceWidth,
emulateVerticalOffsetBy: emulateVerticalOffsetBy,
startHour: startHour),
lineColor: hourIndicatorSettings.color,
lineHeight: hourIndicatorSettings.height,
offset: timeLineWidth + hourIndicatorSettings.offset,
minuteHeight: heightPerMinute,
verticalLineOffset: verticalLineOffset,
showVerticalLine: showVerticalLine,
lineStyle: hourIndicatorSettings.lineStyle,
dashWidth: hourIndicatorSettings.dashWidth,
dashSpaceWidth: hourIndicatorSettings.dashSpaceWidth,
emulateVerticalOffsetBy: emulateVerticalOffsetBy,
startHour: startHour,
endHour: endHour,
),
),
if (showHalfHours)
CustomPaint(
Expand All @@ -207,6 +213,7 @@ class InternalDayViewPage<T extends Object?> extends StatelessWidget {
dashSpaceWidth:
halfHourIndicatorSettings.dashSpaceWidth,
startHour: startHour,
endHour: endHour,
),
),
if (showQuarterHours)
Expand Down Expand Up @@ -248,6 +255,7 @@ class InternalDayViewPage<T extends Object?> extends StatelessWidget {
eventTileBuilder: eventTileBuilder,
scrollNotifier: scrollNotifier,
startHour: startHour,
endHour: endHour,
width: width -
timeLineWidth -
hourIndicatorSettings.offset -
Expand All @@ -262,6 +270,7 @@ class InternalDayViewPage<T extends Object?> extends StatelessWidget {
timeLineWidth: timeLineWidth,
showHalfHours: showHalfHours,
startHour: startHour,
endHour: endHour,
showQuarterHours: showQuarterHours,
key: ValueKey(heightPerMinute),
liveTimeIndicatorSettings: liveTimeIndicatorSettings,
Expand All @@ -275,6 +284,7 @@ class InternalDayViewPage<T extends Object?> extends StatelessWidget {
heightPerMinute: heightPerMinute,
timeLineWidth: timeLineWidth,
startHour: startHour,
endHour: endHour,
),
),
],
Expand Down
59 changes: 36 additions & 23 deletions lib/src/day_view/day_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,9 @@ class DayView<T extends Object?> extends StatefulWidget {
/// Emulate vertical line offset from hour line starts.
final double emulateVerticalOffsetBy;

/// This field will be used to set end hour for day view
final int? endHour;

/// Main widget for day view.
const DayView({
Key? key,
Expand Down Expand Up @@ -271,6 +274,7 @@ class DayView<T extends Object?> extends StatefulWidget {
this.onHeaderTitleTap,
this.emulateVerticalOffsetBy = 0,
this.onEventDoubleTap,
this.endHour,
}) : assert(!(onHeaderTitleTap != null && dayTitleBuilder != null),
"can't use [onHeaderTitleTap] & [dayTitleBuilder] simultaneously"),
assert(timeLineOffset >= 0,
Expand Down Expand Up @@ -303,6 +307,7 @@ class DayViewState<T extends Object?> extends State<DayView<T>> {
late int _totalDays;
late int _currentIndex;
late int _startHour;
late int _endHour;

late EventArranger<T> _eventArranger;

Expand Down Expand Up @@ -340,7 +345,10 @@ class DayViewState<T extends Object?> extends State<DayView<T>> {
super.initState();

_startHour = widget.startHour ?? 0;
_endHour = widget.endHour ?? Constants.hoursADay;

if (_startHour > 24) _startHour = 0;
if (_endHour > 24 || _startHour > _endHour) _endHour = Constants.hoursADay;

_reloadCallback = _reload;
_setDateRange();
Expand Down Expand Up @@ -482,6 +490,7 @@ class DayViewState<T extends Object?> extends State<DayView<T>> {
halfHourIndicatorSettings:
_halfHourIndicatorSettings,
startHour: _startHour,
endHour: _endHour,
quarterHourIndicatorSettings:
_quarterHourIndicatorSettings,
emulateVerticalOffsetBy:
Expand Down Expand Up @@ -567,7 +576,7 @@ class DayViewState<T extends Object?> extends State<DayView<T>> {

void _calculateHeights() {
_hourHeight = widget.heightPerMinute * 60;
_height = _hourHeight * (Constants.hoursADay - _startHour);
_height = _hourHeight * (_endHour - _startHour);
}

void _assignBuilders() {
Expand Down Expand Up @@ -691,29 +700,33 @@ class DayViewState<T extends Object?> extends State<DayView<T>> {
FullDayEventView(events: events, date: date);

HourLinePainter _defaultHourLinePainter(
Color lineColor,
double lineHeight,
double offset,
double minuteHeight,
bool showVerticalLine,
double verticalLineOffset,
LineStyle lineStyle,
double dashWidth,
double dashSpaceWidth,
double emulateVerticalOffsetBy,
int startHour) {
Color lineColor,
double lineHeight,
double offset,
double minuteHeight,
bool showVerticalLine,
double verticalLineOffset,
LineStyle lineStyle,
double dashWidth,
double dashSpaceWidth,
double emulateVerticalOffsetBy,
int startHour,
int endHour,
) {
return HourLinePainter(
lineColor: lineColor,
lineHeight: lineHeight,
offset: offset,
minuteHeight: minuteHeight,
verticalLineOffset: verticalLineOffset,
showVerticalLine: showVerticalLine,
lineStyle: lineStyle,
dashWidth: dashWidth,
dashSpaceWidth: dashSpaceWidth,
emulateVerticalOffsetBy: emulateVerticalOffsetBy,
startHour: startHour);
lineColor: lineColor,
lineHeight: lineHeight,
offset: offset,
minuteHeight: minuteHeight,
verticalLineOffset: verticalLineOffset,
showVerticalLine: showVerticalLine,
lineStyle: lineStyle,
dashWidth: dashWidth,
dashSpaceWidth: dashSpaceWidth,
emulateVerticalOffsetBy: emulateVerticalOffsetBy,
startHour: startHour,
endHour: endHour,
);
}

/// Called when user change page using any gesture or inbuilt functions.
Expand Down
12 changes: 10 additions & 2 deletions lib/src/painters.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ class HourLinePainter extends CustomPainter {
/// Emulates offset of vertical line from hour line starts.
final double emulateVerticalOffsetBy;

/// This field will be used to set end hour for day and week view
final int endHour;

/// Paints 24 hour lines.
HourLinePainter({
required this.lineColor,
Expand All @@ -53,6 +56,7 @@ class HourLinePainter extends CustomPainter {
required this.showVerticalLine,
required this.startHour,
required this.emulateVerticalOffsetBy,
required this.endHour,
this.verticalLineOffset = 10,
this.lineStyle = LineStyle.solid,
this.dashWidth = 4,
Expand All @@ -66,7 +70,7 @@ class HourLinePainter extends CustomPainter {
..color = lineColor
..strokeWidth = lineHeight;

for (var i = startHour + 1; i < Constants.hoursADay; i++) {
for (var i = startHour + 1; i < endHour; i++) {
final dy = (i - startHour) * minuteHeight * 60;
if (lineStyle == LineStyle.dashed) {
var startX = dx;
Expand Down Expand Up @@ -131,6 +135,9 @@ class HalfHourLinePainter extends CustomPainter {
/// First hour displayed in the layout
final int startHour;

/// This field will be used to set end hour for day and week view
final int endHour;

/// Paint half hour lines
HalfHourLinePainter({
required this.lineColor,
Expand All @@ -141,6 +148,7 @@ class HalfHourLinePainter extends CustomPainter {
required this.startHour,
this.dashWidth = 4,
this.dashSpaceWidth = 4,
required this.endHour,
});

@override
Expand All @@ -149,7 +157,7 @@ class HalfHourLinePainter extends CustomPainter {
..color = lineColor
..strokeWidth = lineHeight;

for (var i = startHour; i < Constants.hoursADay; i++) {
for (var i = startHour; i < endHour; i++) {
final dy = (i - startHour) * minuteHeight * 60 + (minuteHeight * 30);
if (lineStyle == LineStyle.dashed) {
var startX = offset;
Expand Down
24 changes: 13 additions & 11 deletions lib/src/typedefs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,18 @@ typedef EventSorter<T extends Object?> = int Function(
CalendarEventData<T> a, CalendarEventData<T> b);

typedef CustomHourLinePainter = CustomPainter Function(
Color lineColor,
double lineHeight,
double offset,
double minuteHeight,
bool showVerticalLine,
double verticalLineOffset,
LineStyle lineStyle,
double dashWidth,
double dashSpaceWidth,
double emulateVerticalOffsetBy,
int startHour);
Color lineColor,
double lineHeight,
double offset,
double minuteHeight,
bool showVerticalLine,
double verticalLineOffset,
LineStyle lineStyle,
double dashWidth,
double dashSpaceWidth,
double emulateVerticalOffsetBy,
int startHour,
int endHour,
);

typedef TestPredicate<T> = bool Function(T element);
Loading

0 comments on commit af8ab3a

Please sign in to comment.