Skip to content

Commit

Permalink
Sinchronize scroll from wekkview
Browse files Browse the repository at this point in the history
  • Loading branch information
Fernando Luis authored and apurva010 committed May 8, 2024
1 parent 696afc9 commit db4e2aa
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 51 deletions.
137 changes: 88 additions & 49 deletions lib/src/week_view/_internal_week_view_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import '../painters.dart';
import '../typedefs.dart';

/// A single page for week view.
class InternalWeekViewPage<T extends Object?> extends StatelessWidget {
class InternalWeekViewPage<T extends Object?> extends StatefulWidget {
/// Width of the page.
final double width;

Expand Down Expand Up @@ -88,8 +88,6 @@ class InternalWeekViewPage<T extends Object?> extends StatelessWidget {
/// Width of week title.
final double weekTitleWidth;

final ScrollController scrollController;

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

Expand Down Expand Up @@ -144,6 +142,11 @@ class InternalWeekViewPage<T extends Object?> extends StatelessWidget {
/// This field will be used to set end hour for week view
final int endHour;

final void Function(ScrollController) scrollListener;

/// Scroll offset of week view page.
final double scrollOffset;

/// A single page for week view.
const InternalWeekViewPage({
Key? key,
Expand All @@ -170,7 +173,6 @@ class InternalWeekViewPage<T extends Object?> extends StatelessWidget {
required this.eventArranger,
required this.verticalLineOffset,
required this.weekTitleWidth,
required this.scrollController,
required this.onTileTap,
required this.onTileLongTap,
required this.onDateLongPress,
Expand All @@ -187,35 +189,67 @@ class InternalWeekViewPage<T extends Object?> extends StatelessWidget {
required this.emulateVerticalOffsetBy,
required this.onTileDoubleTap,
required this.endHour,
required this.scrollListener,
this.scrollOffset = 0.0,
}) : super(key: key);

@override
State<InternalWeekViewPage> createState() => _InternalWeekViewPageState();
}

class _InternalWeekViewPageState<T extends Object?>
extends State<InternalWeekViewPage<T>> {
late ScrollController scrollController;

@override
void initState() {
super.initState();
scrollController = ScrollController(
initialScrollOffset: widget.scrollOffset,
);
scrollController.addListener(_scrollControllerListener);
}

@override
void dispose() {
scrollController
..removeListener(_scrollControllerListener)
..dispose();
super.dispose();
}

void _scrollControllerListener() {
widget.scrollListener(scrollController);
}

@override
Widget build(BuildContext context) {
final filteredDates = _filteredDate();
return Container(
height: height + weekTitleHeight,
width: width,
height: widget.height + widget.weekTitleHeight,
width: widget.width,
child: Column(
verticalDirection:
showWeekDayAtBottom ? VerticalDirection.up : VerticalDirection.down,
crossAxisAlignment: CrossAxisAlignment.end,
children: [
SizedBox(
width: width,
width: widget.width,
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(
height: weekTitleHeight,
width: timeLineWidth + hourIndicatorSettings.offset,
child: weekNumberBuilder.call(filteredDates[0]),
height: widget.weekTitleHeight,
width: widget.timeLineWidth +
widget.hourIndicatorSettings.offset,
child: widget.weekNumberBuilder.call(filteredDates[0]),
),
...List.generate(
filteredDates.length,
(index) => SizedBox(
height: weekTitleHeight,
width: weekTitleWidth,
child: weekDayBuilder(
height: widget.weekTitleHeight,
width: widget.weekTitleWidth,
child: widget.weekDayBuilder(
filteredDates[index],
),
),
Expand All @@ -228,21 +262,23 @@ class InternalWeekViewPage<T extends Object?> extends StatelessWidget {
height: 1,
),
SizedBox(
width: width,
width: widget.width,
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(width: timeLineWidth + hourIndicatorSettings.offset),
SizedBox(
width: widget.timeLineWidth +
widget.hourIndicatorSettings.offset),
...List.generate(
filteredDates.length,
(index) {
final fullDayEventList =
controller.getFullDayEvent(filteredDates[index]);
widget.controller.getFullDayEvent(filteredDates[index]);
return SizedBox(
width: weekTitleWidth,
child: fullDayEventBuilder.call(
width: widget.weekTitleWidth,
child: widget.fullDayEventBuilder.call(
fullDayEventList,
dates[index],
widget.dates[index],
),
);
},
Expand All @@ -254,19 +290,20 @@ class InternalWeekViewPage<T extends Object?> extends StatelessWidget {
child: SingleChildScrollView(
controller: scrollController,
child: SizedBox(
height: height,
width: width,
height: widget.height,
width: widget.width,
child: Stack(
children: [
CustomPaint(
size: Size(width, height),
size: Size(widget.width, widget.height),
painter: HourLinePainter(
lineColor: hourIndicatorSettings.color,
lineHeight: hourIndicatorSettings.height,
offset: timeLineWidth + hourIndicatorSettings.offset,
minuteHeight: heightPerMinute,
verticalLineOffset: verticalLineOffset,
showVerticalLine: showVerticalLine,
lineColor: widget.hourIndicatorSettings.color,
lineHeight: widget.hourIndicatorSettings.height,
offset: widget.timeLineWidth +
widget.hourIndicatorSettings.offset,
minuteHeight: widget.heightPerMinute,
verticalLineOffset: widget.verticalLineOffset,
showVerticalLine: widget.showVerticalLine,
startHour: startHour,
emulateVerticalOffsetBy: emulateVerticalOffsetBy,
endHour: endHour,
Expand Down Expand Up @@ -307,8 +344,8 @@ class InternalWeekViewPage<T extends Object?> extends StatelessWidget {
Align(
alignment: Alignment.centerRight,
child: SizedBox(
width: weekTitleWidth * filteredDates.length,
height: height,
width: widget.weekTitleWidth * filteredDates.length,
height: widget.height,
child: Row(
children: [
...List.generate(
Expand All @@ -318,14 +355,15 @@ class InternalWeekViewPage<T extends Object?> extends StatelessWidget {
? BoxDecoration(
border: Border(
right: BorderSide(
color: hourIndicatorSettings.color,
width: hourIndicatorSettings.height,
color: widget.hourIndicatorSettings.color,
width:
widget.hourIndicatorSettings.height,
),
),
)
: null,
height: height,
width: weekTitleWidth,
height: widget.height,
width: widget.weekTitleWidth,
child: Stack(
children: [
weekDetectorBuilder(
Expand All @@ -336,21 +374,22 @@ class InternalWeekViewPage<T extends Object?> extends StatelessWidget {
minuteSlotSize: minuteSlotSize,
),
EventGenerator<T>(
height: height,
height: widget.height,
date: filteredDates[index],
onTileTap: onTileTap,
onTileTap: widget.onTileTap,
onTileLongTap: onTileLongTap,
onTileDoubleTap: onTileDoubleTap,
width: weekTitleWidth,
eventArranger: eventArranger,
eventTileBuilder: eventTileBuilder,
scrollNotifier: scrollConfiguration,
width: widget.weekTitleWidth,
eventArranger: widget.eventArranger,
eventTileBuilder: widget.eventTileBuilder,
scrollNotifier:
widget.scrollConfiguration,
startHour: startHour,
events: controller.getEventsOnDay(
events: widget.controller.getEventsOnDay(
filteredDates[index],
includeFullDayEvents: false,
),
heightPerMinute: heightPerMinute,
heightPerMinute: widget.heightPerMinute,
endHour: endHour,
),
],
Expand All @@ -362,11 +401,11 @@ class InternalWeekViewPage<T extends Object?> extends StatelessWidget {
),
),
TimeLine(
timeLineWidth: timeLineWidth,
hourHeight: hourHeight,
height: height,
timeLineOffset: timeLineOffset,
timeLineBuilder: timeLineBuilder,
timeLineWidth: widget.timeLineWidth,
hourHeight: widget.hourHeight,
height: widget.height,
timeLineOffset: widget.timeLineOffset,
timeLineBuilder: widget.timeLineBuilder,
startHour: startHour,
showHalfHours: showHalfHours,
showQuarterHours: showQuarterHours,
Expand Down Expand Up @@ -396,9 +435,9 @@ class InternalWeekViewPage<T extends Object?> extends StatelessWidget {
List<DateTime> _filteredDate() {
final output = <DateTime>[];

final weekDays = this.weekDays.toList();
final weekDays = widget.weekDays.toList();

for (final date in dates) {
for (final date in widget.dates) {
if (weekDays.any((weekDay) => weekDay.index + 1 == date.weekday)) {
output.add(date);
}
Expand Down
12 changes: 10 additions & 2 deletions lib/src/week_view/week_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ class WeekViewState<T extends Object?> extends State<WeekView<T>> {
late double _height;
late double _timeLineWidth;
late double _hourHeight;
late double _lastScrollOffset;
late DateTime _currentStartDate;
late DateTime _currentEndDate;
late DateTime _maxDate;
Expand Down Expand Up @@ -368,6 +369,7 @@ class WeekViewState<T extends Object?> extends State<WeekView<T>> {
@override
void initState() {
super.initState();
_lastScrollOffset = widget.scrollOffset;

_startHour = widget.startHour;
_endHour = widget.endHour;
Expand All @@ -382,8 +384,7 @@ class WeekViewState<T extends Object?> extends State<WeekView<T>> {
_regulateCurrentDate();

_calculateHeights();
_scrollController =
ScrollController(initialScrollOffset: widget.scrollOffset);

_pageController = PageController(initialPage: _currentIndex);
_eventArranger = widget.eventArranger ?? SideEventArranger<T>();

Expand Down Expand Up @@ -533,6 +534,8 @@ class WeekViewState<T extends Object?> extends State<WeekView<T>> {
widget.emulateVerticalOffsetBy,
showWeekDayAtBottom: widget.showWeekDayAtBottom,
endHour: _endHour,
scrollOffset: _lastScrollOffset,
scrollListener: _scrollPageListener,
),
);
},
Expand Down Expand Up @@ -978,6 +981,11 @@ class WeekViewState<T extends Object?> extends State<WeekView<T>> {
/// Returns true if it does else false.
bool _showLiveTimeIndicator(List<DateTime> dates) =>
dates.any((date) => date.compareWithoutTime(DateTime.now()));

/// Listener for every week page ScrollController
void _scrollPageListener(ScrollController controller) {
_lastScrollOffset = controller.offset;
}
}

class WeekHeader {
Expand Down

0 comments on commit db4e2aa

Please sign in to comment.