2

I want to create an app, that shows sometimes shorter, somertimes longer text in a Container. This container should not change in size, after the widget is build to fit with the size of the screen. The text itself comes from a database and I don't know, how long it can be.

When I print the text in the container, sometimes it fits, sometimes there is many space above and below, when the text is short, or sometime the text overflows and is not visible.

I want, that the text size is automatically changed to fit into the container. Container with overflow

The flutter package Auto-Size-Text is not working for me, because I only could resize the text to fit the width or set the maximum line, which I don't know either.

This is my code simplified:

AutoSizeText autotext = AutoSizeText(text, style: style, textAlign: TextAlign.center, presetFontSizes: [25.0,22.0,20.0,18.0], semanticsLabel: text);


Container(
  child: autotext
);
6
  • Are you sure about that? I also used the auto_size_text package and it works for me. See the third screenshot on pub.dartlang.org/packages/auto_size_text Commented Dec 21, 2018 at 9:58
  • @Marcel The text is resizing in the third screenshot because of the maximum 4 lines. But not to fit the container. Commented Dec 21, 2018 at 10:00
  • @Marcel I edited my post with a picture and Code Commented Dec 21, 2018 at 10:09
  • Does the Container have fixed dimensions? If not, there could be a problem if it's provided with infinite bounds, like if it's in a Column, ListView or Stack. In that case, the Container would try to be as small as possible by asking its child, the AutoSizeText, which dimensions it would prefer, resulting in the AutoSizeText just picking the default size. Commented Dec 23, 2018 at 0:23
  • @Marcel the Conatiner itself is in a some Columns, Rows and Expanded Widgets integrated, because I want it to be as big as it can get Commented Dec 23, 2018 at 12:55

1 Answer 1

15

TL;DR: Your AutoSizeText widget is probably provided with unbounded constraints. This can be the case if it's wrapped in a Column, Row, ListView or something similar. To counter this issue, wrap it in an Expanded widget.


In order to answer that question, we need to understand how Flutter performs the layout. Basically, the top widget's height and width (called its "dimensions") are requested by the framework, which provides a min and max width and height (called "constraints"). In order to calculate its dimensions, the widgets ask their children what dimensions they want to have based on some constraints. So, every widget gets told constraints and returns concrete dimensions within these constraints.

Widgets like ListView or Column don't want to influence their children's dimensions too much - they should simply be as small as possible. In order to communicate that, they are given a maximum width/height of double.infinity, telling them that they should default to their "natural"/default dimensions. A constraint of this type is called "unbounded".

Here's a Google Tech Talk video about Flutter's rendering pipeline, where all of this is described in greater detail.

With this process in mind, let's have a look at how your widgets perform during layout:

In a comment to your post, you said there's a Column widget, so I assume there's a Column containing a Container, which in turn contains the AutoSizeText widget. If instead of a Column, there's a Row or ListView or something similar, the process is basically the same.

The Column wants its children to be as small as possible, so it passes unbounded constraints to all of its children.

Being one of those children, the Container receives those unbounded constraints. Because the Container itself is just a wrapper class, it simply passes these constraints on to its child, the AutoSizeText.

The AutoSizeText receives unbounded constraints. Looking at its source code, we can see that it starts with a default text size and checks if it fits (calling _checkTextFits). If it doesn't fit, the text size is decreased and this process is repeated until the text is small enough to fit into the given constraints. Because in your case, the constraints are unbound, the text fits on the first try - that means the widget simply uses the default size. Having determined the text size, it can calculate the text's dimensions and returns them up the tree.

The Container receives those dimensions. Because it's just a wrapper class, it simply returns them to its parent in turn.

Finally, the Column receives the dimensions and sizes itself around that (probably adding the dimensions of other children).

Solution

You don't want to pass unbounded constraints to an AutoSizeText widget, because that defeats the purpose of the widget in the first place - fitting text into bounded constraints. You can confirm this is the problem by hardcoding the Container's width and height, that should make the AutoSizeText widget adjust the text size properly.

If the Container is placed inside a Column or something similar, you can always bound the constraints by wrapping it in an Expanded.

Why the solution works

The Column checks which of its children are Expanded widgets. It first lays out all the other widgets. Only then, it divides the remaining space among the Expanded widgets according to their flex factors (that's a whole other topic for itself).

That means the Expandeds' children will receive bounded constraints. If you wrap your Container in an Expanded and it's the only expanded in the Column, it should take all the remaining space, and fit the text perfectly within itself.

Sign up to request clarification or add additional context in comments.

Thanks a lot, @Marcel. I tried it and it now works. You were right, that just the constraint of width was infinite. I just wrapped the AutoSizeText in an Expanded and than in the Column.

Your Answer

Draft saved
Draft discarded

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.