Flutter has a FittedBox that reduces the font size of text. And how can you make the text shrink, for example, to 8px, and if it still doesn’t fit, then it just wraps to a new line?
CodePudding user response:
You can use this package - auto_size_text
it will automatically wrap if not adjusted in the container. It will automatically adjust font sizes as you want.
Usage:
AutoSizeText(
'A really long String',
style: TextStyle(fontSize: 30),
minFontSize: 18,
maxLines: 4,
overflow: TextOverflow.ellipsis,
)
You can also give preset font size by this way:
AutoSizeText(
'A really long String',
presetFontSizes: [40, 20, 14],
maxLines: 4,
)
CodePudding user response:
In Flutter, you can use the FittedBox widget to reduce the font size of text. The FittedBox has a fit property that you can set to BoxFit.scaleDown.
To make the text wrap to a new line when it still doesn't fit, you can use the Text widget's softWrap property and set it to true.
FittedBox(
fit: BoxFit.scaleDown,
child: Text(
'Your Text',
style: TextStyle(fontSize: 8),
softWrap: true,
),
),
CodePudding user response:
Use the LayoutBuilder widget along with the Text widget's maxLines and softWrap properties.
Example:
LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
return Text(
'Your text here',
maxLines: (constraints.maxHeight / 8.0).floor(),
softWrap: true,
style: TextStyle(
fontSize: 8.0,
),
);
},
)
The LayoutBuilder allows you to determine the size of text based on available space.
The maxLines property of Text widget is used to set maximum number of lines for text.
The softWrap property is set to true so that the text can wrap to a new line.
The maxLines is set to (constraints.maxHeight / 8.0).floor(), which will calculate the maximum number of lines based on the available height and font size of 8px.