【发布时间】:2018-12-15 10:04:53
【问题描述】:
我的屏幕上有一个 listView。我已将控制器连接到它。我可以调用我的端点,接收响应,解析它并插入行。 ListView 应该自动滚动。确实如此,但不是以完美的方式。我总是落后。这是我的代码:
@override
Widget build(BuildContext context) {
// Scroll to the most recent item
if (equationList.length > 0) {
_toEnd();
}
return new Scaffold(
appBar: new AppBar(
title: new Text(widget.title),
),
body: EquList(equationList, _scrollController),
floatingActionButton: new FloatingActionButton(
onPressed: onFabClick,
tooltip: 'Fetch Post',
child: new Icon(isLoading ? Icons.pause : Icons.play_arrow),
),
);
}
void _toEnd() {
_scrollController.animateTo(
_scrollController.position.maxScrollExtent,
duration: const Duration(milliseconds: 250),
curve: Curves.ease,
);
}
问题是,我在最后一项插入列表之前调用了_toEnd() 函数。所以,我正在寻找一个回调(如果有的话)告诉我build() 已经完成。然后我调用我的_toEnd() 函数。
在这种情况下,最佳做法是什么?
【问题讨论】:
-
请注意,我们更喜欢这里的技术写作风格。我们轻轻地劝阻问候,希望你能帮助,谢谢,提前感谢,感谢信,问候,亲切的问候,签名,请你能帮助,聊天材料和缩写 txtspk,恳求,你多久了被卡住、投票建议、元评论等。只需解释您的问题,并展示您尝试过的内容、预期的内容以及实际发生的情况。
-
更好的方法是在
ListView中设置reverse: true或滚动视图(如果您使用一个并以相反的顺序传递内容)。 -
@GünterZöchbauer,我正在这样做。但问题是,我仍然需要一个回调来告诉我构建完成。
-
你需要它做什么?
build()是同步的,所以如果你在build()中执行异步操作,它将在build()完成后执行。 -
谢谢@GünterZöchbauer,您可以举个例子回答您的建议吗?我是 Flutter 的新手,恐怕我没有让你好起来。谢谢。
标签: flutter