From ed370d33420c3b7a4377b3728ce62da983d709ef Mon Sep 17 00:00:00 2001 From: hunghd Date: Fri, 26 Jun 2020 17:06:49 +0700 Subject: [PATCH 1/2] support customize dayBarTextStyle from DayViewStyle Currently, if using `dayBarTextStyle` from `WeekViewStyle`, this value is fixed, not be able to configure according to date value, so we cannot highlight the current date in day bar. My solution is to use `dayBarTextStyle` of `DayViewStyle` if it is provided. --- example/pubspec.lock | 62 ++++++--------------------------- lib/src/week_view.dart | 79 ++++++++++++++++++++++++++++++------------ 2 files changed, 66 insertions(+), 75 deletions(-) diff --git a/example/pubspec.lock b/example/pubspec.lock index a6700cf..af6a2f8 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -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: @@ -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 @@ -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: @@ -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: @@ -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 @@ -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" diff --git a/lib/src/week_view.dart b/lib/src/week_view.dart index 0eb9495..bc45ef3 100644 --- a/lib/src/week_view.dart +++ b/lib/src/week_view.dart @@ -16,7 +16,8 @@ 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 { +class WeekView + extends ZoomableHeadersWidget { /// The number of dates. final int dateCount; @@ -46,7 +47,8 @@ class WeekView extends ZoomableHeadersWidget }) : assert(dates != null && dates.isNotEmpty), assert(dayViewStyleBuilder != null), dateCount = dates?.length ?? 0, - dateCreator = ((index) => DefaultBuilders.defaultDateCreator(dates, index)), + dateCreator = + ((index) => DefaultBuilders.defaultDateCreator(dates, index)), events = events ?? [], super( style: style ?? const WeekViewStyle(), @@ -178,7 +180,11 @@ class _WeekViewState extends ZoomableHeadersWidgetState { 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), ), @@ -196,10 +202,10 @@ class _WeekViewState extends ZoomableHeadersWidgetState { 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, @@ -227,7 +233,10 @@ class _WeekViewState extends ZoomableHeadersWidgetState { } @override - bool get shouldScrollToCurrentTime => super.shouldScrollToCurrentTime && dayViewWidth != null && todayDateIndex != null; + bool get shouldScrollToCurrentTime => + super.shouldScrollToCurrentTime && + dayViewWidth != null && + todayDateIndex != null; @override void scrollToCurrentTime() { @@ -237,8 +246,10 @@ class _WeekViewState extends ZoomableHeadersWidgetState { return; } - double leftOffset = (todayDateIndex ?? 0) * (dayViewWidth + widget.style.dayViewSeparatorWidth); - horizontalScrollController.jumpTo(math.min(leftOffset, horizontalScrollController.position.maxScrollExtent)); + double leftOffset = (todayDateIndex ?? 0) * + (dayViewWidth + widget.style.dayViewSeparatorWidth); + horizontalScrollController.jumpTo(math.min( + leftOffset, horizontalScrollController.position.maxScrollExtent)); } @override @@ -294,7 +305,8 @@ class _AutoScrollDayBarState extends State<_AutoScrollDayBar> { scrollController.addListener(onScrolledHorizontally); widget.stateScrollController.addListener(updateScrollPosition); - WidgetsBinding.instance.scheduleFrameCallback((_) => updateScrollPosition()); + WidgetsBinding.instance + .scheduleFrameCallback((_) => updateScrollPosition()); } @override @@ -310,15 +322,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, ), @@ -332,16 +358,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; } From 4ca8342100f9aa854377c2204b4f300c69b2b393 Mon Sep 17 00:00:00 2001 From: hunghd Date: Mon, 29 Jun 2020 16:20:48 +0700 Subject: [PATCH 2/2] support configure initial date in WeekView add `initialDateIndex` to control the first date visible in WeekView --- example/lib/main.dart | 2 ++ lib/src/week_view.dart | 30 ++++++++++++++++++++++++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/example/lib/main.dart b/example/lib/main.dart index 346e573..265a0a7 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -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( diff --git a/lib/src/week_view.dart b/lib/src/week_view.dart index bc45ef3..576434e 100644 --- a/lib/src/week_view.dart +++ b/lib/src/week_view.dart @@ -21,6 +21,10 @@ class WeekView /// 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; @@ -35,6 +39,7 @@ class WeekView List events, @required List dates, this.dayViewStyleBuilder = DefaultBuilders.defaultDayViewStyleBuilder, + int initialDateIndex, WeekViewStyle style, WeekViewController controller, bool inScrollableWidget = true, @@ -46,6 +51,9 @@ class WeekView 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)), @@ -68,6 +76,7 @@ class WeekView this.dateCount, @required this.dateCreator, this.dayViewStyleBuilder = DefaultBuilders.defaultDayViewStyleBuilder, + int initialDateIndex, WeekViewStyle style, WeekViewController controller, bool inScrollableWidget = true, @@ -80,6 +89,9 @@ class WeekView }) : 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(), @@ -252,6 +264,20 @@ class _WeekViewState extends ZoomableHeadersWidgetState { 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( + leftOffset, horizontalScrollController.position.maxScrollExtent)); + } + @override void dispose() { horizontalScrollController?.dispose(); @@ -336,8 +362,8 @@ class _AutoScrollDayBarState extends State<_AutoScrollDayBar> { date: date ?? DateTime.now(), height: widget.weekView.style.dayBarHeight, backgroundColor: widget.weekView.style.dayBarBackgroundColor, - textStyle: - dayViewStyle?.dayBarTextStyle ?? widget.weekView.style.dayBarTextStyle, + textStyle: dayViewStyle?.dayBarTextStyle ?? + widget.weekView.style.dayBarTextStyle, dateFormatter: widget.weekView.style.dateFormatter, ), );