Skip to content

Commit

Permalink
Add endHour parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
Artur-Wisniewski committed Oct 30, 2023
1 parent c463a4d commit c459301
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 58 deletions.
1 change: 1 addition & 0 deletions example/lib/widgets/week_view_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class WeekViewWidget extends StatelessWidget {
key: state,
width: width,
startHour: 6,
endHour: 21,
);
}
}
20 changes: 16 additions & 4 deletions lib/src/components/_internal_components.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import 'dart:async';
import 'package:flutter/material.dart';

import '../calendar_event_data.dart';
import '../constants.dart';
import '../enumerations.dart';
import '../event_arrangers/event_arrangers.dart';
import '../extensions.dart';
Expand Down Expand Up @@ -118,6 +117,9 @@ class TimeLine extends StatelessWidget {
/// First hour displayed in the layout
final int startHour;

/// Last hour displayed in the layout
final int endHour;

static DateTime get _date => DateTime.now();

double get _halfHourHeight => hourHeight / 2;
Expand All @@ -131,6 +133,7 @@ class TimeLine extends StatelessWidget {
required this.timeLineOffset,
required this.timeLineBuilder,
required this.startHour,
required this.endHour,
this.showHalfHours = false,
}) : super(key: key);

Expand All @@ -146,14 +149,14 @@ class TimeLine extends StatelessWidget {
),
child: Stack(
children: [
for (int i = startHour + 1; i < Constants.hoursADay; i++)
for (int i = startHour + 1; i < endHour; i++)
_timelinePositioned(
topPosition: hourHeight * (i - startHour) - timeLineOffset,
bottomPosition: height - (hourHeight * (i - startHour + 1)) + timeLineOffset,
hour: i,
),
if (showHalfHours)
for (int i = startHour; i < Constants.hoursADay; i++)
for (int i = startHour; i < endHour; i++)
_timelinePositioned(
topPosition: hourHeight * (i - startHour) - timeLineOffset + _halfHourHeight,
bottomPosition: height - (hourHeight * (i - startHour + 1)) + timeLineOffset,
Expand Down Expand Up @@ -219,6 +222,9 @@ class EventGenerator<T extends Object?> extends StatelessWidget {
/// First hour displayed in the layout
final int startHour;

/// Last hour displayed in the layout
final int endHour;

/// Called when user taps on event tile.
final CellTapCallback<T>? onTileTap;

Expand All @@ -233,6 +239,7 @@ class EventGenerator<T extends Object?> extends StatelessWidget {
required this.heightPerMinute,
required this.eventArranger,
required this.startHour,
required this.endHour,
required this.eventTileBuilder,
required this.date,
required this.onTileTap,
Expand All @@ -249,6 +256,7 @@ class EventGenerator<T extends Object?> extends StatelessWidget {
width: width,
heightPerMinute: heightPerMinute,
startHour: startHour,
endHour: endHour,
);

return List.generate(events.length, (index) {
Expand Down Expand Up @@ -342,6 +350,9 @@ class PressDetector extends StatelessWidget {
/// First hour displayed in the layout
final int startHour;

/// Last hour displayed in the layout
final int endHour;

/// A widget that display event tiles in day/week view.
const PressDetector({
Key? key,
Expand All @@ -353,12 +364,13 @@ class PressDetector extends StatelessWidget {
required this.onDateTap,
required this.minuteSlotSize,
required this.startHour,
required this.endHour,
}) : super(key: key);

@override
Widget build(BuildContext context) {
final heightPerSlot = minuteSlotSize.minutes * heightPerMinute;
final slots = ((Constants.hoursADay - startHour) * 60) ~/ minuteSlotSize.minutes;
final slots = ((endHour - startHour) * 60) ~/ minuteSlotSize.minutes;

return Container(
height: height,
Expand Down
29 changes: 19 additions & 10 deletions lib/src/day_view/_internal_day_view_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ class InternalDayViewPage<T extends Object?> extends StatelessWidget {
/// First hour displayed in the layout
final int startHour;

/// Last hour displayed in the layout
final int endHour;

final ScrollController scrollController;

/// Defines a single day page.
Expand Down Expand Up @@ -133,6 +136,7 @@ class InternalDayViewPage<T extends Object?> extends StatelessWidget {
required this.showHalfHours,
required this.halfHourIndicatorSettings,
required this.startHour,
required this.endHour,
}) : super(key: key);

@override
Expand All @@ -155,16 +159,18 @@ 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,
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,
startHour: startHour,
endHour: endHour,
),
),
if (showHalfHours)
CustomPaint(
Expand All @@ -178,6 +184,7 @@ class InternalDayViewPage<T extends Object?> extends StatelessWidget {
dashWidth: halfHourIndicatorSettings.dashWidth,
dashSpaceWidth: halfHourIndicatorSettings.dashSpaceWidth,
startHour: startHour,
endHour: endHour,
),
),
dayDetectorBuilder(
Expand All @@ -199,6 +206,7 @@ class InternalDayViewPage<T extends Object?> extends StatelessWidget {
eventTileBuilder: eventTileBuilder,
scrollNotifier: scrollNotifier,
startHour: startHour,
endHour: endHour,
width: width - timeLineWidth - hourIndicatorSettings.offset - verticalLineOffset,
),
),
Expand All @@ -210,6 +218,7 @@ class InternalDayViewPage<T extends Object?> extends StatelessWidget {
timeLineWidth: timeLineWidth,
showHalfHours: showHalfHours,
startHour: startHour,
endHour: endHour,
key: ValueKey(heightPerMinute),
),
if (showLiveLine && liveTimeIndicatorSettings.height > 0)
Expand Down
11 changes: 10 additions & 1 deletion lib/src/day_view/day_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ class DayView<T extends Object?> extends StatefulWidget {
/// First hour displayed in the layout, goes from 0 to 24
final int? startHour;

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

final bool showHalfHours;

/// Duration from where default day view will be visible
Expand Down Expand Up @@ -231,6 +234,7 @@ class DayView<T extends Object?> extends StatefulWidget {
this.showHalfHours = false,
this.halfHourIndicatorSettings,
this.startHour,
this.endHour,
this.startDuration = const Duration(hours: 0),
}) : assert(timeLineOffset >= 0, "timeLineOffset must be greater than or equal to 0"),
assert(width == null || width > 0, "Calendar width must be greater than 0."),
Expand Down Expand Up @@ -258,6 +262,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 @@ -294,6 +299,9 @@ class DayViewState<T extends Object?> extends State<DayView<T>> {
_startHour = widget.startHour ?? 0;
if (_startHour > 24) _startHour = 0;

_endHour = widget.endHour ?? 24;
if (_endHour > 24) _endHour = 24;

_reloadCallback = _reload;
_setDateRange();

Expand Down Expand Up @@ -424,6 +432,7 @@ class DayViewState<T extends Object?> extends State<DayView<T>> {
showHalfHours: widget.showHalfHours,
halfHourIndicatorSettings: _halfHourIndicatorSettings,
startHour: _startHour,
endHour: _endHour,
),
);
},
Expand Down Expand Up @@ -494,7 +503,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
1 change: 1 addition & 0 deletions lib/src/event_arrangers/event_arrangers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ abstract class EventArranger<T extends Object?> {
required double width,
required double heightPerMinute,
required int startHour,
required int endHour,
});
}

Expand Down
1 change: 1 addition & 0 deletions lib/src/event_arrangers/merge_event_arranger.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class MergeEventArranger<T extends Object?> extends EventArranger<T> {
required double width,
required double heightPerMinute,
required int startHour,
required int endHour,
}) {
final arrangedEvents = <OrganizedCalendarEventData<T>>[];

Expand Down
2 changes: 2 additions & 0 deletions lib/src/event_arrangers/side_event_arranger.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ class SideEventArranger<T extends Object?> extends EventArranger<T> {
required double width,
required double heightPerMinute,
required int startHour,
required int endHour,
}) {
final mergedEvents = MergeEventArranger<T>().arrange(
events: events,
height: height,
width: width,
heightPerMinute: heightPerMinute,
startHour: startHour,
endHour: endHour,
);

final arrangedEvents = <OrganizedCalendarEventData<T>>[];
Expand Down
13 changes: 10 additions & 3 deletions lib/src/painters.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import 'package:flutter/material.dart';

import 'constants.dart';
import 'enumerations.dart';

/// Paints 24 hour lines.
Expand Down Expand Up @@ -39,6 +38,9 @@ class HourLinePainter extends CustomPainter {
/// First hour displayed in the layout
final int startHour;

/// Last hour displayed in the layout
final int endHour;

/// Paints 24 hour lines.
HourLinePainter({
required this.lineColor,
Expand All @@ -47,6 +49,7 @@ class HourLinePainter extends CustomPainter {
required this.offset,
required this.showVerticalLine,
required this.startHour,
required this.endHour,
this.verticalLineOffset = 10,
this.lineStyle = LineStyle.solid,
this.dashWidth = 4,
Expand All @@ -59,7 +62,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 = offset;
Expand Down Expand Up @@ -120,6 +123,9 @@ class HalfHourLinePainter extends CustomPainter {
/// First hour displayed in the layout
final int startHour;

/// Last hour displayed in the layout
final int endHour;

/// Paint half hour lines
HalfHourLinePainter({
required this.lineColor,
Expand All @@ -128,6 +134,7 @@ class HalfHourLinePainter extends CustomPainter {
required this.minuteHeight,
required this.lineStyle,
required this.startHour,
required this.endHour,
this.dashWidth = 4,
this.dashSpaceWidth = 4,
});
Expand All @@ -138,7 +145,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
22 changes: 15 additions & 7 deletions lib/src/week_view/_internal_week_view_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ class InternalWeekViewPage<T extends Object?> extends StatelessWidget {
/// First hour displayed in the layout
final int startHour;

/// First hour displayed in the layout
final int endHour;

/// A single page for week view.
const InternalWeekViewPage({
Key? key,
Expand Down Expand Up @@ -145,6 +148,7 @@ class InternalWeekViewPage<T extends Object?> extends StatelessWidget {
required this.minuteSlotSize,
required this.scrollConfiguration,
required this.startHour,
required this.endHour,
required this.fullDayEventBuilder,
required this.weekDetectorBuilder,
}) : super(key: key);
Expand Down Expand Up @@ -220,13 +224,15 @@ class InternalWeekViewPage<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,
startHour: startHour),
lineColor: hourIndicatorSettings.color,
lineHeight: hourIndicatorSettings.height,
offset: timeLineWidth + hourIndicatorSettings.offset,
minuteHeight: heightPerMinute,
verticalLineOffset: verticalLineOffset,
showVerticalLine: showVerticalLine,
startHour: startHour,
endHour: endHour,
),
),
if (showLiveLine && liveTimeIndicatorSettings.height > 0)
LiveTimeIndicator(
Expand Down Expand Up @@ -274,6 +280,7 @@ class InternalWeekViewPage<T extends Object?> extends StatelessWidget {
eventTileBuilder: eventTileBuilder,
scrollNotifier: scrollConfiguration,
startHour: startHour,
endHour: endHour,
events: controller.getEventsOnDay(filteredDates[index]),
heightPerMinute: heightPerMinute,
),
Expand All @@ -292,6 +299,7 @@ class InternalWeekViewPage<T extends Object?> extends StatelessWidget {
timeLineOffset: timeLineOffset,
timeLineBuilder: timeLineBuilder,
startHour: startHour,
endHour: endHour,
),
],
),
Expand Down
Loading

0 comments on commit c459301

Please sign in to comment.