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 2, 2024
1 parent fd248d3 commit 9c8f1fa
Show file tree
Hide file tree
Showing 7 changed files with 203 additions and 135 deletions.
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 @@ -331,6 +339,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 @@ -345,6 +356,7 @@ class EventGenerator<T extends Object?> extends StatelessWidget {
required this.onTileTap,
required this.onTileLongTap,
required this.scrollNotifier,
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 @@ -118,6 +118,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 @@ -151,6 +154,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,
}) : super(key: key);
Expand All @@ -177,17 +181,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 @@ -203,6 +209,7 @@ class InternalDayViewPage<T extends Object?> extends StatelessWidget {
dashSpaceWidth:
halfHourIndicatorSettings.dashSpaceWidth,
startHour: startHour,
endHour: endHour,
),
),
if (showQuarterHours)
Expand Down Expand Up @@ -243,6 +250,7 @@ class InternalDayViewPage<T extends Object?> extends StatelessWidget {
eventTileBuilder: eventTileBuilder,
scrollNotifier: scrollNotifier,
startHour: startHour,
endHour: endHour,
width: width -
timeLineWidth -
hourIndicatorSettings.offset -
Expand All @@ -257,6 +265,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 @@ -270,6 +279,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 @@ -220,6 +220,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 @@ -267,6 +270,7 @@ class DayView<T extends Object?> extends StatefulWidget {
this.startDuration = const Duration(hours: 0),
this.onHeaderTitleTap,
this.emulateVerticalOffsetBy = 0,
this.endHour,
}) : assert(!(onHeaderTitleTap != null && dayTitleBuilder != null),
"can't use [onHeaderTitleTap] & [dayTitleBuilder] simultaneously"),
assert(timeLineOffset >= 0,
Expand Down Expand Up @@ -299,6 +303,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 @@ -336,7 +341,10 @@ class DayViewState<T extends Object?> extends State<DayView<T>> {
super.initState();

_startHour = widget.startHour ?? 0;
_endHour = widget.endHour ?? 24;

if (_startHour > 24) _startHour = 0;
if (_endHour > 24 || _startHour > _endHour) _endHour = 24;

_reloadCallback = _reload;
_setDateRange();
Expand Down Expand Up @@ -477,6 +485,7 @@ class DayViewState<T extends Object?> extends State<DayView<T>> {
halfHourIndicatorSettings:
_halfHourIndicatorSettings,
startHour: _startHour,
endHour: _endHour,
quarterHourIndicatorSettings:
_quarterHourIndicatorSettings,
emulateVerticalOffsetBy:
Expand Down Expand Up @@ -562,7 +571,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 @@ -686,29 +695,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 9c8f1fa

Please sign in to comment.