how to make the text go to the next line in flutter - flutter

I am trying to build a cv app that i can present my information. but when i write a long text it can not go to next line auto and i tried other solution but it does not work. here the code
SingleChildScrollView(
scrollDirection: Axis.vertical,
child: Flexible(
child: Text(
'longgggggggggggggggggggggggggggggggggggggg text',
overflow: TextOverflow.ellipsis,
softWrap: false,
style: TextStyle(
color: Colors.white,
fontFamily: 'NotoSans',
fontSize: 15,
),
),
),
),
here is the image
here is the image

Simply use \n inside the text.

You have to usesoftWrap: true, instead of softWrap: false, this will make the text go to the next line whenthere is no more space.
Just make sure the container where the text is, lets it go to the next line, if the height of the container is fixed it'll get the overflow error but on the vertical axis

Another simple way to do it is make a container and customize it a bit like you want for example you want border or not its up-to you. Add padding to it so the text inside the container will adjust itself automatically it wont go out of the container.

Related

set overflow TextOverflow.ellipsis text length Flutter

How to set the length of the overflow: TextOverflow.ellipsis becuase it truncate the text as you see in img. There is a empty space between the text 'This is the first...' and the nummber '#1520'. The overflow truncates the text after 'first...' word.
I want that the words 'card now' will be shown in the Row. How to fix it?, thanks
Container(
color: Colors.grey,
child: ListTile(
title: Container(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Flexible(child: Text('This is the first card now its time sad sad sad sads ad sdsadsads dsa dsad asdasdasd', overflow: TextOverflow.ellipsis, style: TextStyle(color: Colors.white),)),
Flexible(child: Text('#1520', overflow: TextOverflow.ellipsis, style: TextStyle(color: Colors.white),)),
],
),
),
),
),
I dont want all of my text to be visible, I just want to show some words in one line. If you look at the img, you will see an empty space between the text and the #1520. I want to use overflow and show: This is the first card now.. #1520
If you want all of your text to be visible just remove overflow property from the text widget.
If you want to limit your text to certain lines then add the maxLines property with overflow. like this
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Flexible(child: Text("This is the first card now its time sad sad sad sads ad sdsadsads dsa dsad asdasdasd", maxLines: 2, overflow: TextOverflow.ellipsis,)),
Flexible(child: Text("#1520"))
],
),
This is the default behaviour of how Text widget works with overflowing - it will truncate sentences with words if it detects the text width is greater than the width it can take. Because of this, sometimes it will cause the unexpected extra space between the truncated text and the container edge.
Here is a simple solution if you don't mind part of a word is covered:
simply apply softWrap: false to your text widget.
Text(
'Long long long long long long text text text text text',
softWrap: false,
maxLines: 1,
overflow: TextOverflow.fade,
)

Force remained text onto next line

I want to make textWidget1 wrap its remained text to the next line in a row.
like below :
how can I achieve this?
Using Wrap make the entire textWidget1to next line.
you may use triple quotes like in the following example:
Container(
child: Text(
'''Text1
Text2
''',
maxLines: 3,
style: TextStyle(
fontSize: 16.0, fontWeight: FontWeight.bold, color: Colors.white),
),
);
Run the above code on DartPad.
Hope I could help you.
Luis
You can wrap your Text inside a Container and the container wrapped by Flexible.
I solved my problem by drawing them.

FittedBox Flutter

i am trying to render a dynamic Text widget inside the fittedBox.
if the text to long, it will be decrease font-size and vice versa.
child: FittedBox(
fit: BoxFit.fitWidth,
child: Text(
text,
style: TextStyle(color: fontColor, height: 1),
),
),
But i got an error: RenderBox was not laid out: RenderFittedBox.
i seem like, inside fittedBox must be a block with exactly size, but Text widget does not have exactly size. Please help.
The best way to do this is by using AutoSizeText package. It is extremely performant and can easily replace Text. You can also set an overflow which I think you would want.
Here is an example:
SizedBox(
width:20,
child:AutoSizeText(
'The text to display. This is a really long text with a lot of words.',
overflow: TextOverflow.ellipsis,
style: TextStyle(fontSize: 20),
maxLines: 1,
),
),

