Member-only story

How to dynamically resize text in Flutter?

Prafulla Singh
1 min readMay 5, 2020

--

There are two approaches to do this.

  1. Using FittedBox
  2. 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:

sized, small, large text

--

--

Prafulla Singh
Prafulla Singh

Responses (2)