简体   繁体   中英

Count number of user viewed the posts in ListView FLutter

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.

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"))
  );
},

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM