feat: scroll to currentTime when no items

This commit is contained in:
Freek van de Ven 2023-08-17 10:36:16 +02:00
parent ae8d135eb4
commit 289f1ea25a
6 changed files with 68 additions and 49 deletions

View file

@ -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

View file

@ -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

View file

@ -174,7 +174,7 @@ packages:
path: ".."
relative: true
source: path
version: "1.0.0"
version: "1.1.0"
vector_math:
dependency: transitive
description:

View file

@ -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:

View file

@ -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<Timetable> {
widget.scrollController ?? ScrollController(initialScrollOffset: 0);
if (widget.timeBlocks.isNotEmpty) {
_scrollToFirstBlock();
} else {
_scrollToInitialTime();
}
}
@ -224,8 +230,7 @@ class _TimetableState extends State<Timetable> {
);
}
Size _calculateTableStart(Axis axis) {
return Size(
Size _calculateTableStart(Axis axis) => Size(
(axis == Axis.horizontal)
? _calculateTableTextSize().width / 2
: _calculateTableTextSize().width +
@ -235,10 +240,8 @@ class _TimetableState extends State<Timetable> {
? _calculateTableTextSize().height / 2
: _calculateTableTextSize().height,
);
}
Widget _showBlock(TimeBlock block, {double linePadding = 0}) {
return Block(
Widget _showBlock(TimeBlock block, {double linePadding = 0}) => Block(
blockDirection: widget.tableDirection,
linePadding: linePadding,
start: block.start,
@ -249,7 +252,6 @@ class _TimetableState extends State<Timetable> {
blockColor: widget.blockColor,
child: block.child,
);
}
void _scrollToFirstBlock() {
SchedulerBinding.instance.addPostFrameCallback((_) {
@ -271,18 +273,31 @@ class _TimetableState extends State<Timetable> {
});
}
Size _calculateTableTextSize() {
return (TextPainter(
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,
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;

View file

@ -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: