How to Add Items to the Bottom of a ListView in Flutter – A Step-by-Step Guide

The ListView widget is essential for displaying long scrollable lists in Flutter. A common requirement is to dynamically add new items at the bottom as the user scrolls. This allows the implementation of infinite or paginated scrolling lists.

In this comprehensive guide, we will explore multiple techniques to add items to the bottom of a ListView in Flutter.

Prerequisites

Before going further, you should have:

  • Basic knowledge of Dart programming
  • Understanding of Flutter stateful widgets
  • Experience with ListView and ScrollController
  • Familiarity with callbacks like setState()

ScrollController Approach

The simplest way to add items at the bottom is using a ScrollController:

Step 1: Create a ScrollController

First, create a ScrollController property in the state class:

// State class
final ScrollController _scrollController = ScrollController();

Step 2: Pass the Controller to the ListView

Now pass this controller to the ListView constructor:

ListView(
  controller: _scrollController,
  children: [ ... ],
)

This allows controlling the scroll position.

Step 3: Animate Scroll on Item Add

When adding a new item, call animateTo() to scroll to the bottom:

_scrollController.animateTo(
  _scrollController.position.maxScrollExtent,
  duration: Duration(milliseconds: 300),
  curve: Curves.easeOut
);

This smoothly scrolls to the last position.

Step 4: Rebuild the ListView

Finally call setState() to rebuild the ListView:

setState(() {
  items.add("New item"); 
});

This will add the item and scroll to it.

Full Example:

class _MyHomePageState extends State<MyHomePage> {

  final ScrollController _scrollController = ScrollController();

  List<String> items = [];

  void _addNewItem() {
    setState(() {
      items.add("New item");
    });
    
    _scrollController.animateTo(
      _scrollController.position.maxScrollExtent,
      duration: Duration(milliseconds: 300),
      curve: Curves.easeOut,
    );
  }

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      controller: _scrollController,
      itemCount: items.length,
      itemBuilder: (context, index) {
        return Text(items[index]); 
      }
    );
  }
}

This provides complete control for programatically adding items and scrolling.

NotificationListener Approach

For efficiency, we can also use a NotificationListener to detect when the user scrolls to the bottom:

Step 1: Create Notification Listener

Wrap the ListView in a NotificationListener:

NotificationListener<ScrollNotification>(
  onNotification: (notification) {
    // Handle notification
  },
  child: ListView(),
)

Step 2: Check for Bottom Reach

In the callback, check if extentAfter reaches zero:

if (notification is ScrollEndNotification && 
    _scrollController.position.extentAfter == 0) {

  // Bottom reached
  
}

This indicates the bottom.

Step 3: Add Item

Add a new item and rebuild:

setState(() {
  items.add("New item");
});

Full Example:

class _MyHomePageState extends State<MyHomePage> {
  
  final ScrollController _scrollController = ScrollController();

  List<String> items = [];

  @override
  Widget build(BuildContext context) {

    return NotificationListener<ScrollNotification>(
      onNotification: (notification) {
        if (notification is ScrollEndNotification && 
            _scrollController.position.extentAfter == 0) {
            
          setState(() {
            items.add("New item");
          });
        }
        return true;
      },

      child: ListView.builder(
        controller: _scrollController,
        itemCount: items.length,
        itemBuilder: (context, index) {
          return Text(items[index]);
        },
      ),
    );
  }
}

This efficiently adds new items when scrolled to the bottom.

Comparison

ApproachProsCons
ScrollControllerPrecise controlNeed to manually scroll on each item add
NotificationListenerAutomatic scroll based on user reaching bottomCannot programmatically trigger item add

Summary

  • Use ScrollController to programatically scroll when adding items
  • Use NotificationListener to scroll on user reaching bottom
  • Call setState() to rebuild ListView after adding items
  • Prefer NotificationListener if user needs to trigger scrolling
  • Use ScrollController if programmatically adding items

This provides a complete guide to implementing infinite scroll and pagination with ListView in Flutter.

Let me know if you need any clarification or have additional questions!

Leave a Comment