Hi I am using the below code in a CustomScrollView to show images in grid using SliverGrid and SliverChildBuilderDelegate. On Scrolling very fast, the children widget that has already have cached image gets destroyed when gone off screen and rebuilt when they are on screen. How to avoid them destroying and rebuilding them on scroll?
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,
),
)
Note: if I remove the index in the image url, then on fast scroll it loads very quickly since its displaying the same image.
https://source.unsplash.com/${150}/${150 + index}/
I don't know if you found an answer, but these two options worked for me: cacheExtent, shrinkWrap false. You can make the cacheExtent more or less. I'm sure if it's too much it consumes too much memory as list builder manages the memory for you as it builds and destroys.