AutoSizeText

Travis Version Runtime GitHub license

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

Resources:

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.

minFontSize & maxFontSize

With minFontSize you can specify the smallest possible font size. If the text still not fits, it will be handled according to overflow. The default minFontSize is 12.0.

maxFontSize sets the largest passible font size. This is useful if the TextStyle inherits the font size and you want to constrain it.

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.

presetFontSizes

If you want to allow only specific font sizes, you can set them with presetFontSizes. Only font sizes which match minFontSize and maxFontSize will be used.
If presetFontSizes is set, stepGranularity will be ignored.

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,
          )
        ),
      ],
    );
  }
}

MIT License

Copyright (c) 2018 Simon Leier

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Libraries

auto_size_text