Skip to content

Software Development at Program Tom LTD

Place for coding, programming, development and software in general.

Menu
  • Blog
  • PDF Booklets
  • Dev Utils & Content
  • Java Spring Boot Or Web Apps
  • English
    • български
    • English
    • Español
    • Português
    • हिन्दी
    • Русский
    • Deutsch
    • Français
    • Italiano
    • العربية
  • About Us
Menu
How to detect what Elements in a ListView in Flutter are visible

How to detect what Elements in a ListView in Flutter are Visible

Posted on May 18, 2025 by Toma Velev

To detect which Elements in a ListView are currently Visible in Flutter, you can use the VisibilityDetector package or the ScrollablePositionedList package. Here’s how to do it with both approaches:


✅ Option 1: Using VisibilityDetector (More General Purpose)

  1. Add the dependency:
dependencies:
  visibility_detector: ^0.4.0
  1. Wrap each item in the list with VisibilityDetector:
import 'package:flutter/material.dart';
import 'package:visibility_detector/visibility_detector.dart';

class MyListView extends StatelessWidget {
  final List<String> items = List.generate(100, (index) => 'Item $index');

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      itemCount: items.length,
      itemBuilder: (context, index) {
        return VisibilityDetector(
          key: Key('item-$index'),
          onVisibilityChanged: (info) {
            if (info.visibleFraction > 0) {
              print('Item $index is visible with ${info.visibleFraction * 100}%');
            }
          },
          child: ListTile(title: Text(items[index])),
        );
      },
    );
  }
}

✅ Option 2: Using ScrollablePositionedList (Efficient for Large Lists)

  1. Add the dependency:
dependencies:
  scrollable_positioned_list: ^0.3.5
  1. Use it like this:
import 'package:flutter/material.dart';
import 'package:scrollable_positioned_list/scrollable_positioned_list.dart';

class MyListView extends StatefulWidget {
  @override
  _MyListViewState createState() => _MyListViewState();
}

class _MyListViewState extends State<MyListView> {
  final ItemScrollController itemScrollController = ItemScrollController();
  final ItemPositionsListener itemPositionsListener = ItemPositionsListener.create();

  final List<String> items = List.generate(100, (index) => 'Item $index');

  @override
  void initState() {
    super.initState();
    itemPositionsListener.itemPositions.addListener(() {
      final visibleItems = itemPositionsListener.itemPositions.value
          .where((position) => position.itemLeadingEdge >= 0 && position.itemTrailingEdge <= 1)
          .map((e) => e.index)
          .toList();
      print('Visible items: $visibleItems');
    });
  }

  @override
  Widget build(BuildContext context) {
    return ScrollablePositionedList.builder(
      itemCount: items.length,
      itemBuilder: (context, index) => ListTile(title: Text(items[index])),
      itemScrollController: itemScrollController,
      itemPositionsListener: itemPositionsListener,
    );
  }
}

📝 Notes

  • VisibilityDetector is more flexible and works with any scrollable.
  • ScrollablePositionedList is more efficient for tracking visible items and gives you better control for things like programmatically scrolling to an item. Here is also how to implement lazy loading: https://programtom.com/dev/2025/04/23/lazy-loading-with-scrollcontroller-in-flutter/
  • Skipping AI? You’re a Relic – Time to Evolve or Perish!
  • 2026 Flutter Launch Blueprint: Your 10-Step Checklist to App Store Domination
  • Product Requirements Document – for different software development levels
  • The Bottleneck Factory: AI Production Line vs. Human Quality Gate
  • Most Important Things You Could Code in 2026

Categories

  • Apps (25)
  • ChatGPT (26)
  • Choosing a Framework (38)
  • Flutter (280)
  • Graphical User Interface (14)
  • Marketing (119)
  • Software Development (292)
  • Spring (45)
  • StartUp (22)
  • Uncategorized (14)
  • Uncategorized (4)
  • Vaadin (16)

Tags

Algorithms (9) crypto (29) flutterdev (39) General (86) Java (7) QR & Bar Codes (3) Software Dev Choices (33) Spring Boot (1) standards (1) Theme (3) User Authentication & Authorization (9) User Experience (10) Utilities (19) WordPress (11)

Product categories

  • All Technologies (87)
    • Flutter Apps (26)
    • GPT (4)
    • Java (39)
    • Native Android (3)
    • PHP (9)
    • Spring (Boot) / Quarkus (36)
    • Utils (15)
    • Vaadin 24+ (28)
    • Vaadin 8 (1)
  • Apps (18)
    • Employees DB (1)
    • Notes (6)
    • Personal Budget (1)
    • Recipes Book (1)
    • Stuff Organizer (1)
    • To-Do (2)
  • PDF Books (3)
  • Source Code Generators (8)

Recent Posts

  • Skipping AI? You’re a Relic – Time to Evolve or Perish!
  • 2026 Flutter Launch Blueprint: Your 10-Step Checklist to App Store Domination
  • Product Requirements Document – for different software development levels
  • The Bottleneck Factory: AI Production Line vs. Human Quality Gate
  • Most Important Things You Could Code in 2026

Post Categories

  • Apps (25)
  • ChatGPT (26)
  • Choosing a Framework (38)
  • Flutter (280)
  • Graphical User Interface (14)
  • Marketing (119)
  • Software Development (292)
  • Spring (45)
  • StartUp (22)
  • Uncategorized (14)
  • Uncategorized (4)
  • Vaadin (16)