Reputation: 491
How to cache memory image using Image.memory()
or MemoryImage()
flutter ?
i have i list of data but the image type is bytes. i was using Image.memory()
or MemoryImage()
inside of Gridview.builder
but it's blinking every re render the grid example when pagin(new data is coming), or scrolling.
Thanks for help 🙏
Code:
StreamBuilder<List<Datum>>(
stream: _provider.promotionsStream,
builder: (BuildContext context, AsyncSnapshot<List<Datum>> snapshot) {
return Padding(
padding: EdgeInsets.only(top: 16.0),
child: NotificationListener<ScrollEndNotification>(
onNotification: (scrollEnd) {
if (scrollEnd.metrics.pixels >=
scrollEnd.metrics.maxScrollExtent * .45) {
_provider.nextPage();
}
return true;
},
child: SingleChildScrollView(
child: Column(
children: [
GridView.builder(
shrinkWrap: true,
physics: BouncingScrollPhysics(),
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3,
),
itemCount:
_provider.dataLentgh > 16 ? _provider.dataLentgh : 18,
itemBuilder: (ctx, idx) {
Datum? _data = snapshot.data == null
? null
: _provider.dataLentgh > idx
? snapshot.data![idx]
: null;
return AspectRatio(
aspectRatio: 1.0,
child: Container(
padding: EdgeInsets.all(2.0),
alignment: Alignment.center,
width: 60,
decoration: BoxDecoration(
color: Colors.white,
border:
Border.all(color: Colors.grey, width: 0.5),
),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
width: 60,
height: 60,
decoration: BoxDecoration(
image: _data == null
? null
: _data.imageUrl.isEmpty
? null
: DecorationImage(
fit: BoxFit.contain,
image: Image.memory(
Uint8List.fromList(
_data.imageBytes),
).image,
),
color: Colors.transparent,
),
),
],
),
),
);
},
),
],
),
),
),
);
},
),
the data come from the API with bytes type need to display that in gridview as image. but the issues is blinking eah time the data updated or scrolling
Upvotes: 8
Views: 6337
Reputation: 491
Finally found the solution, i just customize the Image Provider to cache the Image with bytes type. here is the code
https://gist.github.com/darmawan01/9be266df44594ea59f07032e325ffa3b
Maybe there is another way to do this you guys want to share with anyone who read this post. i will appreciate that.
Hope this help 🎉
Upvotes: 9