需要在页面上使用SliverList通过API实现内容和组件的延迟加载。我假设可以通过访问initState接口在子对象中加载内容,但在这种情况下,由于SliverChildListDelegate会在滚动时移除组件,所以每次都会发生调用initState和访问接口。因此,执行每个API调用的唯一正确方法是在父组件中加载所有必要的信息,并将其传递给子组件?然而,只有当页面的最后一个元素加载时,页面才会被绘制,并且希望加载组件及其信息,而不会减慢已经加载的内容的呈现速度。
无样式父级
SliverList(
delegate: SliverChildListDelegate([
HomeSlider(), // every time calling initState when come and out of viewport
Container(
height: 3000, // Space for test
color: Colors.black,
),
]),
),
Statefull子级
@override
void initState() {
fetchSlides();
super.initState();
}
Future<void> fetchSlides() async {
final response = await Request().get('/sliders');
setState(() {
slides = response.data['sliders'];
});
}
发布于 2020-10-08 16:42:39
通过使用AutomaticKeepAliveClientMixin创建子窗口小部件,它们不会每次都在狭长列表中重新构建。
在您的子窗口小部件中尝试以下内容:
class _SomeChildState extends State<SomeChild> with AutomaticKeepAliveClientMixin {
@override
bool get wantKeepAlive => true; // You can add custom logic based on whether you want to remove the child or not.
@override
Widget build(BuildContext context) {
super.build(context); // You need to call super.build(context)
return ... // Your widget
}
}
https://stackoverflow.com/questions/64264720
复制