In some Text Widgets we display text which is getting truncated on some devices and we want to replace the text in this cases instead of truncating it.
I.E. we display a name like "Kamala Devi Harris"
When the name is too long for displaying we want to call a method and transform it to "Kamala D. Harris" and if that is also to long then "K. D. Harris" ... "K. Harris" or even "K. H."
How can we recognize that the current text is getting truncated?
I don't know if it's the most suitable way to achieve what you're trying to do but here's how we can do it :
Make a custom
Widgetwhich will take a list ofStringfrom greater value to lower so that it can check which value fit's in the available space.Our
OverflowAwareTexttakes a parameteralternativeTextswhich isList<String>. You have to provide a list of Strings with larger to smaller values.We can now use the
OverflowAwareText:We've wrapped the
OverflowAwareTextin aContainer, so that we can check if the text is being changed or not when there is less space.RESULT :
Containerwidth = 330inContainerwidth = 180inContainerWith
width = 100inContainer