【发布时间】:2020-11-30 16:39:49
【问题描述】:
在我的应用程序中,我有一个 Scaffold 网格控制器,它显示生成的 4 列和最多 50 行的图块(卡片)。我还有一个按钮,它会生成一个新图块并将其附加到网格中。
基本上我的应用程序是这样工作的:它从一个空网格开始,如果你按下按钮,它将生成并添加一个新的图块,直到最多 50 块。 现在,我的网格尺寸是 4x5 ,这意味着我最多可以显示 20 个没有滚动的图块,并且已经从 21 开始我应该向下滚动以查看新图块。
我想实现一个逻辑,以便在图块数量大于 20 时进行自动滚动。
我尝试使用滚动控制器,但它以一种奇怪的方式工作:如果我添加 21 号磁贴(基本上是第 6 行),则不会调用滚动控制器,但对于 22,23 磁贴, 24 - 会的。
我的代码如下所示:
ScrollController _scrollController;
...
@override
void initState() {
super.initState();
// Scoll Controler
_scrollController = new ScrollController(initialScrollOffset: 0.0, keepScrollOffset: true );
}
...
void _toEnd() {
_scrollController.animateTo(
_scrollController.position.maxScrollExtent,
duration: const Duration(milliseconds: 500),
curve: Curves.ease,
);
}
...
@override
Widget build(BuildContext context) {
...
floatingActionButton: FloatingActionButton (
onPressed: () {
// append new tile to grid
setState(() { actual_cards = actual_cards + 1; });
_toEnd();
};
}
我检查了一下,似乎我的问题出在 _scrollController.position.maxScrollExtent,因为当我添加 21 块时它的值仍然是 0.0 ,而在 22 上它的值是 61.71。
按下按钮时如何触发此参数更新?
谢谢。
【问题讨论】: