text_wrap_auto_size 1.0.3+1 copy "text_wrap_auto_size: ^1.0.3+1" to clipboard
text_wrap_auto_size: ^1.0.3+1 copied to clipboard

Wraps text and auto sizes it dynamically with respect to the given space.

text_wrap_auto_size #

Wraps text and auto sizes it with respect to the given space.

The text is never cut off. Insteads, it will apply the largest font size possible to fill the maximum of the available space.

Changing the text or changing the boundaries triggers a new layout cycle, thus adapting the text size dynamically.

Requirements #

The widget requires a given width and height. It will throw an Exception, if it reveives an unbound (infinite) width or height. More about Flutter contraints here.

Usage #

Generally, several Text attributes are respected.

final style = TextStyle(
    fontWeight: FontWeight.bold, 
    color: Colors.red);

final text = Text(
    'text',
    style: style,
    textAlign: TextAlign.center,
    locale: Locale('de'),
    softWrap: true,
    textScaleFactor: 1.0,
    semanticsLabel: 'semanticsLabel',
    strutStyle: StrutStyle(),
    textWidthBasis: TextWidthBasis.parent,
    textDirection: TextDirection.ltr,
);

TextWrapAutoSize(text);

Use As Method #

The static method solution allows for accessing the resulting font size directly, which resides in the TextStyle. Using the result, one can construct a font size adjusted widget manually.

Solution sol = TextWrapAutoSize.solution(Size size, Text text);

// String text for easy reference.

print(sol.text); // String 

// TextStyle with adjusted font size.  
// All other style properties of the Text-parameter 
// are merged into it.

print(sol.style); // TextStyle 

// Resulting Size of the wrapped and auto sized text.
// Smaller or equal to the Size-parameter of the outer box.

print(sol.sizeInner); // Size

// The Size-parameter of the outer box for easy reference.

print(sol.sizeOuter); // Size

// How to output the font adjusted text yourself.

SizedBox(
    width: sol.sizeOuter.width,
    height: sol.sizeOuter.height,
    child: Text(sol.text, style: sol.style),
);

Use As Widget #

Define width and height.

SizedBox(
    width:250,
    height:250,
    child: TextWrapAutoSize(Text('text'))
);

In some cases, width and height can be determined by wrapping the widget in Expanded.

Expanded(
    child: TextWrapAutoSize(Text('text'))
);

Use it as the Scaffold's body:

@override
Widget build(BuildContext context) {
    return SafeArea(
        child: Scaffold(
        body: TextWrapAutoSize(Text('text')),
        ),
    );
}

Alternatives #

The package auto_size_text does something similar and offers more options (at the moment).

Issues and Background #

The package follows the most simple apporach I could think of. There might be a more efficient approach.

Internally, the widget performs a binary-search for the optimal font size and renders the text multiple times in its own render-tree.

In my typical use cases, the widgets needs nine steps to find the optimal font size.

The code measuring the widget size can be found in the Flutter docs.

Open an issue on Github.

5
likes
0
pub points
70%
popularity

Publisher

verified publishernovelplotter.com

Wraps text and auto sizes it dynamically with respect to the given space.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter

More

Packages that depend on text_wrap_auto_size