I am generating individual letters of a word into their own containers and I need to get the width of each container and add them to a list. How do I go about achieving this?
For example, if the word is "character"
then flutter will render: [c][h][a][r][a][c][t][e][r]
Initially, I was using GlobalKeys
to retrieve the width however, since I split the code into multiple files, I couldn't figure out how to access the GlobalKeys
from another file and I can't find any information on google how to do that. Also, if the word has 20 letters then I will need to declare 20 GlobalKeys
and a map
to store those 20 keys.
I figure it would be better if there was a way to retrieve each container's width and store them in a list as they are being generated.
String displayWord = "character";
final [key0 to key19] = GlobalKey(); //PREVIOUS APPROACH
Map keys = { ** map of 20 keys ** }; //PREVIOUS APPROACH
.
.
.
return Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: List.generate(displayWord.length, (index) {
return Container(
key: keys[index], //PREVIOUS APPROACH
child: Text(displayWord[index]),
);
}));