wrapByVisibilityDetector method

Widget wrapByVisibilityDetector(
  1. int index,
  2. Widget child
)

wrap widget by VisibilityDetector that can know if child is visible

Implementation

Widget wrapByVisibilityDetector(int index, Widget child) {
  return VisibilityDetector(
    key: ValueKey(index.toString()),
    onVisibilityChanged: (VisibilityInfo info) {
      final visibleFraction = info.visibleFraction;
      if (isForward) {
        visibleFraction == 0
            ? indexTreeSet.remove(index)
            : indexTreeSet.add(index);
      } else {
        visibleFraction == 1.0
            ? indexTreeSet.add(index)
            : indexTreeSet.remove(index);
      }
      if (indexTreeSet.isNotEmpty) {
        _tocController?.onIndexChanged(indexTreeSet.first);
      }
    },
    child: child,
  );
}