颤振:屏幕打开时自动滚动到底部 [英] Flutter:Scrolling To Bottom Automatically when the screen opens

查看:134
本文介绍了颤振:屏幕打开时自动滚动到底部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用onpressed()向下滚动到列表视图的底部, 但是我想无需按下该按钮来实现该目标,

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

它必须在每次打开屏幕时自动滚动. 我试图将其放在initState()中 它的不起作用,但是如果我按下按钮,它就会变

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

如何使其自动滚动?

工作代码:

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

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

推荐答案

在构建ListView或添加项目时(不确定您的操作方式),请使用SchedulerBinding.instance.addPostFrameCallback更改下一帧的滚动位置.

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

这篇关于颤振:屏幕打开时自动滚动到底部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