quire-io / scroll-to-index

scroll to index with fixed/variable row height inside Flutter scrollable widget
MIT License
511 stars 104 forks source link

Possible to get current index based on current position? #26

Closed fadhlisulaimi closed 4 years ago

fadhlisulaimi commented 4 years ago

I am planning to return current index when user pop the navigation. Is it possible? For example, my screen is currently showing index 3 out of 15, when i scroll down the list...and lets say I am at index 6.. when i pop and come back to the screen, i want to maintain the last scrollable position which is index 6.

I have also implemented endless scrolling with this package, so the height of the listview will always increase and each item has dynamic height.

jerrywell commented 4 years ago

you can get the information when user tap on the row, before you push the view that will popped later. if you didn't count on the tap gesture, you can still get the value from the popped view, and get the index information that you rendered the list before (e.g. you create a cache map for your data and index).

fadhlisulaimi commented 4 years ago

Hi there,

Thanks for the reply. For my app, the user will not tap on the row (it's read-only content), so this method wont do.

Im not sure if I understood your second suggestion. I can create a cache map for the data/index but how do I know which index to return from the last scroll position. My list contains row of various heights depending on content and its endless scroll, so the user can scroll up to page 6 for eg

Right now im thinking of scrollNotification but i dont know if it's possible. Is there a method that is already defined in AutoScrollController that i can use?

fadhlisulaimi commented 4 years ago

Hi I just realise there is a pending commit for this. #25

I have integrated that solution and it works! I will just put the solution here

 autoScrollController.addListener((){

  int currentIndex = autoScrollController.currentTagIndexInViewport(preferPosition: AutoScrollPosition.begin)
AutoScrollTagState asts = autoScrollController.tagMap[currentIndex];

//you can get the data of your row from asts
(asts.widget.child as YourRowWidget).yourdata

});
jerrywell commented 4 years ago

the pending PR has the performance issue since it will loop all generated states. in your case, you can use this solution though. for better performance (e.g. you search it in ScrollNotification), just refer to https://github.com/flutter/flutter/issues/19941#issuecomment-486950990. :)

fadhlisulaimi commented 4 years ago

I see, thanks i will refer to that as well

bhanuka96 commented 4 years ago

Hi I just realise there is a pending commit for this. #25

I have integrated that solution and it works! I will just put the solution here

 autoScrollController.addListener((){

  int currentIndex = autoScrollController.currentTagIndexInViewport(preferPosition: AutoScrollPosition.begin)
AutoScrollTagState asts = autoScrollController.tagMap[currentIndex];

//you can get the data of your row from asts
(asts.widget.child as YourRowWidget).yourdata

});

how do I use currentTagIndexInViewport?

bhanuka96 commented 4 years ago

Hi I just realise there is a pending commit for this. #25

I have integrated that solution and it works! I will just put the solution here

 autoScrollController.addListener((){

  int currentIndex = autoScrollController.currentTagIndexInViewport(preferPosition: AutoScrollPosition.begin)
AutoScrollTagState asts = autoScrollController.tagMap[currentIndex];

//you can get the data of your row from asts
(asts.widget.child as YourRowWidget).yourdata

});

wrong index getting

diego-lipinski-de-castro commented 3 years ago

how is this nowadays?