I am trying to do create a view of ExpansionTile
with ListView.builder
as children. It works well but just if my ListView.builder
become larger, it will need to take some time to render it which slightly affect the user expereince. Is there anyway to help to improve the performance for this?
Below is my sample code:
ExpansionTile(
title: Text(title),
controlAffinity: ListTileControlAffinity.trailing,
initiallyExpanded: true,
maintainState: true,
children: [
ListView.builder(
itemCount: data.length,
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
itemBuilder: (context, index) => widgetList[index],
)
])
2
Answers
Here’s an improved version of your code with optimizations for better performance:
Key Changes:
Remember, these are general guidelines, and the actual performance improvements may vary depending on your specific use case and data.
1. shrinkWrap
I would suggest to get rid of shirnkWrap (if possible), as it calculates the size of all elements in the list.
2. ItemBuilder
Also, I recommend to be aware of the operations inside the itemBuilder, because it can slow down rendering the items.