Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter:Scrolling To Bottom Automatically when the screen opens

I'm using onpressed() to scroll down to bottom of the List view, but i want to achieve that without Pressing the botton,

It must autoscroll for every screen opening. I tried to put it inside initState() its not working but if i press the button it wokrks

How to make it autoScroll?

Working code:

 floatingActionButton: new FloatingActionButton(child: 
 Icon(Icons.arrow_downward),onPressed:_hola,)



_hola(){
  print("inti state started successfully");
controller1.animateTo(
controller1.position.maxScrollExtent,
  duration: const Duration(milliseconds: 10),
   curve: Curves.easeOut,);
}

Non Working Code: //This code prints successfully,but not really calling the function

class HomeState extends State<MyNewMessages> {
  @override
  void initState() 
{
 super.initState();
  print("hola is scrolling");
  _hola;
}
);
}

Before Pressing Floating Button Before Pressing Floating Button After Pressing Floating Button After Pressing Floating Button

like image 208
Rajesh Jr. Avatar asked Jun 27 '18 17:06

Rajesh Jr.


People also ask

How do I scroll automatically in flutter?

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.

How do I stop my scroll from fluttering?

How do I stop list view scrolling in flutter? Flutter ListView – Never Scrollable You can make the ListView widget never scrollable by setting physics property to NeverScrollableScrollPhysics().


1 Answers

When building your ListView or adding an item (not sure how you're doing it), use SchedulerBinding.instance.addPostFrameCallback to change the scroll position on the next frame.

SchedulerBinding.instance.addPostFrameCallback((_) {
  controller1.animateTo(
    controller1.position.maxScrollExtent,
    duration: const Duration(milliseconds: 10),
    curve: Curves.easeOut,);
  });
}
like image 74
Jacob Phillips Avatar answered Nov 10 '22 03:11

Jacob Phillips