How does flutter scroll to an item position in the listview according to its index?

27 Views Asked by At

How does flutter scroll to an item position in the listview according to its index?

A list scrolls to a specific item, but the height of the item may be uncertain

for example: scroll to the 22nd ListTile in the list

1

There are 1 best solutions below

0
AmirHossein On

Unfortunately, Flutter doesn't have controller.jumpToIndex(index);

you can achieve this by adding a third-party library which is called scrollable_positioned_list.

It is exactly like ListView but you can use controller.jumpToIndex(index);

For Example:

Init controller:

final ItemScrollController itemScrollController = ItemScrollController();

Add widget:

 ScrollablePositionedList.builder(
  itemCount: 500,
  itemBuilder: (context, index) => Text('Item $index'),
  itemScrollController: itemScrollController,
  scrollOffsetController: scrollOffsetController,
  itemPositionsListener: itemPositionsListener,
  scrollOffsetListener: scrollOffsetListener,
);

Call scrollTo method:

itemScrollController.scrollTo(
index: 150,
duration: Duration(seconds: 2),
curve: Curves.easeInOutCubic);

Happy Coding :)