Skip to content

Commit

Permalink
refactor code of day week view
Browse files Browse the repository at this point in the history
- create common widgets which being used as widget function in both day and week view
  • Loading branch information
apurva010 committed Apr 30, 2024
1 parent e310366 commit 3e14c0f
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 126 deletions.
108 changes: 108 additions & 0 deletions lib/src/components/common_components.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

import '../calendar_event_data.dart';
import '../constants.dart';
import '../extensions.dart';
import '../style/header_style.dart';
import '../typedefs.dart';
import '../enumerations.dart';
import 'components.dart';

class CalendarPageHeader extends StatelessWidget {
/// When user taps on right arrow.
Expand Down Expand Up @@ -117,3 +121,107 @@ class CalendarPageHeader extends StatelessWidget {
);
}
}

/// This will used in day and week view
class DefaultPressDetector extends StatelessWidget {
/// default press detector builder used in week and day view
const DefaultPressDetector({
required this.date,
required this.height,
required this.width,
required this.heightPerMinute,
required this.minuteSlotSize,
this.onDateTap,
this.onDateLongPress,
});

final DateTime date;
final double height;
final double width;
final double heightPerMinute;
final MinuteSlotSize minuteSlotSize;
final DateTapCallback? onDateTap;
final DatePressCallback? onDateLongPress;

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

return SizedBox(
height: height,
width: width,
child: Stack(
children: [
for (int i = 0; i < slots; i++)
Positioned(
top: heightPerSlot * i,
left: 0,
right: 0,
bottom: height - (heightPerSlot * (i + 1)),
child: GestureDetector(
behavior: HitTestBehavior.translucent,
onLongPress: () => onDateLongPress?.call(
getTappedDateTime(i),
),
onTap: () => onDateTap?.call(
getTappedDateTime(i),
),
child: SizedBox(
width: width,
height: heightPerSlot,
),
),
),
],
),
);
}

DateTime getTappedDateTime(int slot) {
return DateTime(
date.year,
date.month,
date.day,
0,
(minuteSlotSize.minutes * slot),
);
}
}

