From 289f1ea25a379e139b066f3305b0c6a7b0ae9eb4 Mon Sep 17 00:00:00 2001 From: Freek van de Ven Date: Thu, 17 Aug 2023 10:36:16 +0200 Subject: [PATCH] feat: scroll to currentTime when no items --- CHANGELOG.md | 4 ++ example/.metadata | 16 ++++---- example/pubspec.lock | 2 +- example/pubspec.yaml | 4 +- lib/src/timetable.dart | 87 +++++++++++++++++++++++++----------------- pubspec.yaml | 4 +- 6 files changed, 68 insertions(+), 49 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 35bec5f..32b6cc1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,3 +10,7 @@ * Added horizontal variant * Adjustable size for the component + +## [1.1.0] - 17 August 2023 + +* Set scrolling to the current time by default if there are no blocks diff --git a/example/.metadata b/example/.metadata index 5651284..d07f2f1 100644 --- a/example/.metadata +++ b/example/.metadata @@ -1,11 +1,11 @@ # This file tracks properties of this Flutter project. # Used by Flutter tool to assess capabilities and perform upgrades etc. # -# This file should be version controlled. +# This file should be version controlled and should not be manually edited. version: - revision: 52b3dc25f6471c27b2144594abb11c741cb88f57 - channel: stable + revision: "efbf63d9c66b9f6ec30e9ad4611189aa80003d31" + channel: "stable" project_type: app @@ -13,11 +13,11 @@ project_type: app migration: platforms: - platform: root - create_revision: 52b3dc25f6471c27b2144594abb11c741cb88f57 - base_revision: 52b3dc25f6471c27b2144594abb11c741cb88f57 - - platform: ios - create_revision: 52b3dc25f6471c27b2144594abb11c741cb88f57 - base_revision: 52b3dc25f6471c27b2144594abb11c741cb88f57 + create_revision: efbf63d9c66b9f6ec30e9ad4611189aa80003d31 + base_revision: efbf63d9c66b9f6ec30e9ad4611189aa80003d31 + - platform: web + create_revision: efbf63d9c66b9f6ec30e9ad4611189aa80003d31 + base_revision: efbf63d9c66b9f6ec30e9ad4611189aa80003d31 # User provided section diff --git a/example/pubspec.lock b/example/pubspec.lock index 829ab5e..c2762ee 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -174,7 +174,7 @@ packages: path: ".." relative: true source: path - version: "1.0.0" + version: "1.1.0" vector_math: dependency: transitive description: diff --git a/example/pubspec.yaml b/example/pubspec.yaml index 5fc46aa..6f2fffa 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -3,10 +3,10 @@ description: Timetable Widget publish_to: 'none' -version: 1.0.0+1 +version: 1.1.0+2 environment: - sdk: ">=2.17.6 <3.0.0" + sdk: ">=3.0.0 <4.0.0" dependencies: flutter: diff --git a/lib/src/timetable.dart b/lib/src/timetable.dart index f7c5eee..9294d72 100644 --- a/lib/src/timetable.dart +++ b/lib/src/timetable.dart @@ -22,6 +22,7 @@ class Timetable extends StatefulWidget { this.tableDirection = Axis.vertical, this.timeBlocks = const [], this.size, + this.initialScrollTime, this.scrollController, this.scrollPhysics, this.startHour = 0, @@ -63,6 +64,9 @@ class Timetable extends StatefulWidget { /// The theme of the timetable. final TableTheme theme; + /// The initial time to scroll to if there are no timeblocks. If nothing is provided it will scroll to the current time or to the first block if there is one. + final TimeOfDay? initialScrollTime; + /// The scroll controller to control the scrolling of the timetable. final ScrollController? scrollController; @@ -90,6 +94,8 @@ class _TimetableState extends State { widget.scrollController ?? ScrollController(initialScrollOffset: 0); if (widget.timeBlocks.isNotEmpty) { _scrollToFirstBlock(); + } else { + _scrollToInitialTime(); } } @@ -224,32 +230,28 @@ class _TimetableState extends State { ); } - Size _calculateTableStart(Axis axis) { - return Size( - (axis == Axis.horizontal) - ? _calculateTableTextSize().width / 2 - : _calculateTableTextSize().width + - widget.theme.tablePaddingStart + - widget.theme.tableTextOffset, - (axis == Axis.vertical) - ? _calculateTableTextSize().height / 2 - : _calculateTableTextSize().height, - ); - } + Size _calculateTableStart(Axis axis) => Size( + (axis == Axis.horizontal) + ? _calculateTableTextSize().width / 2 + : _calculateTableTextSize().width + + widget.theme.tablePaddingStart + + widget.theme.tableTextOffset, + (axis == Axis.vertical) + ? _calculateTableTextSize().height / 2 + : _calculateTableTextSize().height, + ); - Widget _showBlock(TimeBlock block, {double linePadding = 0}) { - return Block( - blockDirection: widget.tableDirection, - linePadding: linePadding, - start: block.start, - end: block.end, - startHour: widget.startHour, - hourDimension: widget.hourDimension, - blockDimension: widget.blockDimension, - blockColor: widget.blockColor, - child: block.child, - ); - } + Widget _showBlock(TimeBlock block, {double linePadding = 0}) => Block( + blockDirection: widget.tableDirection, + linePadding: linePadding, + start: block.start, + end: block.end, + startHour: widget.startHour, + hourDimension: widget.hourDimension, + blockDimension: widget.blockDimension, + blockColor: widget.blockColor, + child: block.child, + ); void _scrollToFirstBlock() { SchedulerBinding.instance.addPostFrameCallback((_) { @@ -271,19 +273,32 @@ class _TimetableState extends State { }); } - Size _calculateTableTextSize() { - return (TextPainter( - text: TextSpan( - text: '22:22', - style: widget.theme.timeStyle ?? Theme.of(context).textTheme.bodyLarge, - ), - maxLines: 1, - textScaleFactor: MediaQuery.of(context).textScaleFactor, - textDirection: TextDirection.ltr, - )..layout()) - .size; + void _scrollToInitialTime() { + SchedulerBinding.instance.addPostFrameCallback((_) { + var startingTime = widget.initialScrollTime ?? TimeOfDay.now(); + var initialOffset = + (widget.hourDimension * (widget.endHour - widget.startHour)) * + ((startingTime.hour - widget.startHour) / + (widget.endHour - widget.startHour)) + + _calculateTableTextSize().width / 2; + _scrollController.jumpTo( + initialOffset, + ); + }); } + Size _calculateTableTextSize() => (TextPainter( + text: TextSpan( + text: '22:22', + style: + widget.theme.timeStyle ?? Theme.of(context).textTheme.bodyLarge, + ), + maxLines: 1, + textScaleFactor: MediaQuery.of(context).textScaleFactor, + textDirection: TextDirection.ltr, + )..layout()) + .size; + double calculateTableHeight() { var sum = 0.0; if (widget.mergeBlocks || widget.combineBlocks) { diff --git a/pubspec.yaml b/pubspec.yaml index fc523ae..e326deb 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,10 +1,10 @@ name: timetable description: Flutter package to create a Timetable Widget that display blocks of widgets inside a timetable. -version: 1.0.0 +version: 1.1.0 repository: https://github.com/Iconica-Development/timetable environment: - sdk: ">=2.18.0 <3.0.0" + sdk: ">=3.0.0 <4.0.0" flutter: ">=1.17.0" dependencies: