Flutter:Scrolling 屏幕打开时自动底部

Flutter:Scrolling To Bottom Automatically when the screen opens

我正在使用 onpressed() 向下滚动到列表视图的底部, 但我想 不按 按钮

每次打开屏幕都必须自动滚动。 我试着把它放在 initState() 里面 它 不工作 但如果我按下按钮它会工作

如何让它自动滚动?

工作代码:

 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,);
}

非工作代码: //这段代码打印成功,但没有真正调用函数

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

按下浮动按钮之前 按下浮动按钮后

构建 ListView 或添加项目时(不确定您是怎么做的),使用 SchedulerBinding.instance.addPostFrameCallback 更改下一帧的滚动位置。

SchedulerBinding.instance.addPostFrameCallback((_) {
  controller1.animateTo(
    controller1.position.maxScrollExtent,
    duration: const Duration(milliseconds: 10),
    curve: Curves.easeOut,);
  });
}