Listview scroll to be top when load more

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 {

`'package:flutter/material.dart'`1

`'package:flutter/material.dart'`3

'package:flutter/material.dart'`4'package:flutter/material.dart'5 'package:flutter/material.dart'`6

'package:flutter/material.dart'`7'package:flutter/material.dart'8'package:flutter/material.dart'9; `0

'package:flutter/material.dart'`7; `2

; `3; `4

'package:flutter/material.dart'`7; `6

'package:flutter/material.dart'`7; 8'package:flutter/material.dart'9; `6

`'package:flutter/material.dart'`4`void`2

}

}

class `void`7

`void`9`main() { `0`main() { `1

`main() { `3

`'package:flutter/material.dart'`1

`main() { `7

}

class ``0

`'package:flutter/material.dart'`1

`'package:flutter/material.dart'`3

'package:flutter/material.dart'`4'package:flutter/material.dart'`5 ``7

'package:flutter/material.dart'`7 `9

`; `3`runApp(MyApp()); `1

'package:flutter/material.dart'`7; `6

`'package:flutter/material.dart'`7`runApp(MyApp()); `5

`; `3`runApp(MyApp()); `7

; `3`runApp(MyApp()); `9} 0; `0

; `3} 3} 4; `0

; `3} `7

'package:flutter/material.dart'`7; `6

`'package:flutter/material.dart'`7`class`1

`; `3`class`3

`; `3`class`5

class`6'package:flutter/material.dart'`5 `class`8

class`9`MyApp extends StatelessWidget { `0`MyApp extends StatelessWidget { `1; `6

`class`6`void`2

`; `3`MyApp extends StatelessWidget { `6

'package:flutter/material.dart'`7; `6

`'package:flutter/material.dart'`4`void`2

}

}

Run the code and the result will be as follows.

Listview scroll to be top when load more

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

`'package:flutter/material.dart'`04

Now assign this ScrollController to the controller field of ListView.

Dart

`'package:flutter/material.dart'`05

Now come to the onPressed() field of FloatingActionButton.

Here we will use the following code:

Dart

`'package:flutter/material.dart'`06

'package:flutter/material.dart'`08 'package:flutter/material.dart'`09

'package:flutter/material.dart'`4'package:flutter/material.dart'`11

'package:flutter/material.dart'`4'package:flutter/material.dart'`13

}

`'package:flutter/material.dart'`16

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

Dart

`'package:flutter/material.dart'`17

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

`'package:flutter/material.dart'`18

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

`'package:flutter/material.dart'`19

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

Listview scroll to be top when load more

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

`'package:flutter/material.dart'`20

`'package:flutter/material.dart'`22

`'package:flutter/material.dart'`24

`'package:flutter/material.dart'`26

`'package:flutter/material.dart'`27

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

Listview scroll to be top when load more

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

`runApp(MyApp()); `5

`'package:flutter/material.dart'`06

'package:flutter/material.dart'`4'package:flutter/material.dart'08 'package:flutter/material.dart'`09

'package:flutter/material.dart'`7'package:flutter/material.dart'`35

'package:flutter/material.dart'`7'package:flutter/material.dart'`20

; `3'package:flutter/material.dart'`22

; `3'package:flutter/material.dart'`24

; `3'package:flutter/material.dart'`26

`'package:flutter/material.dart'`7`void`2

'package:flutter/material.dart'`4} `

`MyApp extends StatelessWidget { `6

runApp(MyApp()); `9} 0; `0

} `3'package:flutter/material.dart'56; `0

`'package:flutter/material.dart'`59

`'package:flutter/material.dart'`60

Now run the app again.

Listview scroll to be top when load more

Scrolling top of ListView

Complete Source Code:

Dart

import 'package:flutter/material.dart' ;

void main() {

runApp(MyApp());

}

class MyApp extends StatelessWidget {

`'package:flutter/material.dart'`1

`'package:flutter/material.dart'`3

'package:flutter/material.dart'`4'package:flutter/material.dart'5 'package:flutter/material.dart'`6

'package:flutter/material.dart'`7'package:flutter/material.dart'8'package:flutter/material.dart'9; `0

'package:flutter/material.dart'`7; `2

; `3; `4

'package:flutter/material.dart'`7; `6

'package:flutter/material.dart'`7; 8'package:flutter/material.dart'9; `6

`'package:flutter/material.dart'`4`void`2

}

}

class `void`7

`void`9`main() { `0`main() { `1

`main() { `3

`'package:flutter/material.dart'`1

`main() { `7

}

class ``0

`; `13

`'package:flutter/material.dart'`1

`'package:flutter/material.dart'`3

'package:flutter/material.dart'`4'package:flutter/material.dart'`5 ``7

'package:flutter/material.dart'`7 `9

`; `3`runApp(MyApp()); `1

'package:flutter/material.dart'`7; `6

`'package:flutter/material.dart'`7`runApp(MyApp()); `5

; `3'package:flutter/material.dart'`06

class`6'package:flutter/material.dart'08 'package:flutter/material.dart'`09

class`9'package:flutter/material.dart'`11

class`9'package:flutter/material.dart'`20

; `38'package:flutter/material.dart'`22

; `38'package:flutter/material.dart'`24

; `38'package:flutter/material.dart'`26

`class`9`void`2

class`6} `

`; `3`MyApp extends StatelessWidget { `6

; `3`runApp(MyApp()); `9} 0; `0

; `3} 3} 4; `0

; `3} `7

'package:flutter/material.dart'`7; `6

`'package:flutter/material.dart'`7`class`1

; `3; `65

`; `3`class`3

`; `3`class`5

class`6'package:flutter/material.dart'`5 `class`8

class`9`MyApp extends StatelessWidget { `0`MyApp extends StatelessWidget { `1; `6

`class`6`void`2

`; `3`MyApp extends StatelessWidget { `6

'package:flutter/material.dart'`7; `6

`'package:flutter/material.dart'`4`void`2

}

}

Output:

Listview scroll to be top when load more

Scroll to the bottom of the list

Listview scroll to be top when load more

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.

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

How do I make ListView scroll smoothly?

Beginning with Android 3.0 (API level 11), an extra feature is available in AsyncTask so you can enable it to run across multiple processor cores. Instead of calling execute() you can specify executeOnExecutor() and multiple requests can be executed at the same time depending on the number of cores available.

How to load large data in RecyclerView when scrolling on android?

Step by Step Implementation.

Step 1: Create a New Project..

Step 2: Add the below dependency in your build.gradle file and allow internet permission in the manifests file..

Step 3: Working with the activity_main.xml file..

Step 4: Working with Modal Class..

Step 5: Creating a layout file for our item of RecyclerView..

What is the difference between list and scroll view?

In a scroll view, all items exist all the time and need to be generated on the first frame. More geometry to store, more style to be applied, etc.. The list view is harder to use, is less flexible and requires c

code, but has better performance.

Can you scroll a ListView?

ListView is the most commonly used scrolling widget. It displays its children one after another in the scroll direction.