fittedbox with maximum two lines in flutter - 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,

Related

Add text field with text paragraph in Flutter

I am trying to add a text field inside the paragraph in flutter but not getting success below screen shot what I want to achive.
I am trying the below code
RichText(
text: TextSpan(
text: "“",
style: TextStyle(
color:
const Color(0xff56A1D5),
fontSize: 20.sp),
children: <InlineSpan>[
WidgetSpan(
child: Text(
"I want to talk with you about ",
textAlign: TextAlign.center,
style: TextStyle(
color: const Color(
0xff506274),
fontSize: 20.sp),
),
),
const WidgetSpan(
child: SizedBox(
width: 120,
child: TextField(
decoration:
InputDecoration(
isDense: true,
contentPadding:
EdgeInsets.all(0),
),
),
),
),
WidgetSpan(
child: Text(
"(the actions, attitude, or behavior)",
textAlign: TextAlign.center,
style: TextStyle(
color: const Color(
0xff506274),
fontSize: 20.sp),
),
),
],
),
),
But My output is look like below.
Can anyone show me where I am going to wrong or How can I achieve the same as the above image without leaving extra space?
Thanks in advance.
Your code is perfectly right. as (the actions, attitude, or behavior) this text does not fit in the screen it came into a new line. For checking this reduce your text then you may understand the issue.
Your code output when I run your code:
Desktop:
Mobile:

Text resize while maintaining multiline

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,
)

How do I make two texts touch each other in a column in Flutter?

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.

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,
),
)