scrollview_observer 0.1.0 copy "scrollview_observer: ^0.1.0" to clipboard
scrollview_observer: ^0.1.0 copied to clipboard

Gets all the child widgets those are displaying in listView.

Flutter ScrollView Observer #

author pub stars

Language: English | 中文 | Article

This is a library of widget that can be used to listen for child widgets those are being displayed in the scroll view.

Support #

  • ListView
  • SliverList
  • GridView
  • SliverGrid

Installing #

Add scrollview_observer to your pubspec.yaml file:

dependencies:
  scrollview_observer: latest_version

Import scrollview_observer in files that it will be used:

import 'package:scrollview_observer/scrollview_observer.dart';

Getting Started #

Take ListView as an example

BuildContext? _sliverListViewContext;

Create a ListView and record BuildContext in its builder callback

ListView _buildListView() {
  return ListView.separated(
    itemBuilder: (ctx, index) {
      if (_sliverListViewContext != ctx) {
        _sliverListViewContext = ctx;
      }
      ...
    },
    ...
  );
}

Create ListViewObserver

  • child: Create ListView as a child of ListViewObserver
  • sliverListContexts: In this callback, we need to return all BuildContext of the ListView those needs to be observed
  • onObserve: This callback can listen for information about the child widgets those are currently being displayed
ListViewObserver(
  child: _buildListView(),
  sliverListContexts: () {
    return [if (_sliverListViewContext != null) _sliverListViewContext!];
  },
  onObserve: (resultMap) {
    final model = resultMap[_sliverListViewContext];
    if (model == null) return;

    // Prints the first child widget index that is currently being displayed
    print('firstChild.index -- ${model.firstChild?.index}');

    // Prints the index of all child widgets those are currently being displayed
    print('displaying -- ${model.displayingChildIndexList}');
  },
)

By default, ListView relevant data will only be observed when rolling.

If needed, you can use ListViewOnceObserveNotification triggered an observation manually.

ListViewOnceObserveNotification().dispatch(_sliverListViewContext);

Example #

220
likes
150
pub points
97%
popularity

Publisher

verified publisherfullstackaction.com

Gets all the child widgets those are displaying in listView.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

flutter

More

Packages that depend on scrollview_observer