Skip to content

Commit

Permalink
fix: 🐛 change end time for day and week view #298 (#354)
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 authored May 8, 2024
1 parent 1770d66 commit cfd0612
Show file tree
Hide file tree
Showing 9 changed files with 191 additions and 108 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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ DayView(
onEventLongTap: (events, date) => print(events),
onDateLongPress: (date) => print(date),
startHour: 5 // 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
25 changes: 22 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,
this.endHour = Constants.hoursADay,
}) : super(key: key);

@override
Expand Down Expand Up @@ -93,6 +97,13 @@ class _LiveTimeIndicatorState extends State<LiveTimeIndicator> {
/// remove startHour minute from [_currentTime.getTotalMinutes]
/// to set dy offset of live time indicator
final startMinutes = widget.startHour * 60;

/// Check if live time is not between startHour and endHour then
/// don't show live time indicator
if (_currentTime.hour > widget.startHour ||
widget.endHour < _currentTime.hour) {
return SizedBox.shrink();
}
return CustomPaint(
size: Size(widget.width, widget.liveTimeIndicatorSettings.height),
painter: CurrentTimeLinePainter(
Expand Down Expand Up @@ -150,6 +161,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 +176,7 @@ class TimeLine extends StatefulWidget {
this.showHalfHours = false,
this.showQuarterHours = false,
required this.liveTimeIndicatorSettings,
this.endHour = Constants.hoursADay,
}) : super(key: key);

@override
Expand Down Expand Up @@ -208,7 +223,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 +233,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 +245,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 +349,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 +367,7 @@ class EventGenerator<T extends Object?> extends StatelessWidget {
required this.onTileLongTap,
required this.scrollNotifier,
required this.onTileDoubleTap,
this.endHour = Constants.hoursADay,
}) : 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
75 changes: 44 additions & 31 deletions lib/src/day_view/day_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ class DayView<T extends Object?> extends StatefulWidget {
final FullDayEventBuilder<T>? fullDayEventBuilder;

/// First hour displayed in the layout, goes from 0 to 24
final int? startHour;
final int startHour;

/// Show half hour indicator
final bool showHalfHours;
Expand All @@ -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 @@ -265,12 +268,13 @@ class DayView<T extends Object?> extends StatefulWidget {
this.showHalfHours = false,
this.showQuarterHours = false,
this.halfHourIndicatorSettings,
this.startHour,
this.startHour = 0,
this.quarterHourIndicatorSettings,
this.startDuration = const Duration(hours: 0),
this.onHeaderTitleTap,
this.emulateVerticalOffsetBy = 0,
this.onEventDoubleTap,
this.endHour = Constants.hoursADay,
}) : assert(!(onHeaderTitleTap != null && dayTitleBuilder != null),
"can't use [onHeaderTitleTap] & [dayTitleBuilder] simultaneously"),
assert(timeLineOffset >= 0,
Expand All @@ -286,6 +290,14 @@ class DayView<T extends Object?> extends StatefulWidget {
"""If you use [dayPressDetectorBuilder]
do not provide [onDateLongPress]""",
),
assert(
startHour <= 0 || startHour != endHour,
"startHour must be greater than 0 or startHour should not equal to endHour",
),
assert(
endHour <= Constants.hoursADay || endHour < startHour,
"End hour must be less than 24 or startHour must be less than endHour",
),
super(key: key);

@override
Expand All @@ -302,7 +314,6 @@ class DayViewState<T extends Object?> extends State<DayView<T>> {
late DateTime _minDate;
late int _totalDays;
late int _currentIndex;
late int _startHour;

late EventArranger<T> _eventArranger;

Expand Down Expand Up @@ -339,9 +350,6 @@ class DayViewState<T extends Object?> extends State<DayView<T>> {
void initState() {
super.initState();

_startHour = widget.startHour ?? 0;
if (_startHour > 24) _startHour = 0;

_reloadCallback = _reload;
_setDateRange();

Expand Down Expand Up @@ -481,7 +489,8 @@ class DayViewState<T extends Object?> extends State<DayView<T>> {
showQuarterHours: widget.showQuarterHours,
halfHourIndicatorSettings:
_halfHourIndicatorSettings,
startHour: _startHour,
startHour: widget.startHour,
endHour: widget.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 * (widget.endHour - widget.startHour);
}

void _assignBuilders() {
Expand Down Expand Up @@ -631,7 +640,7 @@ class DayViewState<T extends Object?> extends State<DayView<T>> {
minuteSlotSize: minuteSlotSize,
onDateTap: widget.onDateTap,
onDateLongPress: widget.onDateLongPress,
startHour: _startHour,
startHour: widget.startHour,
);

/// Default timeline builder this builder will be used if
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
Loading

0 comments on commit cfd0612

Please sign in to comment.