'Scroll To Top on Button Click in Flutter
I'm new to Flutter, I'm facing this issue, There is a button below in ScrollController, And when I click it. It should scroll To top.
ScrollController is embedded in SingleChildScrollView
Widget build(BuildContext context) {
return Container(
child: SingleChildScrollView(
controller: scrollController)
);
}
I tried using
setState(() {
scrollController.animateTo(0.0,
duration: const Duration(milliseconds: 300),
curve: Curves.ease);
}
Below is the Whole Code
Future<void> nextPressed() async {
final bool connectivity = await ConnectivityManager().isInternetAvailable();
if(connectivity) {
final FormState form = _formKey.currentState;
if (form.validate()) {
form.save();
updateDOBError(_dobController.text);
setState(() {
if(dobErrorString.isEmpty){
_errorAlertVisibilityDOB = false;
}else{
_errorAlertVisibilityDOB = true;
}
_errorAlertVisibility = true;
});
} else {
updateDOBError(_dobController.text);
setState(() {
if(dobErrorString.isEmpty){
_errorAlertVisibilityDOB = false;
}else{
_errorAlertVisibilityDOB = true;
}
errorType = RegistrationErrorType.validationError;
_errorAlertVisibility = true;
});
--> scrollController.animateTo(0.0,
duration: const Duration(milliseconds: 300), curve: Curves.ease);
}
}else{
const SnackBar snackBar =
SnackBar(content: Text(AppStrings.no_internet));
Scaffold.of(context).showSnackBar(snackBar);
}}
The --> is the place, I'm facing issue
Solution 1:[1]
Use following on button clicked
setState((){
_scrollController.animateTo(
_scrollController.position.minScrollExtent,
curve: Curves.easeOut,
duration: const Duration(milliseconds: 500),
);})
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | MORE AKASH |
