flutter text widget overflow and multiline - flutter

some times when I'm coding with flutter, in the Text widget when I use long text, some times I get overflow message, and some times the message work right and be in many lines, why that's happen with me? please explain to me how to avoid this problem.

there are some key property in Text Widgt:
softWrap // if overflow then can wrap new line
overflow // if overflow the overed text style
maxLines // max lines
and if the parent container of Text Widgt has a width less or eq than device width then text overflowed will wrap to multiple lines, or Text will throw overflow error if text is too long
give a width to parent container
for example:
// overflow error
Container(
child: Column(
children: <Widget>[
Text("hellohellohellohellohellohellohellohellohe" +
"llohellohellohellohellohellohellohellohellohe" +
"llohellohellohellohellohellohello"),
Text("dd")
],
),
)
give parent container fix width
// overflow will change to multiple lines, notice padding and margin's effect
Container(
width: 100,
child: Column(
children: <Widget>[
Text("hellohellohellohellohellohellohellohellohe" +
"llohellohellohellohellohellohellohellohellohe" +
"llohellohellohellohellohellohello"),
Text("dd")
],
),
)
or let Text fill parant container using Expended or Flexible
Expanded(
child: Column(
children: <Widget>[
Text("hellohellohellohellohellohellohellohellohe" +
"llohellohellohellohellohellohellohellohellohe" +
"llohellohellohellohellohellohello"),
Text("dd")
],
),
)
// or
Flexible(
child: Column(
children: <Widget>[
Text("hellohellohellohellohellohellohellohellohe" +
"llohellohellohellohellohellohellohellohellohe" +
"llohellohellohellohellohellohello"),
Text("dd")
],
),
)

Related

i need to remove this text over flow in flutter. how can i do that?

I am facing error in the Text widget which is in Column.
body: Padding(
padding: const EdgeInsets.all(20),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
widget.songModel.displayNameWOExt, <------- here is my text
),
Text(
widget.songModel.artist.toString(),
overflow: TextOverflow.fade,
),
],
),
],
), ), );
This is what happened to me
This is what i need look like
Wrap the Text that is inside a Row widget with an Expanded widget:
Expanded(child: Text(/* Here the text*/),
wrap the text with the container and give a specific size to the container after that use overflow: TextOverflow.fade or any other property of "overflow", because these properties only work when you give a size to textfield but you cant give size to textfield so wrap it with container.
try embedding the Text() widget inside a Expanded() widget expanded documentation
Expanded is a widget that expands a child of a Row, Column, or Flex so that the child fills the available space.

Flutter Layouts, expand Container in Column to match other widgets width

I am creating chat bubbles, I want the width to be defined by the message itself, but then I added a "header" (a Row) with the name of the sender and a decorative line (a Container).
I want this line to fill the available space but when I expand it it pushes the width of the bubble to its maximum regardles of the width of the message. In this sceenshots you can see that the recieved messages are too wide for its content.
This is a simplified version of the code, If I don't put the expanded the red Container is invisible (width 0), with the expanded takes the maximum space available but I want column to remain of the width that determines the message.
Thanks a lot.
return Container(
color: Colors.grey.shade200,
constraints:
BoxConstraints(maxWidth: (MediaQuery.of(context).size.width * 0.7)),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Row(
mainAxisSize: MainAxisSize.min,
children: [
Text("Juan"),
Expanded(
child: Container(
color: Colors.red,
height: 3,
),
)
],
),
Text("Text of the message"),
],
));
pskink nailed it. His answer in the comment:
you need: child: IntrinsicWidth(child: Column(... –
pskink
Hopes this also helps somebody else.

Flutter - Text overflow in Row when another widget in the same Row is flexible

I want to implement a Row having two Text widgets and a middle line like this:
The following code is my current implementation.
Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(
'Text',
overflow: TextOverflow.ellipsis
),
Expanded(
child: Container(
padding: EdgeInsets.only(left: 16, right: 16),
height: 1,
child: Container(
color: Colors.black
)
),
),
Text(
'Right Text'
)
]
)
The problem of this code is that the left text overflows if it is long like this:
The expected output is:
Wrapping the first Text widget with a Flexible can solve the overflow issue, but the output is not the same as the expected output because of the Expanded widget that fills the space between two Text widgets with a line.
How can I achieve this? Any help is highly appreciated!

Flutter: how to center a child, but set a limit on how big it can grow on one side of the margin

I have a home/login screen which is made up of a column that fills the entire screen like so:
Column(
children: <Widget>[
Expanded(
child: Container(
child: Logo(),
),
),
showThis ? This() : That(),
],
),
The second child of the column is dynamic and can have different heights, and this screen will have inputs so the keyboard will also affect the height.
I want to center Logo() vertically within the container when it's small (e.g. when keyboard is active), but limit how much the 'top margin' is able to grow, so that when the keyboard is hidden and This()/That() is small enough, Logo() will be in a static position on the screen, say 150 from the top (no longer centred vertically).
One method I have tried was using 2 empty Expanded() above and below Logo() and wrapping the top part in a ConstraintedBox(), but I am not able to get it to behave correctly.
Have you tried with Center() and also if you wanna in a specific position use Stack, for example
Stack(
children: <Widget>[
showThis(),
Positioned(top: 150, child: Logo())
],
)
This is what ended up working for me:
Column(
children: <Widget>[
Expanded(
child: Column(
children: <Widget>[
// constrain top spacing with a max height
Flexible(child: Container(height: 160)),
_logo(),
Spacer(),
],
),
),
_bottomWidget(),
],
),

How to make a Row widget a Flexible widget?

Dart file: https://drive.google.com/file/d/1HqM_nxCiCCmx-swLInrSQfwwAmiXRVK3/view?usp=sharing
I have been getting this message: Right Overflowed by 200 pixels
You can see the code and please help to solve it and make the ow widget a flexible one without changing the alignment
see the image here
I have tried SingleChildScrollView, Wrap but all of them change the alignment to center.
Code:
Row(
children: <Widget>[
Icon(Icons.mail),
Text(
" " + usersData[index]['email'],
style: TextStyle(fontSize: 23),
),
],
),
try to change your Row Widget into Column.
If your Row Widget is like this:
Row(
children: <Widget>[
new Text("..."),
]
)
Change it to :
Column(
crossAxisAlignment: CrossAxisAlignment.start, // for the horizontal alignment
children: <Widget>[
new Text("..."),
]
)