Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scrollable_positioned_list: expose position, offset and add jumpToOffset in ScrollOffsetController #515

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,10 @@ 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;
Offset get offset => _scrollableListState!.primary.scrollController.offset;

Future<void> animateScroll(
{required double offset,
required Duration duration,
Expand All @@ -293,6 +297,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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});


}