Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD

flutter flutter logo auto-resizes text

flutter widget that automatically resizes text to fit perfectly within its bounds.

show some ❤️ and star the repo to support the project

auto-resizes text to fit bounds
auto-resizes text to fit bounds

usage

autosizetext behaves exactly like a text. the only difference is that it resizes text to fit within its bounds.

autosizetext(
  "the text to display",
  style: textstyle(fontsize: 20.0),
  maxlines: 2,
)

maxlines

the maxlines parameter works like you are used to with the text widget. if there is no maxlines parameter specified, the autosizetext only fits the text according to the available width and height.

autosizetext(
  "a really long string",
  style: textstyle(fontsize: 30.0),
  maxlines: 2,
)
auto-resizes text to fit bounds

minfontsize & maxfontsize

the autosizetext starts with textstyle.fontsize. it measures the resulting text and rescales it to fit within its bonds. you can however set the allowed range of the resulting font size.

with minfontsize you can specify the smallest possible font size. if the text still doesn’t fit, it will be handled according to overflow. the default minfontsize is 12.0.

maxfontsize sets the largest possible font size. this is useful if the textstyle inherits the font size and you want to constrain it.

autosizetext(
  "a really long string",
  style: textstyle(fontsize: 30.0),
  minfontsize: 18.0,
  maxlines: 4,
  overflow: textoverflow.ellipsis,
)
auto-resizes text to fit bounds

stepgranularity

the autosizetext will try each font size, starting with textstyle.fontsize until the text fits within its bounds.
stepgranularity specifies how much the font size is decreased each step. usually, this value should not be below 1.0 for best performance.

autosizetext(
  "a really long string",
  style: textstyle(fontsize: 40.0),
  minfontsize: 10.0,
  stepgranularity: 10.0,
  maxlines: 4,
  overflow: textoverflow.ellipsis,
)
auto-resizes text to fit bounds

presetfontsizes

if you want to allow only specific font sizes, you can set them with presetfontsizes.
if presetfontsizes is set, minfontsize, maxfontsize and stepgranularity will be ignored.

autosizetext(
  "a really long string",
  presetfontsizes: [40.0, 20.0, 14.0],
  maxlines: 4,
)
auto-resizes text to fit bounds

rich text

you can also use rich text (like different text styles or links) with autosizetext. just use the autosizetext.rich() constructor
(which works exactly like the text.rich() constructor).

the only thing you have to be aware of is how the font size calculation works: the fontsize in the style
parameter of autosizetext (or the inherited fontsize if none is set) is used as reference.

for example:

autosizetext.rich(
  textspan(text: "a really long string"),
  style: textstyle(fontsize: 20.0),
  minfontsize: 5.0,
)

the text will be at least 1/4 of its original size (5 / 20 = 1/4).
but it does not mean that all textspans have at least font size 5.0.

auto-resizes text to fit bounds

performance

autosizetext is really fast. nevertheless you should not use an unreasonable high fontsize in your textstyle. e.g. don’t set the fontsize to 1000.0 if you know, that the text will never be larger than 30.0.

if your font size has a large range, consider increasing stepgranularity.

troubleshooting

if autosizetext overflows or does not resize the text, you should check if it has constraint width / height.

wrong code:

class mypage extends statelesswidget {
  @override
  widget build(buildcontext context) {
    return row(
      children: <widget>[
        autosizetext(
          "here is a very long text, which should be resized",
          maxlines: 1,
        ),
      ],
    );
  }
}

because row does not constrain the width of its children, the text will overflow.

correct code:

class mypage extends statelesswidget {
  @override
  widget build(buildcontext context) {
    return row(
      children: <widget>[
        expanded( // constrains autosizetext to the width of the row
          child: autosizetext(
            "here is a very long text, which should be resized",
            maxlines: 1,
          )
        ),
      ],
    );
  }
}

download the full project for this post from the following button

this source is fully free for all time

download as zip


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD

Comments are closed.