Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ class _DemoWeekView extends StatelessWidget {
DateTime date = DateTime(now.year, now.month, now.day);
return WeekView(
initialTime: const HourMinute(hour: 7),
initialDateIndex: 2,
scrollToCurrentTime: false,
dates: [date.subtract(const Duration(days: 1)), date, date.add(const Duration(days: 1))],
events: [
FlutterWeekViewEvent(
Expand Down
62 changes: 10 additions & 52 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
@@ -1,20 +1,6 @@
# Generated by pub
# See https://dart.dev/tools/pub/glossary#lockfile
packages:
archive:
dependency: transitive
description:
name: archive
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.13"
args:
dependency: transitive
description:
name: args
url: "https://pub.dartlang.org"
source: hosted
version: "1.6.0"
async:
dependency: transitive
description:
Expand All @@ -36,27 +22,27 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.3"
collection:
clock:
dependency: transitive
description:
name: collection
name: clock
url: "https://pub.dartlang.org"
source: hosted
version: "1.14.12"
convert:
version: "1.0.1"
collection:
dependency: transitive
description:
name: convert
name: collection
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.1"
crypto:
version: "1.14.12"
fake_async:
dependency: transitive
description:
name: crypto
name: fake_async
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.4"
version: "1.1.0"
flutter:
dependency: "direct main"
description: flutter
Expand All @@ -79,13 +65,6 @@ packages:
relative: true
source: path
version: "0.3.1"
image:
dependency: transitive
description:
name: image
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.12"
matcher:
dependency: transitive
description:
Expand All @@ -106,14 +85,7 @@ packages:
name: path
url: "https://pub.dartlang.org"
source: hosted
version: "1.6.4"
petitparser:
dependency: transitive
description:
name: petitparser
url: "https://pub.dartlang.org"
source: hosted
version: "2.4.0"
version: "1.7.0"
platform_detect:
dependency: transitive
description:
Expand All @@ -135,13 +107,6 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.4.4"
quiver:
dependency: transitive
description:
name: quiver
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.3"
sky_engine:
dependency: transitive
description: flutter
Expand Down Expand Up @@ -231,13 +196,6 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.8"
xml:
dependency: transitive
description:
name: xml
url: "https://pub.dartlang.org"
source: hosted
version: "3.6.1"
sdks:
dart: ">=2.6.0 <3.0.0"
flutter: ">=1.12.13+hotfix.5 <2.0.0"
105 changes: 82 additions & 23 deletions lib/src/week_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@ typedef DayViewStyleBuilder = DayViewStyle Function(DateTime date);
typedef DateCreator = DateTime Function(int index);

/// A (scrollable) week view which is able to display events, zoom and un-zoom and more !
class WeekView extends ZoomableHeadersWidget<WeekViewStyle, WeekViewController> {
class WeekView
extends ZoomableHeadersWidget<WeekViewStyle, WeekViewController> {
/// The number of dates.
final int dateCount;

/// The index of the first visible date. If this is set, [scrollToCurrentTime] should be false, since
/// that takes priority over [initialDateIndex].
final int initialDateIndex;

/// The date creator.
final DateCreator dateCreator;

Expand All @@ -34,6 +39,7 @@ class WeekView extends ZoomableHeadersWidget<WeekViewStyle, WeekViewController>
List<FlutterWeekViewEvent> events,
@required List<DateTime> dates,
this.dayViewStyleBuilder = DefaultBuilders.defaultDayViewStyleBuilder,
int initialDateIndex,
WeekViewStyle style,
WeekViewController controller,
bool inScrollableWidget = true,
Expand All @@ -45,8 +51,12 @@ class WeekView extends ZoomableHeadersWidget<WeekViewStyle, WeekViewController>
HoursColumnTappedDownCallback onHoursColumnTappedDown,
}) : assert(dates != null && dates.isNotEmpty),
assert(dayViewStyleBuilder != null),
assert(initialDateIndex == null ||
(initialDateIndex >= 0 && initialDateIndex < dates.length)),
initialDateIndex = initialDateIndex ?? 0,
dateCount = dates?.length ?? 0,
dateCreator = ((index) => DefaultBuilders.defaultDateCreator(dates, index)),
dateCreator =
((index) => DefaultBuilders.defaultDateCreator(dates, index)),
events = events ?? [],
super(
style: style ?? const WeekViewStyle(),
Expand All @@ -66,6 +76,7 @@ class WeekView extends ZoomableHeadersWidget<WeekViewStyle, WeekViewController>
this.dateCount,
@required this.dateCreator,
this.dayViewStyleBuilder = DefaultBuilders.defaultDayViewStyleBuilder,
int initialDateIndex,
WeekViewStyle style,
WeekViewController controller,
bool inScrollableWidget = true,
Expand All @@ -78,6 +89,9 @@ class WeekView extends ZoomableHeadersWidget<WeekViewStyle, WeekViewController>
}) : assert(dateCount == null || dateCount >= 0),
assert(dateCreator != null),
assert(dayViewStyleBuilder != null),
assert(initialDateIndex == null ||
(initialDateIndex >= 0 && initialDateIndex < dateCount)),
initialDateIndex = initialDateIndex ?? 0,
events = events ?? [],
super(
style: style ?? const WeekViewStyle(),
Expand Down Expand Up @@ -178,7 +192,11 @@ class _WeekViewState extends ZoomableHeadersWidgetState<WeekView> {
padding: EdgeInsets.only(left: widget.style.hoursColumnWidth),
controller: horizontalScrollController,
scrollDirection: Axis.horizontal,
physics: widget.inScrollableWidget ? MagnetScrollPhysics(itemSize: dayViewWidth + widget.style.dayViewSeparatorWidth) : const NeverScrollableScrollPhysics(),
physics: widget.inScrollableWidget
? MagnetScrollPhysics(
itemSize:
dayViewWidth + widget.style.dayViewSeparatorWidth)
: const NeverScrollableScrollPhysics(),
itemCount: widget.dateCount,
itemBuilder: (context, index) => createDayView(index),
),
Expand All @@ -196,10 +214,10 @@ class _WeekViewState extends ZoomableHeadersWidgetState<WeekView> {
date: date,
events: widget.events,
style: widget.dayViewStyleBuilder(date).copyWith(
dayBarHeight: 0,
hoursColumnWidth: 0,
hourRowHeight: widget.style.hourRowHeight,
),
dayBarHeight: 0,
hoursColumnWidth: 0,
hourRowHeight: widget.style.hourRowHeight,
),
controller: widget.controller.getDayViewController(date),
minimumTime: widget.minimumTime,
maximumTime: widget.maximumTime,
Expand Down Expand Up @@ -227,7 +245,10 @@ class _WeekViewState extends ZoomableHeadersWidgetState<WeekView> {
}

@override
bool get shouldScrollToCurrentTime => super.shouldScrollToCurrentTime && dayViewWidth != null && todayDateIndex != null;
bool get shouldScrollToCurrentTime =>
super.shouldScrollToCurrentTime &&
dayViewWidth != null &&
todayDateIndex != null;

@override
void scrollToCurrentTime() {
Expand All @@ -237,8 +258,24 @@ class _WeekViewState extends ZoomableHeadersWidgetState<WeekView> {
return;
}

double leftOffset = (todayDateIndex ?? 0) * (dayViewWidth + widget.style.dayViewSeparatorWidth);
horizontalScrollController.jumpTo(math.min<double>(leftOffset, horizontalScrollController.position.maxScrollExtent));
double leftOffset = (todayDateIndex ?? 0) *
(dayViewWidth + widget.style.dayViewSeparatorWidth);
horizontalScrollController.jumpTo(math.min<double>(
leftOffset, horizontalScrollController.position.maxScrollExtent));
}

@override
void scrollToTime(HourMinute time) {
super.scrollToTime(time);

if (horizontalScrollController == null || widget.initialDateIndex <= 0) {
return;
}

double leftOffset = widget.initialDateIndex *
(dayViewWidth + widget.style.dayViewSeparatorWidth);
horizontalScrollController.jumpTo(math.min<double>(
leftOffset, horizontalScrollController.position.maxScrollExtent));
}

@override
Expand Down Expand Up @@ -294,7 +331,8 @@ class _AutoScrollDayBarState extends State<_AutoScrollDayBar> {
scrollController.addListener(onScrolledHorizontally);
widget.stateScrollController.addListener(updateScrollPosition);

WidgetsBinding.instance.scheduleFrameCallback((_) => updateScrollPosition());
WidgetsBinding.instance
.scheduleFrameCallback((_) => updateScrollPosition());
}

@override
Expand All @@ -310,15 +348,29 @@ class _AutoScrollDayBarState extends State<_AutoScrollDayBar> {
height: widget.weekView.style.dayBarHeight,
child: ListView.builder(
itemCount: widget.weekView.dateCount,
itemBuilder: (context, position) => SizedBox(
width: calculateWidth(position),
child: DayBar.fromHeadersWidget(
parent: widget.weekView,
date: widget.weekView.dateCreator(position),
),
),
itemBuilder: (context, position) {
final date =
widget.weekView.dateCreator(position) ?? DateTime.now();
final dayViewStyle = widget.weekView.dayViewStyleBuilder(date);
return SizedBox(
width: calculateWidth(position),
// child: DayBar.fromHeadersWidget(
// parent: widget.weekView,
// date: widget.weekView.dateCreator(position),
// ),
child: DayBar(
date: date ?? DateTime.now(),
height: widget.weekView.style.dayBarHeight,
backgroundColor: widget.weekView.style.dayBarBackgroundColor,
textStyle: dayViewStyle?.dayBarTextStyle ??
widget.weekView.style.dayBarTextStyle,
dateFormatter: widget.weekView.style.dateFormatter,
),
);
},
physics: MagnetScrollPhysics(itemSize: calculateWidth()),
padding: EdgeInsets.only(left: widget.weekView.style.hoursColumnWidth),
padding:
EdgeInsets.only(left: widget.weekView.style.hoursColumnWidth),
controller: scrollController,
scrollDirection: Axis.horizontal,
),
Expand All @@ -332,16 +384,23 @@ class _AutoScrollDayBarState extends State<_AutoScrollDayBar> {
}

/// Returns the widget width.
double calculateWidth([int position]) => widget.dayViewWidth + (position == widget.weekView.dateCount ? 0 : widget.weekView.style.dayViewSeparatorWidth);
double calculateWidth([int position]) =>
widget.dayViewWidth +
(position == widget.weekView.dateCount
? 0
: widget.weekView.style.dayViewSeparatorWidth);

/// Triggered when this widget is scrolling horizontally.
void onScrolledHorizontally() => updateScrollBasedOnAnother(scrollController, widget.stateScrollController);
void onScrolledHorizontally() => updateScrollBasedOnAnother(
scrollController, widget.stateScrollController);

/// Triggered when the week view is scrolling horizontally.
void updateScrollPosition() => updateScrollBasedOnAnother(widget.stateScrollController, scrollController);
void updateScrollPosition() => updateScrollBasedOnAnother(
widget.stateScrollController, scrollController);

/// Updates a scroll controller position based on another scroll controller.
void updateScrollBasedOnAnother(ScrollController base, SilentScrollController target) {
void updateScrollBasedOnAnother(
ScrollController base, SilentScrollController target) {
if (!mounted) {
return;
}
Expand Down