How to set Flutter text max length value?

I am using Flutter programming. I need to display a title and a button next to the title. If the text length is less button also should display next to the text. If text length is more i want to fix the text length as maximum 200px and need to show the button next to the title.
I am using the below code. And working fine with the less text length.
Problem is: If text length is more, then my button is moving out of screen. You can see the behaviour in the below attached image.
Row(
children: <Widget>[
Container(
child: RichText(
overflow: TextOverflow.ellipsis,
strutStyle: StrutStyle(fontSize: 12.0),
text: TextSpan(
style: TextStyle(color: Colors.white), text: model.title),
),
),
Text("My Button"),
],
)
You can put maximum characters
text.substring(0,150)
I think there's no maxLength property for RichText and Text widget. If you are using RichText class, you'll have maxLines property that can be used. I tried to recreated your case and wrapped RichText widget with Flexible widget that lets the child widget to use only the minimum available space. But I wasn't able to replicate the error you mentioned. The long text I used gets ellipsis and the button doesn't go out of the screen. Note that I also used maxLines property which if you set to 2, will wrap the long text in new line without moving away the button and the long text will be shown as ellipsis. Sample working code below:
body: Center(
child: Row(
children: <Widget>[
Container(
child: Flexible(
child: RichText(
maxLines: 2,
overflow: TextOverflow.ellipsis,
strutStyle: StrutStyle(fontSize: 12.0),
text: TextSpan(
style: TextStyle(color: Colors.white), text: 'I am using Flutter programming. I need to display a title and a button next to the title. If the text length is less button also should display next to the text. If text length is more i want to fix the text length as maximum 200px and need to show the button next to the title.'),
),
)
),
Text("My Button"),
],
)
)
If you set maxLines to 1, the long text will wrap in ellipsis.
Hope this helps.
String addressText = userAddress.address.toString();
if(addressText.length > 22)
addressText = addressText.substring(0,22);
You can check length and split string if exceeds max length.
you can use Expanded or give fixed width to container of rich text, like this with Expanded widget:-
Row(
children: <Widget>[
Expanded(
child: RichText(
overflow: TextOverflow.ellipsis,
strutStyle: StrutStyle(fontSize: 12.0),
text: TextSpan(
style: TextStyle(color: Colors.white), text: model.title),
),
),
Text("My Button"),
],
)

Set spacing between two lines of text

I am trying to set spacing between two lines of text, but am not able to. Right now, the two lines are rendering after each other, but i want a bit of spacing between that i can adjust. I have tried using the Spacer widget but this makes the spacing too large. Is there a better way to do this?
Expanded(
child: Column(
children: <Widget>[
Text("We're finding your pizza.",
style: theme.textTheme.body1.copyWith(fontSize: 18)),
Text(
"We'll send you a notification.",
textAlign: TextAlign.center,
style: theme.textTheme.body1.copyWith(fontSize: 18)),
],
),
),
You can also set a specific padding between text lines by setting the height property in the TextStyle. With this you set the height for each line.
Text(
"Let's make\nsome pancakes",
style: TextStyle(
height: 1.2, //SETTING THIS CAN SOLVE YOUR PROBLEM
color: Colors.white,
fontSize: 20,
fontWeight: FontWeight.w300,
),
textAlign: TextAlign.center,
),
In fact, we can confirm from the docs that:
For most fonts, setting height to 1.0 is not the same as omitting or setting height to null because the fontSize sets the height of the EM-square, which is different than the font provided metrics for line height.
For more info: https://api.flutter.dev/flutter/painting/TextStyle/height.html
I posted this same answer at Flutter how do I remove unwanted padding from Text widget? to get the opposite result but it works the same way.
Spacer will take up all the available spaces left, and you may not need this. So you can simply use SizedBox widget.
Text("One"),
SizedBox(height: 20), // use this
Text("Two"),
you can simply use the SizedBox widget or Container widget with padding.
SizedBox(height: 15.0)
or
Container(
padding: const EdgeInsets.all(15.0))