Skip to content

Commit

Permalink
✨ update example to support latest flutter versions.
Browse files Browse the repository at this point in the history
  • Loading branch information
ParthBaraiya committed Jun 13, 2024
1 parent 99bb35f commit f404f75
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 55 deletions.
2 changes: 1 addition & 1 deletion example/lib/widgets/day_view_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class DayViewWidget extends StatelessWidget {
hourIndicatorSettings: HourIndicatorSettings(
color: Theme.of(context).dividerColor,
),
showQuarterHours: true,
onEventTap: (events, date) {
Navigator.of(context).push(
MaterialPageRoute(
Expand All @@ -44,7 +45,6 @@ class DayViewWidget extends StatelessWidget {
),
verticalLineOffset: 0,
timeLineWidth: 65,
showQuarterHours: true,
showLiveTimeLineInAllDays: true,
liveTimeIndicatorSettings: LiveTimeIndicatorSettings(
color: Colors.redAccent,
Expand Down
65 changes: 38 additions & 27 deletions lib/src/components/_internal_components.dart
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ class TimeLine extends StatefulWidget {
/// Defines if we need to display the 0 hr and 24 hr text in time line or not.
final bool showStartHours;

final EdgeInsets padding;

/// Time line to display time at left side of day or week view.
const TimeLine({
Key? key,
Expand All @@ -188,6 +190,7 @@ class TimeLine extends StatefulWidget {
this.endHour = Constants.hoursADay,
required this.showEndHours,
required this.showStartHours,
required this.padding,
}) : super(key: key);

@override
Expand All @@ -204,24 +207,6 @@ class _TimeLineState extends State<TimeLine> {
_timer = Timer.periodic(Duration(seconds: 1), _onTick);
}

@override
void dispose() {
_timer.cancel();
super.dispose();
}

/// Creates an recursive call that runs every 1 seconds.
/// This will rebuild TimeLine every second. This will allow us
/// to show/hide time line when there is overlap with
/// live time line indicator in Week and Day view.
void _onTick(Timer? timer) {
final time = TimeOfDay.now();
if (time != _currentTime && mounted) {
_currentTime = time;
setState(() {});
}
}

@override
Widget build(BuildContext context) {
return ConstrainedBox(
Expand All @@ -239,21 +224,25 @@ class _TimeLineState extends State<TimeLine> {
i++)
_timelinePositioned(
topPosition: widget.hourHeight * (i - widget.startHour) -
widget.timeLineOffset,
widget.timeLineOffset +
widget.padding.top,
bottomPosition: widget.height -
(widget.hourHeight * (i - widget.startHour + 1)) +
widget.timeLineOffset,
widget.timeLineOffset -
widget.padding.bottom,
hour: i,
),
if (widget.showHalfHours)
for (int i = widget.startHour; i < widget.endHour; i++)
_timelinePositioned(
topPosition: widget.hourHeight * (i - widget.startHour) -
widget.timeLineOffset +
widget._halfHourHeight,
widget._halfHourHeight +
widget.padding.top,
bottomPosition: widget.height -
(widget.hourHeight * (i - widget.startHour + 1)) +
widget.timeLineOffset,
widget.timeLineOffset -
widget.padding.bottom,
hour: i,
minutes: 30,
),
Expand All @@ -263,10 +252,12 @@ class _TimeLineState extends State<TimeLine> {
_timelinePositioned(
topPosition: widget.hourHeight * i -
widget.timeLineOffset +
widget.hourHeight * 0.25,
widget.hourHeight * 0.25 +
widget.padding.top,
bottomPosition: widget.height -
(widget.hourHeight * (i + 1)) +
widget.timeLineOffset,
widget.timeLineOffset -
widget.padding.bottom,
hour: i,
minutes: 15,
),
Expand All @@ -275,10 +266,12 @@ class _TimeLineState extends State<TimeLine> {
_timelinePositioned(
topPosition: widget.hourHeight * i -
widget.timeLineOffset +
widget.hourHeight * 0.75,
widget.hourHeight * 0.75 +
widget.padding.top,
bottomPosition: widget.height -
(widget.hourHeight * (i + 1)) +
widget.timeLineOffset,
widget.timeLineOffset -
widget.padding.bottom,
hour: i,
minutes: 45,
),
Expand All @@ -288,6 +281,24 @@ class _TimeLineState extends State<TimeLine> {
);
}

@override
void dispose() {
_timer.cancel();
super.dispose();
}

/// Creates an recursive call that runs every 1 seconds.
/// This will rebuild TimeLine every second. This will allow us
/// to show/hide time line when there is overlap with
/// live time line indicator in Week and Day view.
void _onTick(Timer? timer) {
final time = TimeOfDay.now();
if (time != _currentTime && mounted) {
_currentTime = time;
setState(() {});
}
}

/// To avoid overlap of live time line indicator, show time line when
/// current min is less than 45 min and is previous hour or
/// current min is greater than 15 min and is current hour
Expand All @@ -307,7 +318,7 @@ class _TimeLineState extends State<TimeLine> {
left: 0,
right: 0,
bottom: bottomPosition,
child: Container(
child: SizedBox(
height: widget.hourHeight,
width: widget.timeLineWidth,
child: widget.timeLineBuilder.call(
Expand Down
13 changes: 7 additions & 6 deletions lib/src/components/common_components.dart
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ class DefaultPressDetector extends StatelessWidget {
this.onDateTap,
this.onDateLongPress,
this.startHour = 0,
required this.padding,
required this.endHour,
});

final DateTime date;
Expand All @@ -144,11 +146,13 @@ class DefaultPressDetector extends StatelessWidget {
final DateTapCallback? onDateTap;
final DatePressCallback? onDateLongPress;
final int startHour;
final int endHour;
final EdgeInsets padding;

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

return SizedBox(
height: height,
Expand All @@ -157,10 +161,10 @@ class DefaultPressDetector extends StatelessWidget {
children: [
for (int i = 0; i < slots; i++)
Positioned(
top: heightPerSlot * i,
top: padding.top + heightPerSlot * i,
left: 0,
right: 0,
bottom: height - (heightPerSlot * (i + 1)),
bottom: height - (heightPerSlot * (i + 1)) - padding.bottom,
child: GestureDetector(
behavior: HitTestBehavior.translucent,
onLongPress: () => onDateLongPress?.call(
Expand All @@ -172,9 +176,6 @@ class DefaultPressDetector extends StatelessWidget {
child: SizedBox(
width: width,
height: heightPerSlot,
child: kDebugMode
? ColoredBox(color: Colors.red.withOpacity(0.1))
: SizedBox.shrink(),
),
),
),
Expand Down
8 changes: 1 addition & 7 deletions lib/src/components/day_view_components.dart
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,7 @@ class DefaultTimeLineMark extends StatelessWidget {
: "$hour ${time.hour ~/ 12 == 0 ? "am" : "pm"}";

return Transform.translate(
offset: Offset(
0,
time.hour == 0 && time.minute == 0
? -7.5
: time.hour == 24 && time.minute == 0
? -18
: -7.5),
offset: Offset(0, -8),
child: Padding(
padding: const EdgeInsets.only(right: 7.0),
child: Text(
Expand Down
27 changes: 13 additions & 14 deletions lib/src/day_view/_internal_day_view_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -306,23 +306,21 @@ class _InternalDayViewPageState<T extends Object?>
//#endregion

// Enables gesture in empty areas.
Padding(
padding: widget.pagePadding,
child: widget.dayDetectorBuilder(
width: widget.width,
height: widget.height,
heightPerMinute: widget.heightPerMinute,
date: widget.date,
minuteSlotSize: widget.minuteSlotSize,
),
widget.dayDetectorBuilder(
width: widget.width,
height: widget.height,
heightPerMinute: widget.heightPerMinute,
date: widget.date,
minuteSlotSize: widget.minuteSlotSize,
pagePadding: widget.pagePadding,
),

Align(
alignment: Alignment.centerRight,
child: Padding(
padding: widget.pagePadding,
Padding(
padding: widget.pagePadding,
child: Align(
alignment: Alignment.centerRight,
child: EventGenerator<T>(
height: widget.height,
height: widget.height - widget.pagePadding.vertical,
date: widget.date,
onTileLongTap: widget.onTileLongTap,
onTileDoubleTap: widget.onTileDoubleTap,
Expand Down Expand Up @@ -358,6 +356,7 @@ class _InternalDayViewPageState<T extends Object?>
liveTimeIndicatorSettings: widget.liveTimeIndicatorSettings,
showEndHours: widget.showEndHours,
showStartHours: widget.showStartHours,
padding: widget.pagePadding,
),
if (widget.showLiveLine &&
widget.liveTimeIndicatorSettings.height > 0)
Expand Down
3 changes: 3 additions & 0 deletions lib/src/day_view/day_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,7 @@ class DayViewState<T extends Object?> extends State<DayView<T>> {
required double width,
required double heightPerMinute,
required MinuteSlotSize minuteSlotSize,
required EdgeInsets pagePadding,
}) =>
DefaultPressDetector(
date: date,
Expand All @@ -675,6 +676,8 @@ class DayViewState<T extends Object?> extends State<DayView<T>> {
onDateTap: widget.onDateTap,
onDateLongPress: widget.onDateLongPress,
startHour: widget.startHour,
endHour: widget.endHour,
padding: pagePadding,
);

/// Default timeline builder this builder will be used if
Expand Down
1 change: 1 addition & 0 deletions lib/src/typedefs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ typedef DetectorBuilder<T extends Object?> = Widget Function({
required double width,
required double heightPerMinute,
required MinuteSlotSize minuteSlotSize,
required EdgeInsets pagePadding,
});

typedef WeekDayBuilder = Widget Function(
Expand Down
2 changes: 2 additions & 0 deletions lib/src/week_view/_internal_week_view_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,7 @@ class _InternalWeekViewPageState<T extends Object?>
widget.heightPerMinute,
date: widget.dates[index],
minuteSlotSize: widget.minuteSlotSize,
pagePadding: widget.pagePadding,
),
),
EventGenerator<T>(
Expand Down Expand Up @@ -498,6 +499,7 @@ class _InternalWeekViewPageState<T extends Object?>
endHour: widget.endHour,
showEndHours: widget.showEndHours,
showStartHours: widget.showStartHours,
padding: widget.pagePadding,
),
if (widget.showLiveLine &&
widget.liveTimeIndicatorSettings.height > 0)
Expand Down
3 changes: 3 additions & 0 deletions lib/src/week_view/week_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,7 @@ class WeekViewState<T extends Object?> extends State<WeekView<T>> {
required double width,
required double heightPerMinute,
required MinuteSlotSize minuteSlotSize,
required EdgeInsets pagePadding,
}) =>
DefaultPressDetector(
date: date,
Expand All @@ -768,6 +769,8 @@ class WeekViewState<T extends Object?> extends State<WeekView<T>> {
onDateTap: widget.onDateTap,
onDateLongPress: widget.onDateLongPress,
startHour: _startHour,
endHour: _endHour,
padding: pagePadding,
);

/// Default builder for week line.
Expand Down

0 comments on commit f404f75

Please sign in to comment.