Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find out which items in a ListView are visible

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

like image 804
DolDurma Avatar asked Jul 12 '19 17:07

DolDurma


3 Answers

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.

like image 199
creativecreatorormaybenot Avatar answered Oct 19 '22 19:10

creativecreatorormaybenot


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

like image 39
Ashkan Sarlak Avatar answered Oct 19 '22 17:10

Ashkan Sarlak


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,
  )
like image 1
K.Amanov Avatar answered Oct 19 '22 18:10

K.Amanov