robert-luoqing / flutter_list_view

MIT License
47 stars 17 forks source link

Keep track of visible items #2

Closed royvangeel closed 8 months ago

royvangeel commented 2 years ago

Hi there! Thanks for developing this package, works great. What I'm currently missing is to keep track of the visible items, I want to know what's the first visible item in the list; kind of functionality scrollable_positioned_list offers

robert-bitguild commented 2 years ago

@royvangeel To track visible items of flutter_list_view, please reference the below code.

FlutterSliverListController flutterListViewController =
      FlutterSliverListController();

@override
initState() {
/// ...
flutterListViewController.onPaintItemPositionsCallback =
    (height, positions) {
        // height is widget's height
        // positions is the items which render in viewports
            for (var pos in positions) {
            // index is item index of list
            // offset is based on viewport
        print("index:${pos.index} offset:${pos.offset}");
            }
    };

    super.initState();
}

FlutterSliverList(
    controller: flutterListViewController,
    delegate: FlutterListViewDelegate(
        (BuildContext context, int index) => Container(
              color: Colors.white,
              child: ListTile(
                  title: Text('List Item ${data[index]}')),
            ),
        childCount: data.length)),

// Above example will print like below, First item's index is 33, the y coordinate is -53 height is 2.93-(-53) flutter: index:33 offset:-53.06911662310972 flutter: index:34 offset:2.9308833768902787 flutter: index:35 offset:58.93088337689028 flutter: index:36 offset:114.93088337689028 flutter: index:37 offset:170.93088337689028 flutter: index:38 offset:226.93088337689028 flutter: index:39 offset:282.9308833768903 flutter: index:40 offset:338.9308833768903 flutter: index:41 offset:394.9308833768903 flutter: index:42 offset:450.9308833768903 flutter: index:43 offset:506.9308833768903 flutter: index:44 offset:562.9308833768903

I Hope this can be helpful for you.

alexkmbk commented 1 year ago

Is it same as flutterListViewController.sliverController.onPaintItemPositionsCallback for the FlutterListView widget?