Member-only story
How to dynamically resize text in Flutter?
1 min readMay 5, 2020
There are two approaches to do this.
- Using FittedBox
- Using TextPainter (calculate width with text Painter and update font )
Using FitterdBox, Wrap the Text Widget into FittedBox Widget with fitWidth parameter.
Container(
width: 250,
height: 20,
child: FittedBox(fit:BoxFit.fitWidth,
child: Text("Lorem ipsum dolor sit amet, consectetur adipiscing elit."),
),
),
The drawback of the above approach is that we do not have control over the minimum and maximum font size.
To avoid above drawback. We can just leverage Text Painter and calculate he appropriate scale factor like following:
Demo and Results:
