How to scroll SingleChildScrollView programmatically?
On long Form, it is desirable to automatically scroll to a require input field.
You can create a ScrollController and pass it to the controller parameter of your scrolling widget. Then you can use the animateTo method to animate to an offset.
This widget is useful when you have a single box that will normally be entirely visible, for example a clock face in a time picker, but you need to make sure it can be scrolled if the container gets too small in one axis (the scroll direction).
A scroll controller creates a ScrollPosition to manage the state specific to an individual Scrollable widget. To use a custom ScrollPosition, subclass ScrollController and override createScrollPosition. A ScrollController is a Listenable.
Use ScrollController
to scroll inside Column
or ListView
Examples:
Colum With SingleChildScrollView
class ScrollContainerPage extends StatelessWidget {
ScrollController _scrollController = ScrollController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('ScrollView Example'),
),
body: SingleChildScrollView(
controller: _scrollController,
child: ListView(
children: [
RaisedButton(
onPressed: () {
/// Scroll maximum end, if you want you can give hardcoded values also in place of _scrollController.position.maxScrollExtent
_scrollController.animateTo(
_scrollController.position.maxScrollExtent,
duration: Duration(milliseconds: 500),
curve: Curves.ease);
},
child: Text('Scroll To Bottom'),
),
SizedBox(height: 20,),
Container(height: 200, color: Colors.red,),
SizedBox(height: 20,),
Container(height: 200, color: Colors.blue,),
SizedBox(height: 20,),
Container(height: 200, color: Colors.green,),
SizedBox(height: 20,),
Container(height: 200, color: Colors.yellow,),
SizedBox(height: 20,),
],
),
),
);
}
}
ListView
class ScrollContainerPage extends StatelessWidget {
ScrollController _scrollController = ScrollController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('ScrollView Example'),
),
body: ListView(
controller: _scrollController,
children: [
RaisedButton(
onPressed: () {
_scrollController.animateTo(
_scrollController.position.maxScrollExtent,
duration: Duration(milliseconds: 500),
curve: Curves.ease);
},
child: Text('Scroll To Bottom'),
),
SizedBox(height: 20,),
Container(height: 200, color: Colors.red,),
SizedBox(height: 20,),
Container(height: 200, color: Colors.blue,),
SizedBox(height: 20,),
Container(height: 200, color: Colors.green,),
SizedBox(height: 20,),
Container(height: 200, color: Colors.yellow,),
SizedBox(height: 20,),
],
),
);
}
}
Note: Only thing need to make sure that ScrollController
assigned to respective widget
There are multiple solutions :
ScrollController
: Assign it to controller
field of your SingleChildScrollView
. And use myScrollController.animateTo
or myScrollController.jumpTo
Scrollable
. Possibly Scrollable.ensureVisible
. But be aware that the context
parameter is the context of the widget that needs to be visible. You can get that context by using GlobalKey
.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With