嗨,我在CustomScrollView中使用以下代码使用SliverGrid和SliverChildBuilderDelegate在网格中显示图像。在快速滚动时,已经缓存图像的子小部件在离开屏幕时会被破坏,并在它们在屏幕上时重建。如何避免他们在卷轴上摧毁和重建它们?
SliverGrid(
gridDelegate: SliverGridDelegateWithMaxCrossAxisExtent(
maxCrossAxisExtent: 150,
childAspectRatio: 1 / 1,
crossAxisSpacing: 4.0,
mainAxisSpacing: 4.0,
),
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) {
return ClipRRect(
borderRadius: BorderRadius.circular(5.0),
child: Container(
alignment: Alignment.bottomRight,
padding: EdgeInsets.all(10.0),
decoration: BoxDecoration(
image: DecorationImage(
fit: BoxFit.fill,
image: CachedNetworkImageProvider(
"https://source.unsplash.com/${150}/${150 + index}/",
),
),
),
child: index == (index / 2) + 1
? Icon(
Icons.burst_mode,
color: AppTheme.fullWhite,
)
: index == (index / 6)
? Icon(
Icons.play_circle_filled,
color: AppTheme.fullWhite,
)
: Container(),
));
},
childCount: childCount,
),
)
注意:如果我删除图像 url 中的索引,那么在快速滚动时它会很快加载,因为它显示的是相同的图像。
https://source.unsplash.com/ ${150}/${150 + 索引}/