I'm displaying a list of Animated Containers which originally has a certain maxLines of text. When the user clicks on the container, I want the container to expand to perfectly fit the entire text
Here's my simplified code:
AnimatedContainer(
height: containerHeight,
curve: Curves.easeOut,
duration: Duration(milliseconds: 400),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(searchBooksList[index].title),
Text('By ${searchBooksList[index].author}'),
Expanded(
child: Text(
'\n${searchBooksList[index].description}',
overflow: TextOverflow.ellipsis,
maxLines: searchBooksList[index].expanded ? 50 : 7,
)
),
],
),
)
When the user clicks on the container:
onTap: () {
setState(() {
searchBooksList[index].expanded = !searchBooksList[index].expanded;
});
},
Currently, I'm explicitly mentioning the height of the container based on the length of the text
double len;
if(searchBooksList[index].description.length <= 500)
len=0.3;
else if (searchBooksList[index].description.length > 500 && searchBooksList[index].description.length<=750)
len = 0.6;
else if(searchBooksList[index].description.length > 750 && searchBooksList[index].description.length<=1000)
len=0.66;
else if(searchBooksList[index].description.length > 1000 && searchBooksList[index].description.length<=1500)
len=1.0;
else if(searchBooksList[index].description.length>=1500)
len=1.2;
double containerHeight = searchBooksList[index].expanded ? 1000 * len
: 1000 * 0.25;
Here's what's happening now:
But this leaves empty lines at the end for some text. Is there a better way to fit the text without resizing the text?