diff --git a/CHANGELOG.md b/CHANGELOG.md index e36448d..e3f03ee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,3 +26,7 @@ ## [1.3.0] - 8 November 2023 * Add the option for setting an offset for the hours so that the first hour is not 00:00 but 08:00 for example and the last hour can be after 24:00. + +## [1.4.0] - 13 November 2023 + +* Add the option for sorting the blocks by their id. \ No newline at end of file diff --git a/example/lib/main.dart b/example/lib/main.dart index 3eac8df..78c80ef 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -34,7 +34,7 @@ class _TimetableDemoState extends State { ), ), ), - id: 1, + id: 3, ), TimeBlock( start: const TimeOfDay(hour: 10, minute: 0), @@ -50,7 +50,7 @@ class _TimetableDemoState extends State { ), ), childDimension: 300, - id: 3, + id: 1, ), TimeBlock( start: const TimeOfDay(hour: 14, minute: 0), @@ -150,6 +150,7 @@ class _TimetableDemoState extends State { onOverScroll: () {}, onUnderScroll: () {}, hoursOffset: 6, + sortByIdAscending: true, size: Size(size.width, size.height * 0.64), tableDirection: _horizontal ? Axis.horizontal : Axis.vertical, startHour: 0, diff --git a/lib/src/timetable.dart b/lib/src/timetable.dart index 7ffb95e..0940fc3 100644 --- a/lib/src/timetable.dart +++ b/lib/src/timetable.dart @@ -34,15 +34,21 @@ class Timetable extends StatefulWidget { this.theme = const TableTheme(), this.mergeBlocks = false, this.combineBlocks = true, + this.sortByIdAscending = false, this.onOverScroll, this.onUnderScroll, this.scrollTriggerOffset = 120, this.scrollJumpToOffset = 115, super.key, - }) : assert( - scrollTriggerOffset > scrollJumpToOffset, - 'ScrollTriggerOffset cannot be smaller' - ' then the scrollJumpToOffset.'); + }) : assert( + scrollTriggerOffset > scrollJumpToOffset, + 'ScrollTriggerOffset cannot be smaller' + ' then the scrollJumpToOffset.', + ), + assert( + !(mergeBlocks && sortByIdAscending), + 'mergeBlocks and sortByIdAscending' + ' cannot be enabled at the same time.'); /// The Axis in which the table is layed out. final Axis tableDirection; @@ -92,6 +98,9 @@ class Timetable extends StatefulWidget { /// If blocks have the same id and time they will be combined into one block. final bool combineBlocks; + /// Whether or not to sort blocks by their ID in ascending order. + final bool sortByIdAscending; + /// The offset which trigger the jump to either the previous or next page. Can't be lower then [scrollJumpToOffset]. final double scrollTriggerOffset; @@ -163,6 +172,14 @@ class _TimetableState extends State { } else { blocks = widget.timeBlocks; } + + if (widget.sortByIdAscending) { + // if the id is zero then put it at the end + blocks.sort((a, b) => (a.id != 0 ? a.id : double.infinity).compareTo( + (b.id != 0 ? b.id : double.infinity), + )); + } + var linePadding = _calculateTableTextSize().width; return SizedBox( width: widget.size?.width, diff --git a/pubspec.yaml b/pubspec.yaml index 47b06af..5be2b02 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: flutter_timetable description: Flutter package to create a Timetable Widget that display blocks of widgets inside a timetable. -version: 1.3.0 +version: 1.4.0 repository: https://github.com/Iconica-Development/timetable environment: