Flutter – Scroll Down to Bottom or Top of List in ListView
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),
),
floatingActionButton: FloatingActionButton(
onPressed: () {},
isExtended: true ,
tooltip: "Scroll to Bottom" ,
child: Icon(Icons.arrow_downward),
),
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
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
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
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),
),
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),
),
body: ListView.builder(
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.
Last Updated :
21 Aug, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...