Count number of user viewed the posts in ListView FLutter

Viewed 39

I am loading posts from firebase and want to call a function when user scroll to each new post. Basically I want to store user's id in 'Post' collection who view the post. But I am unable to get post ID on scrolling so that I update record on firebase.

1 Answers

There is no such easy way to do this, but you can use VisibilityDetector from visibility_detector package:

You can get the index of the last list item that is completely visible on screen by wrapping each list item with a VisibilityDetector.

  _visibleItem = 0;

itemBuilder: (context, index) {
  return VisibilityDetector(
    key: Key(index.toString()),
    onVisibilityChanged: (VisibilityInfo visibilty) {
      if (visibilty.visibleFraction == 1)
        setState(() {
           log(_visibleItem);
          _visibleItem = index;
        });
    },
    child: ListTile(title: Text("Index $index"))
  );
},
Related