From 271dc86f269cbce9940f97659b8a9ae6b450b465 Mon Sep 17 00:00:00 2001 From: yakagami <92773903+yakagami@users.noreply.github.com> Date: Tue, 6 Feb 2024 12:47:29 -0500 Subject: [PATCH 1/3] scrollable_positioned_list: expose position and add jumpToOffset in ScrollOffsetController --- .../lib/src/scrollable_positioned_list.dart | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/packages/scrollable_positioned_list/lib/src/scrollable_positioned_list.dart b/packages/scrollable_positioned_list/lib/src/scrollable_positioned_list.dart index 60045e9b..c2fa3c78 100644 --- a/packages/scrollable_positioned_list/lib/src/scrollable_positioned_list.dart +++ b/packages/scrollable_positioned_list/lib/src/scrollable_positioned_list.dart @@ -279,6 +279,9 @@ class ItemScrollController { /// This is an experimental API and is subject to change. /// Behavior may be ill-defined in some cases. Please file bugs. class ScrollOffsetController { + + ScrollPosition get position => _scrollableListState!.primary.scrollController.position; + Future animateScroll( {required double offset, required Duration duration, @@ -293,6 +296,12 @@ class ScrollOffsetController { ); } + void jumpToOffset(double offset){ + final currentPosition = _scrollableListState!.primary.scrollController.offset; + final newPosition = currentPosition + offset; + _scrollableListState!.primary.scrollController.jumpTo(newPosition); + } + _ScrollablePositionedListState? _scrollableListState; void _attach(_ScrollablePositionedListState scrollableListState) { From e463f5fdcb181dbade95a27ecce6e8d8ffdf6a62 Mon Sep 17 00:00:00 2001 From: yakagami <92773903+yakagami@users.noreply.github.com> Date: Tue, 6 Feb 2024 13:00:04 -0500 Subject: [PATCH 2/3] add test for jumpToOffset --- .../test/scroll_offset_controller_test.dart | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/packages/scrollable_positioned_list/test/scroll_offset_controller_test.dart b/packages/scrollable_positioned_list/test/scroll_offset_controller_test.dart index 31ffc7f3..03a3f593 100644 --- a/packages/scrollable_positioned_list/test/scroll_offset_controller_test.dart +++ b/packages/scrollable_positioned_list/test/scroll_offset_controller_test.dart @@ -225,4 +225,28 @@ void main() { await tester.pumpAndSettle(); }); + + testWidgets('Programtically jump down 50 pixels', + (WidgetTester tester) async { + final scrollDistance = 50.0; + + ScrollOffsetController scrollOffsetController = ScrollOffsetController(); + + await setUpWidgetTest( + tester, + scrollOffsetController: scrollOffsetController, + initialIndex: 5, + ); + + final originalOffest = tester.getTopLeft(find.text('Item 5')).dy; + + scrollOffsetController.jumpToOffset(-scrollDistance); + await tester.pumpAndSettle(); + + final newOffset = tester.getTopLeft(find.text('Item 5')).dy; + + expect(newOffset - originalOffest, scrollDistance); + }); + + } From aa7c8541cc092948237594c45067f8b1e4fb1e87 Mon Sep 17 00:00:00 2001 From: yakagami <92773903+yakagami@users.noreply.github.com> Date: Tue, 6 Feb 2024 13:08:21 -0500 Subject: [PATCH 3/3] expose offset as well. --- .../lib/src/scrollable_positioned_list.dart | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/scrollable_positioned_list/lib/src/scrollable_positioned_list.dart b/packages/scrollable_positioned_list/lib/src/scrollable_positioned_list.dart index c2fa3c78..3af4bf12 100644 --- a/packages/scrollable_positioned_list/lib/src/scrollable_positioned_list.dart +++ b/packages/scrollable_positioned_list/lib/src/scrollable_positioned_list.dart @@ -281,6 +281,7 @@ class ItemScrollController { class ScrollOffsetController { ScrollPosition get position => _scrollableListState!.primary.scrollController.position; + Offset get offset => _scrollableListState!.primary.scrollController.offset; Future animateScroll( {required double offset,