23

How can I find out which items are currently visible or invisible in a ListView?
For example, I have 100 items in ListView and when i scroll to top of screen or list, I want to detect which items appear or disappear from the viewport.

Illustration:

enter image description here

creativecreatorormaybenot
  • 91,704
  • 51
  • 240
  • 358
DolDurma
  • 13,596
  • 41
  • 168
  • 323
  • Why do you wanna know that? If you are going to make a list with 100 itens you should use `ListView.builder` witch you can pass a `itemBuilder`, that will build the 'widget' of a line in your `listView`. So the flutter will manage to you witch line is visible and render it, and clear from memory the invisible ones... – LgFranco Jul 12 '19 at 18:11
  • yes i know that as flutter can manage that. i want to use this feature for using another listeners to use that – DolDurma Jul 12 '19 at 18:13

3 Answers3

12

There is no easy way to do this. Here is the same question, however, it does not have an answer.

There is an active GitHub issue about this.

There are multiple solutions for the problem in that issue. This Gist features one that requires the rect_getter package.
Alternatively, you could take a look at this proposal.

TL;DR

This is not yet implemented if you are searching for an easy way to find it out. However, there are solutions, like the ones I mentioned above and from other packages, say VisibilityDetector from flutter_widgets.

creativecreatorormaybenot
  • 91,704
  • 51
  • 240
  • 358
1

There is a package for this purpose.

A VisibilityDetector widget wraps an existing Flutter widget and fires a callback when the widget's visibility changes.

Usage:

VisibilityDetector(
    key: Key('my-widget-key'),
    onVisibilityChanged: (visibilityInfo) {
      var visiblePercentage = visibilityInfo.visibleFraction * 100;
      debugPrint(
          'Widget ${visibilityInfo.key} is ${visiblePercentage}% visible');
    },
    child: someOtherWidget,
  )
K.Amanov
  • 855
  • 11
  • 19
1

You can also use inview_notifier_list. It's basically a normal ListView which defines a visible region and it's children get notified when they are in that region.

enter image description here

Ashkan Sarlak
  • 6,504
  • 6
  • 35
  • 46