/// This will used in day and week view
class DefaultEventTile<T> extends StatelessWidget {
const DefaultEventTile({
required this.date,
required this.events,
required this.boundary,
required this.startDuration,
required this.endDuration,
});

final DateTime date;
final List<CalendarEventData<T>> events;
final Rect boundary;
final DateTime startDuration;
final DateTime endDuration;

@override
Widget build(BuildContext context) {
if (events.isNotEmpty) {
final event = events[0];
return RoundedEventTile(
borderRadius: BorderRadius.circular(10.0),
title: event.title,
totalEvents: events.length - 1,
description: event.description,
padding: EdgeInsets.all(10.0),
backgroundColor: event.color,
margin: EdgeInsets.all(2.0),
titleStyle: event.titleStyle,
descriptionStyle: event.descriptionStyle,
);
} else {
return SizedBox.shrink();
}
}
}
66 changes: 14 additions & 52 deletions lib/src/day_view/day_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:flutter/material.dart';
import '../calendar_constants.dart';
import '../calendar_controller_provider.dart';
import '../calendar_event_data.dart';
import '../components/common_components.dart';
import '../components/day_view_components.dart';
import '../components/event_scroll_notifier.dart';
import '../components/safe_area_wrapper.dart';
Expand Down Expand Up @@ -612,45 +613,14 @@ class DayViewState<T extends Object?> extends State<DayView<T>> {
required double heightPerMinute,
required MinuteSlotSize minuteSlotSize,
}) {
final heightPerSlot = minuteSlotSize.minutes * heightPerMinute;
final slots = (Constants.hoursADay * 60) ~/ minuteSlotSize.minutes;

return Container(
return DefaultPressDetector(
date: date,
height: height,
width: width,
child: Stack(
children: [
for (int i = 0; i < slots; i++)
Positioned(
top: heightPerSlot * i,
left: 0,
right: 0,
bottom: height - (heightPerSlot * (i + 1)),
child: GestureDetector(
behavior: HitTestBehavior.translucent,
onLongPress: () => widget.onDateLongPress?.call(
DateTime(
date.year,
date.month,
date.day,
0,
minuteSlotSize.minutes * i,
),
),
onTap: () => widget.onDateTap?.call(
DateTime(
date.year,
date.month,
date.day,
0,
minuteSlotSize.minutes * i,
),
),
child: SizedBox(width: width, height: heightPerSlot),
),
),
],
),
heightPerMinute: heightPerMinute,
minuteSlotSize: minuteSlotSize,
onDateTap: widget.onDateTap,
onDateLongPress: widget.onDateLongPress,
);
}

Expand All @@ -670,21 +640,13 @@ class DayViewState<T extends Object?> extends State<DayView<T>> {
DateTime startDuration,
DateTime endDuration,
) {
if (events.isNotEmpty) {
return RoundedEventTile(
borderRadius: BorderRadius.circular(10.0),
title: events[0].title,
totalEvents: events.length - 1,
description: events[0].description,
padding: EdgeInsets.all(10.0),
backgroundColor: events[0].color,
margin: EdgeInsets.all(2.0),
titleStyle: events[0].titleStyle,
descriptionStyle: events[0].descriptionStyle,
);
} else {
return SizedBox.shrink();
}
return DefaultEventTile(
date: date,
events: events,
boundary: boundary,
startDuration: startDuration,
endDuration: endDuration,
);
}

/// Default view header builder. This builder will be used if
Expand Down
91 changes: 17 additions & 74 deletions lib/src/week_view/week_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:flutter/material.dart';
import '../calendar_constants.dart';
import '../calendar_controller_provider.dart';
import '../calendar_event_data.dart';
import '../components/common_components.dart';
import '../components/components.dart';
import '../components/event_scroll_notifier.dart';
import '../components/safe_area_wrapper.dart';
Expand Down Expand Up @@ -677,46 +678,14 @@ class WeekViewState<T extends Object?> extends State<WeekView<T>> {
required double heightPerMinute,
required MinuteSlotSize minuteSlotSize,
}) {
final heightPerSlot = minuteSlotSize.minutes * heightPerMinute;
final slots =
((Constants.hoursADay - _startHour) * 60) ~/ minuteSlotSize.minutes;

return Container(
return DefaultPressDetector(
date: date,
height: height,
width: width,
child: Stack(
children: [
for (int i = 0; i < slots; i++)
Positioned(
top: heightPerSlot * i,
left: 0,
right: 0,
bottom: height - (heightPerSlot * (i + 1)),
child: GestureDetector(
behavior: HitTestBehavior.translucent,
onLongPress: () => widget.onDateLongPress?.call(
DateTime(
date.year,
date.month,
date.day,
0,
minuteSlotSize.minutes * i,
),
),
onTap: () => widget.onDateTap?.call(
DateTime(
date.year,
date.month,
date.day,
0,
minuteSlotSize.minutes * i,
),
),
child: SizedBox(width: width, height: heightPerSlot),
),
),
],
),
heightPerMinute: heightPerMinute,
minuteSlotSize: minuteSlotSize,
onDateTap: widget.onDateTap,
onDateLongPress: widget.onDateLongPress,
);
}

Expand Down Expand Up @@ -753,25 +722,9 @@ class WeekViewState<T extends Object?> extends State<WeekView<T>> {
/// [widget.eventTileBuilder] is null
///
Widget _defaultTimeLineBuilder(DateTime date) {
final hour = ((date.hour - 1) % 12) + 1;
final timeLineString = (widget.timeLineStringBuilder != null)
? widget.timeLineStringBuilder!(date)
: date.minute != 0
? "$hour:${date.minute}"
: "$hour ${date.hour ~/ 12 == 0 ? "am" : "pm"}";

return Transform.translate(
offset: Offset(0, -7.5),
child: Padding(
padding: const EdgeInsets.only(right: 7.0),
child: Text(
timeLineString,
textAlign: TextAlign.right,
style: TextStyle(
fontSize: 15.0,
),
),
),
return DefaultTimeLineMark(
date: date,
timeStringBuilder: widget.timeLineStringBuilder,
);
}

Expand All @@ -783,23 +736,13 @@ class WeekViewState<T extends Object?> extends State<WeekView<T>> {
Rect boundary,
DateTime startDuration,
DateTime endDuration) {
if (events.isNotEmpty) {
return RoundedEventTile(
borderRadius: BorderRadius.circular(6.0),
title: events[0].title,
titleStyle: events[0].titleStyle ??
TextStyle(
fontSize: 12,
color: events[0].color.accent,
),
descriptionStyle: events[0].descriptionStyle,
totalEvents: events.length,
padding: EdgeInsets.all(7.0),
backgroundColor: events[0].color,
);
} else {
return Container();
}
return DefaultEventTile(
date: date,
events: events,
boundary: boundary,
startDuration: startDuration,
endDuration: endDuration,
);
}

/// Default view header builder. This builder will be used if
Expand Down

0 comments on commit 3e14c0f

Please sign in to comment.