Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flutter ListView scroll to index not available

Tags:

flutter

dart

What I needed:

I want to scroll a list by some index, how can i do that.

What I know:

scrollToIndex should start from n index, but how can we scroll to any index?

like image 941
Deepak Gehlot Avatar asked Jan 04 '19 13:01

Deepak Gehlot


People also ask

How do I scroll at a specific index in ListView in flutter?

Typically used with [ListView], [GridView], [CustomScrollView]. AutoScrollController extends ScrollController which used in the below sample which contains the additional logic of scrolling. It's internally calculated offset value and performs scrolling till the given index.

How do you control scroll in flutter?

If you want to use this controller function, you can add a ScrollNotification and keep calling it over and over again when the scroll ends, until the stop button is pressed.

How do you find scroll position in flutter?

I used NotificationListener that is a widget that listens for notifications bubbling up the tree. Then use ScrollEndNotification , which indicates that scrolling has stopped. For scroll position I used _scrollController that type is ScrollController .


2 Answers

Unfortunately, ListView has no built-in approach to a scrollToIndex() function. You’ll have to develop your own way to measure to that element’s offset for animateTo() or jumpTo(), or you can search through these suggested solutions/plugins or from other posts like Flutter: Scrolling to a widget in ListView

(the general scrollToIndex issue is discussed at flutter/issues/12319 since 2017, but still with no current plans)


But there is a different kind of ListView that does support scrollToIndex (as mentioned by Slashbin):

  • ScrollablePositionedList
    • dependency: scrollable_positioned_list

You set it up exactly like ListView and works the same, except you now have access to a ItemScrollController that does:

  • jumpTo({index, alignment})
  • scrollTo({index, alignment, duration, curve})

Simplified example:

ItemScrollController _scrollController = ItemScrollController();  ScrollablePositionedList.builder(   itemScrollController: _scrollController,   itemCount: _myList.length,   itemBuilder: (context, index) {     return _myList[index];   }, )  _scrollController.scrollTo(index: 150, duration: Duration(seconds: 1)); 

(note that this library is developed by Google but not by the core Flutter team.)

like image 86
TWL Avatar answered Sep 22 '22 12:09

TWL


ScrollablePositionedList can be used for this. https://github.com/google/flutter.widgets/tree/master/packages/scrollable_positioned_list

Pub link - https://pub.dev/packages/scrollable_positioned_list

final ItemScrollController itemScrollController = ItemScrollController(); final ItemPositionsListener itemPositionListener = ItemPositionsListener.create(); 

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

One then can scroll to a particular item with:

itemScrollController.scrollTo(   index: 150,   duration: Duration(seconds: 2),   curve: Curves.easeInOutCubic); 
like image 21
Slashbin Avatar answered Sep 23 '22 12:09

Slashbin