Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Flutter – Scroll Down to Bottom or Top of List in ListView

  • Last Updated : 21 Aug, 2021

In this tutorial, we will learn how to scroll down to the bottom of a ListView in Flutter. Scrolling down to the bottom of the list is very boring and so here is a way to scroll directly to the bottom or top of the list.

Project Setup:

Below is a starter code so that you can follow along with this tutorial. This code generates an app with a list of 100 items.

Dart




import 'package:flutter/material.dart';
  
void main() {
  runApp(MyApp());
}
  
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Geeks For Geeks',
      theme: ThemeData(
        primarySwatch: Colors.green,
      ),
      home: MyHomePage(title: 'Geeks For Geeks'),
    );
  }
}
  
class MyHomePage extends StatefulWidget {
  MyHomePage({Key? key, required this.title}) : super(key: key);
  final String title;
  
  @override
  _MyHomePageState createState() => _MyHomePageState();
}
  
class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
        
      // Floating action button. Functionality to be implemented
      floatingActionButton: FloatingActionButton(
        onPressed: () {},
        isExtended: true,
        tooltip: "Scroll to Bottom",
        child: Icon(Icons.arrow_downward),
      ),
        
      // Simple List of 100 items
      body: ListView.builder(
        itemCount: 100,
        itemBuilder: (context, index) {
          return ListTile(
            title: Text("Item ${index + 1}"),
          );
        },
      ),
    );
  }
}

Run the code and the result will be as follows.

Starter code

Implementation:

As you can see our FloatingActionButton is not working as no function is assigned to it in the onPressed field. First of all, we will need a ScrollController to control our ListView. So create a ScrollController.



Dart




ScrollController listScrollController = ScrollController();

Now assign this ScrollController to the controller field of ListView.

Dart




controller: listScrollController,

Now come to the onPressed() field of FloatingActionButton.

Here we will use the following code:

Dart




// We will jump to the bottom of the list
  
onPressed: () {
  if (listScrollController.hasClients) {
    final position = listScrollController.position.maxScrollExtent;
    listScrollController.jumpTo(position);
  }
},

We have first checked whether we can communicate with the members of ScrollController using

Dart






listScrollController.hasClients

If not checked, we will get a runtime error. The above code returns a boolean value. Now moving to the next part, we asked for the maximum scrolling extent of the ScrollController

Dart




final position = listScrollController.position.maxScrollExtent;

This simply means we are asking for the last position in the ListView via ScrollController. And then, we asked the ScrollController to move the position we got above using the jumpTo function.

Dart




listScrollController.jumpTo(position);

Now run the code and you will see the following result.

Scroll down list

So we have successfully reached the bottom of our list. 

Now you may think that it would be nice if not moving abruptly to the bottom of the list, we will go easily. So the same can be achieved using the animateTo function.

Replace the jumpTo code with the code below:

Dart




// We will reach the bottom of list within a duration of 
// 3 seconds animating down the ListView
  
listScrollController.animateTo(
  position,
  duration: Duration(seconds: 3),
  curve: Curves.easeOut,
);

Now run the app again. You will see the following result.



Animated Scrolling

We have learned how to scroll down the list, now we will learn how to scroll to the top of the ListView.

Instead of getting the maxScrollExtent, we will now use the minScrollExtent from ScrollController and that simply means the top position of ListView.

Here is the code and don’t forget to change the Icon:

Dart




// Floating action button is implemented with the above functionality 
// and onPressed triggers the function
  
floatingActionButton: FloatingActionButton(
  onPressed: () {
    if (listScrollController.hasClients) {
      final position = listScrollController.position.minScrollExtent;
      listScrollController.animateTo(
        position,
        duration: Duration(seconds: 3),
        curve: Curves.easeOut,
      );
    }
  },
  isExtended: true,
  tooltip: "Scroll to Top",
  child: Icon(Icons.arrow_upward),
),

Now run the app again.

Scrolling top of ListView

Complete Source Code:

Dart




import 'package:flutter/material.dart';
  
void main() {
  runApp(MyApp());
}
  
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Geeks For Geeks',
      theme: ThemeData(
        primarySwatch: Colors.green,
      ),
      home: MyHomePage(title: 'Geeks For Geeks'),
    );
  }
}
  
class MyHomePage extends StatefulWidget {
  MyHomePage({Key? key, required this.title}) : super(key: key);
  final String title;
  
  @override
  _MyHomePageState createState() => _MyHomePageState();
}
  
class _MyHomePageState extends State<MyHomePage> {
  ScrollController listScrollController = ScrollController();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
        
      // Floating action button implemented with the
      // auto scroll function to the bottom of list 
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          if (listScrollController.hasClients) {
            final position = listScrollController.position.maxScrollExtent;
            listScrollController.animateTo(
              position,
              duration: Duration(seconds: 3),
              curve: Curves.easeOut,
            );
          }
        },
        isExtended: true,
        tooltip: "Scroll to Bottom",
        child: Icon(Icons.arrow_downward),
      ),
        
      // ListView with 100 list items
      body: ListView.builder(
          
        // Scroll Controller for functionality
        controller: listScrollController,
        itemCount: 100,
        itemBuilder: (context, index) {
          return ListTile(
            title: Text("Item ${index + 1}"),
          );
        },
      ),
    );
  }
}

Output:

Scroll to the bottom of the list

Scroll to the top of the list

Conclusion:

In this tutorial, we have learned a very interesting feature of ListView that you may have come across on many websites and blogs. So now you can also implement the same feature in your app.




My Personal Notes arrow_drop_up
Recommended Articles
Page :