Skip to content

[scrollable_positioned_list] expose ScrollPosition as unstableScrollPosition in ScrollOffsetController #535

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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,15 @@ 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 of the current ScrollablePositionedList. Values such as
/// pixels, maxScrollExtent and jumpTo are not necessarily defined to start
/// from the beginning of the list. When itemScrollController.jumpTo is
/// called, the list will begin from that index. unstableScrollPosition.jumpTo
/// will cause the application to freeze at very large values as it must build
/// all the widgets between the starting offset and the ending offset.
ScrollPosition get unstableScrollPosition =>
_scrollableListState!.primary.scrollController.position;

Future<void> animateScroll(
{required double offset,
required Duration duration,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,4 +225,30 @@ 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 originalOffset = tester.getTopLeft(find.text('Item 5')).dy;

final currentPosition =
scrollOffsetController.unstableScrollPosition.pixels;
final newPosition = currentPosition - scrollDistance;

scrollOffsetController.unstableScrollPosition.jumpTo(newPosition);
await tester.pumpAndSettle();

final newOffset = tester.getTopLeft(find.text('Item 5')).dy;

expect(newOffset - originalOffset, scrollDistance);
});
}