From 52d0b8f5da90a0ef9d548161f587a9cf4123cad7 Mon Sep 17 00:00:00 2001 From: Freek van de Ven Date: Fri, 10 May 2024 13:14:06 +0200 Subject: [PATCH] feat: add ability to sort on starttime --- CHANGELOG.md | 6 +++++- lib/src/timetable.dart | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e3f03ee..796a841 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,4 +29,8 @@ ## [1.4.0] - 13 November 2023 -* Add the option for sorting the blocks by their id. \ No newline at end of file +* Add the option for sorting the blocks by their id. + +## [1.5.0] - 10 May 2024 + +* Added the option to sort on the starttime of an event. diff --git a/lib/src/timetable.dart b/lib/src/timetable.dart index ee09526..cdc2b0a 100644 --- a/lib/src/timetable.dart +++ b/lib/src/timetable.dart @@ -35,6 +35,7 @@ class Timetable extends StatefulWidget { this.mergeBlocks = false, this.combineBlocks = true, this.sortByIdAscending = false, + this.sortByStartTime = false, this.onOverScroll, this.onUnderScroll, this.scrollTriggerOffset = 120, @@ -101,6 +102,9 @@ class Timetable extends StatefulWidget { /// Whether or not to sort blocks by their ID in ascending order. final bool sortByIdAscending; + /// Whether or not to sort blocks by their StartTime. + final bool sortByStartTime; + /// The offset which trigger the jump to either the previous or next page. Can't be lower then [scrollJumpToOffset]. final double scrollTriggerOffset; @@ -156,6 +160,12 @@ class _TimetableState extends State { } } + int compareTimeOfDay(TimeOfDay time1, TimeOfDay time2) { + var totalMinutes1 = time1.hour * 60 + time1.minute; + var totalMinutes2 = time2.hour * 60 + time2.minute; + return totalMinutes1.compareTo(totalMinutes2); + } + @override void dispose() { if (widget.scrollController == null) { @@ -180,6 +190,10 @@ class _TimetableState extends State { )); } + if (widget.sortByStartTime) { + blocks.sort((a, b) => compareTimeOfDay(a.start, b.start)); + } + var linePadding = _calculateTableTextSize().width; return SizedBox( width: widget.size?.width,