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.
Related
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?
So if I want to seperate two texts I would use padding, but how do I bring them so close to each other? I don't want any Space between the bottom of the "Test" and the top of the "Admin".
There is no sizedboxes or padding in between them.
Code:
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
CircleAvatar(
radius: Sizes.appropriateWidth(55),
backgroundImage:
NetworkImage("https://via.placeholder.com/140x100"),
),
SizedBox(
height: Sizes.appropriateHeight(9),
),
Text(
"Test",
style: TextStyle(
fontFamily: 'SF Pro Display Bold',
fontSize: Sizes.appropriateWidth(37),
color: Color(0xFF000000),
),
),
Text(
"Admin",
style: TextStyle(
fontFamily: 'SF Pro Display Regular',
fontSize: Sizes.appropriateWidth(62),
color: Color(0xFF8D99AE),
),
),
],
),
How do I do this in flutter? Thank you!
UPDATE
You could also use the TextStyle.height parameter to achieve the same effect, Like this,
Text(
"Admin",
style: TextStyle(
fontFamily: 'SF Pro Display Regular',
fontSize: 62,
height: 0.7,
color: Color(0xFF8D99AE),
),
),
OR
Transform.translate widget to the rescue.
The Transform widget allows you to perform any kind of transformations while painting your child.
Transform.translate is the named constructor for performing a translation by an Offset. Just wrap your child and give it an Offset to get it working.
You can use it like this,
Transform.translate(
offset: Offset.fromDirection(-pi / 2, 20),
child: Text(
"Admin",
style: TextStyle(
fontFamily: 'SF Pro Display Regular',
fontSize: 62,
color: Color(0xFF8D99AE),
),
),
),
In offset: Offset.fromDirection(-pi / 2, 20), -pi / 2 means towards the top and 20 is the distance to move.
Play with the distance and see what works for you.
There is no padding from flutter itself, the padding come from the typo, imagine you have something like ÓÛÌmAm, you will need extra space for those ` ^ ' on uppercase letters. You cannot get rid off this space. For solutions you can look at Nisanth Reddy answer.
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,
),
)
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,
Sometimes, the text widget does a line break, and sometimes don't. I tried to add overflow and maxline but it doesn't work.
This is the code:
Text(
ArticleData().articleBank[index].articleTitle,
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20.0,
color: Colors.black,
),
),
This is what, I will try to build:
You can use FittedBox class.
Scales and positions its child within itself according to fit. The fit and alignment arguments must not be null.
FittedBox(
Key key,
fit: BoxFit.none,
alignment: Alignment.center,
child: Text(
ArticleData().articleBank[index].articleTitle,
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20.0,
color: Colors.black,
),
),
)