简体   繁体   English

如何在 flutter 中的滚动 controller 中途调用 function

[英]How to call a function half way through the scroll controller in flutter

I have a GridView that loads lazily at the end of the scroll controller, however I want to load the data when I reach halfway through the scroll controller.我有一个 GridView,它在滚动 controller 的末尾延迟加载,但是当我到达滚动 controller 的中途时,我想加载数据。 If I try to do an if statement that checks if the scroll has reached 50% of the controller it will never become equal, however, if I do a greater than equal to 50% it calls the function multiple times ending in duplicates of data.如果我尝试执行 if 语句来检查滚动是否已达到 controller 的 50%,它将永远不会相等,但是,如果我执行大于等于 50% 的操作,它会多次调用 function 以结束数据重复。 How can I avoid this duplication or is there a better way to do this.我怎样才能避免这种重复,或者有更好的方法来做到这一点。

A simple way to do it is just use your current code and an additional boolean 'wasCalled' which you set to true in the function.一个简单的方法是使用您当前的代码和一个额外的 boolean 'wasCalled',您在 function 中将其设置为 true。 But: I'm confident there's better solutions.但是:我相信会有更好的解决方案。

I haven't really worked with GridViews, but as far as I understand it builds its children lazily.我并没有真正使用过 GridViews,但据我所知,它会懒惰地构建它的孩子。 Have you tried inserting a custom widget in the middle of the children's list?您是否尝试在子列表中间插入自定义小部件?

Eg in your build function you could:例如,在您的构建 function 中,您可以:

@override
Widget build(BuildContext _) {
  callCustomFunction();
  return Container();
}

You can use the "extentAfter" property to know how much more space is left till the end..您可以使用“extentAfter”属性来了解到最后还有多少空间..

I guess this is something you are trying to do..我想这是你想要做的事情..

https://stackoverflow.com/a/49509349/13460232 https://stackoverflow.com/a/49509349/13460232

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM