颤振:屏幕打开时自动滚动到底部
我正在使用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,);
});
}