SliverChildListDelegate 和 children initState
SliverChildListDelegate and children initState
需要在页面上使用SliverListAPI实现内容和组件的延迟加载。我假设可以通过访问 API initState 来通过 children 中的 API 加载内容,但是这样,由于 SliverChildListDelegate 在滚动时删除了一个组件,调用 initState 和API 每次都访问。
因此,noat 执行每个 API 调用的唯一正确方法是在 parent 组件中加载所有必要的信息并将其传递给 children?但是,这种方式只会在加载其最后一个元素时绘制页面,并且希望在不减慢已加载内容的呈现速度的情况下加载组件及其信息。
无钢圈Parent
SliverList(
delegate: SliverChildListDelegate([
HomeSlider(), // every time calling initState when come and out of viewport
Container(
height: 3000, // Space for test
color: Colors.black,
),
]),
),
全状态 child
@override
void initState() {
fetchSlides();
super.initState();
}
Future<void> fetchSlides() async {
final response = await Request().get('/sliders');
setState(() {
slides = response.data['sliders'];
});
}
通过使用 AutomaticKeepAliveClientMixin 制作子部件,它们不会每次都在 sliver 列表中重建。
在您的子窗口小部件中试试这个:
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
}
}
需要在页面上使用SliverListAPI实现内容和组件的延迟加载。我假设可以通过访问 API initState 来通过 children 中的 API 加载内容,但是这样,由于 SliverChildListDelegate 在滚动时删除了一个组件,调用 initState 和API 每次都访问。 因此,noat 执行每个 API 调用的唯一正确方法是在 parent 组件中加载所有必要的信息并将其传递给 children?但是,这种方式只会在加载其最后一个元素时绘制页面,并且希望在不减慢已加载内容的呈现速度的情况下加载组件及其信息。
无钢圈Parent
SliverList(
delegate: SliverChildListDelegate([
HomeSlider(), // every time calling initState when come and out of viewport
Container(
height: 3000, // Space for test
color: Colors.black,
),
]),
),
全状态 child
@override
void initState() {
fetchSlides();
super.initState();
}
Future<void> fetchSlides() async {
final response = await Request().get('/sliders');
setState(() {
slides = response.data['sliders'];
});
}
通过使用 AutomaticKeepAliveClientMixin 制作子部件,它们不会每次都在 sliver 列表中重建。
在您的子窗口小部件中试试这个:
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
}
}