Text resize while maintaining multiline - flutter

Code:
Container(
constraints:
BoxConstraints(maxHeight: mediaQuery.height*0.1, minHeight: mediaQuery.height*0.05),
child: Text(
output['title'],
textAlign: TextAlign.start,
style: const TextStyle(
fontWeight: FontWeight.bold, fontSize: 16),
),
),
As the screen won't be scrollable, I determine the max height for the container. With the current text, it takes 3 lines with the font size 16. If its bigger, won't appear. How to resize the fontSize if the text is bigger while mantaining multine text? Have already tried FittedBox then fit but it takes only one line

You can check auto_size_text.
Demo
AutoSizeText(
'The text to display',
style: TextStyle(fontSize: 20),
maxLines: 3,
)

Related

Text is shifted vertically in Flutter

I have the following code:
AutoSizeText.rich(
TextSpan(children: [
const TextSpan(
text: "\$",
style: TextStyle(
color: Colors.white,
fontSize: 45,
height: 1.0,
letterSpacing: .0,
fontWeight: FontWeight.w600,
),
),
TextSpan(
text: widget.budget.truncate().toString(),
style: const TextStyle(
color: Colors.white,
fontSize: 90,
),
),
TextSpan(
text: ".${widget.budget.toStringAsFixed(2).split(".")[1]}",
style: const TextStyle(
color: Colors.white,
fontSize: 45,
),
),
]),
style: GoogleFonts.manrope(
fontSize: 45,
letterSpacing: -4.5,
height: 1.0,
fontWeight: FontWeight.w600,
textStyle: const TextStyle(
leadingDistribution: TextLeadingDistribution.even,
)),
textAlign: TextAlign.center,
maxLines: 1,
);
The code results in this (I also tried with Text.rich instead of AutoSizeText but the result is the same):
In the image, I have wrapped the text in a red container to check its bounding box.
I am implementing this element following a design I made in Figma:
Similatly, in this image the red frame has an autolayout applied with all padding set to 0, in a way that simulates wrapping the text in a Container.
The text looks the same in both the design and the Flutter implementation, but the Flutter one is shifted upwards, which on big text sizes is really noticeable. I figured it might have something to do with the vertical alignment of the text but I could not find any way to fix it.
Is this an expected behaviour or am I doing something wrong?

Flutter Text overflow

How to control TextOverFlow if don't know the const width of the box.
I know about
Text(
'some Text',
overflow: TextOverflow.ellipsis,
)
but is there any way to wrap widgets like expanded or flexible?
FittedBox(
fit: setToWidth ? BoxFit.contain : BoxFit.none,
child: Text(
displayText,
maxLines: 2,
textAlign: textAlign,
style: setToWidth
? TextStyle(
fontWeight: fontWeight,
color: textColor,
fontFamily: AppFonts.circularStd,
)
: TextStyle(
fontWeight: fontWeight,
color: textColor,
fontFamily: AppFonts.circularStd,
fontSize: textSize),
),
);
Try with this
You can try AutoSizeText instead.
https://pub.dev/packages/auto_size_text
Note: it'll increase your overall app size as it requires external dependencies. But it'll give you some extra features such as maxLines and minLines. I will suggest read more about it.

flutter listTile with a long text for the Title

I have a listTile title with a long text .
I whoud like the title to show as must text as possible , in one single line ...
If I use softWrap: true, overflow: TextOverflow.ellipsis, the text is cut after 4 caracter´s
like : exampl....
if I don´t use softWrap: true, overflow: TextOverflow.ellipsis, the text is shown in 3 or 4 lines
I tryied using FittedBox , but the text get´s really small .. unreadable ....
Any ideias how to get this ?
title: Text(
inc.descricao,
style: TextStyle(
color: Colors.white,
fontFamily: "Roboto",
fontSize: 16,
),
softWrap: true,
overflow: TextOverflow.ellipsis,
),
thank You Roque
Use layout Flexible/Container with text overflow:
Flexible(
child: Container(
child: Text(
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
overflow: TextOverflow.ellipsis,
style: TextStyle(
fontSize: 13.0,),
),
),
),
Actually, overflow: TextOverflow.ellipsis should work.
My guess is Text widget container making the Text widget shorter
Try wrapping Text widget in Container with maximum width
Container(
width: double.infinity,
child: Text(
'reaaaaaly long text, like reaaaaaaaaaaaaaly long',
overflow: TextOverflow.ellipsis,
),
You can use text property Overflow.elipsis.
ListTile(
shape: RoundedRectangleBorder(
minLeadingWidth: 0,
title: Text(
"Hello Overflow",
style: const TextStyle(
fontFamily: 'Open Sans',
fontSize: 15,
color: Color(0xff23233c),
letterSpacing: -0.44999999999999996,
fontWeight: FontWeight.w500,
),
softWrap: true,
overflow: TextOverflow.ellipsis,
),
)

fittedbox with maximum two lines in flutter

Container(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 60),
child: Text(
// subCategoryName,
"This is a very Long Text takes more space",
textAlign: TextAlign.center,
maxLines: 2,
overflow: TextOverflow.fade,
style: Theme.of(context)
.textTheme
.headline3
.copyWith(color: Colors.white, fontSize: 32),
),
),
),
Look this Picture
I need this line to be a maximum of two lines and shrink the size like a FittedBox (with BoxFit.fitWidth) if the text gets even longer
When I used the FittedBox it looking like this. But I need to take it up to 2 lines if needed.. Any solution is appreciated
Got it working by the reply of Sam Chan.
I used the AutoSizeText Package
If you don't want to add an pakage, here is workaround. You can check the length of the text and based on that you can handle it.
// declare a widget
Widget titleTextWidget = Text(
title,
style: TextStyle(
fontWeight: FontWeight.w600,
),
maxLines: 2,
textAlign: TextAlign.center,
);
// use condition to render it.
title.length > 45
? FittedBox(
child: titleTextWidget,
)
: titleTextWidget,

Scaling fonts with different screensizes

I am trying to scale my fonts so that they look the same on different screen sizes, large or small. I have the code below, which is linked to an underlying theme (flutter standard theme). I have tried manually changing the fontsize but this does not help since it looks different on all screen sizes.
Is there a recommended way to auto-scale fontsize depending on screen size?
Expanded(
child: Column(
children: <Widget>[
Padding(padding: EdgeInsets.all(MediaQuery.of(context).size.height/50)),
Text("We're finding your pizza!",
textAlign: TextAlign.center,
style: theme.textTheme.title.copyWith(fontSize: 26),
),
Padding(padding: EdgeInsets.all(MediaQuery.of(context).size.height/40)),
Text(
"We'll notify you when we've found one.",
textAlign: TextAlign.center,
style: theme.textTheme.title),
Padding(padding: EdgeInsets.all(MediaQuery.of(context).size.height/40)),
Text(
"Estimated wait time:",
textAlign: TextAlign.center,
style: theme.textTheme.title),
Padding(padding: EdgeInsets.all(MediaQuery.of(context).size.height/80)),
Text(
"< 1 minute.",
textAlign: TextAlign.center,
style: theme.textTheme.title,
)],
),
),
You can try auto_size_text
Installing
add this to your pubspec.yaml
auto_size_text: ^2.1.0
Usage
AutoSizeText(
'Your auto size text',
style: TextStyle(fontSize: 20),//minimum font size
maxLines: 2,//if needed
)