Text ellipsis not working when icon beside it in flutter - flutter

recently I am moving on the Flutter , I am making one list but stuck with Overflow error even ellipsis is there.
My requirement is Icon must be stick with text so I cant use expanded.

Wrap Text widget with Expanded or Flexible because over only works when it's parent has Expanded or Flexible .
or try this.
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Expanded(
child: Row(
children: const [
Flexible(
child: Text(
///your text here
'Yearly',
style: TextStyle(overflow: TextOverflow.ellipsis),
),
),
SizedBox(width: 10),
Icon(Icons.timer),
],
),
),
const SizedBox(width: 10),
Text('2', style: FontStyleUtility.blackInter18W600)
],
)

Wrap your text widget with the SizedBox() or Flexible() and give it a width with the max size of your choice
...
Flexible(
child: Text(
...
),
...
Or
...
SizedBox(
width: 180,
child: Text(
...
),
...

Try Wrapping the Text in a Container and provide the width you want then it will work.

Related

In Flutter, inside Row widget, I have taten two Text widget, I want space between them (like we do with "&nbsp" in Html),

In the above code I used Sized Box Widget but it didn't gives me anything?
How can I add space in it?
Android Emulator Images
Padding(padding: EdgeInsetsDirectional.fromSTEB(24, 4, 24, 0),
child:
Row(mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
'Discount',
style: AppTheme.of(context).subtitle2,
),
SizedBox(height: 20,),
Text(
'₹ ${Provider.of<Cart>(context,listen:false).discountPrice}',
style: AppTheme.of(context).subtitle1,
),
],
),
),
wrap with container/sizebox and add in row mainAxisAlignment: MainAxisAlignment.spaceAround
Using TextSpan() widget is better than instead of Row(). Like that.
RichText(
text: TextSpan(
children: [
TextSpan(text: 'Discount',style: AppTheme.of(context).subtitle2,),
WidgetSpan(
child: SizedBox(
width: 20, // your of space
),),
TextSpan(text: "${Provider.of<Cart(context,listen:false).discountPrice}",
style: AppTheme.of(context).subtitle1,),
],
),),
Edit: but I think in your question your code and your screen shoot is different. Please ask clearly. ** My answer is to space between horizontal text widget. **
That's a quite good approach to use SizedBox in this case, but you need to set width instead of height.

Text overflow on Flutter

I'm trying to put a text inside de window. However, it is out of pixels and I don't know how to fix it.
This is how it looks now:
This is part of my code:
SizedBox(
width: MediaQuery.of(context).size.width * 0.7,
child:
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(children: [
Text('FRI, 4 MAR · 16:00', style: dateStyle),
],),
Row(children: [
Text('Chess Competition at Tetuan',
style: eventStyle, textAlign: TextAlign.left),
],),
),
I want that if the text (that for the moment is hardcoded) can't be shown in the same line, automatically changes to the other.
Try below code, Wrap your Text inside Expanded Widget
SizedBox(
width: MediaQuery.of(context).size.width * 0.7,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Expanded(
child: Text(
'FRI, 4 MAR · 16:00',
),
),
],
),
Row(
children: [
Expanded(
child: Text(
'Chess Competition at Tetuan',
),
),
],
),
],
),
),
Result screen->
By default Row widget tries to give infinite width space for all its children so text widget is getting infinite width.
To make text widget render text on next line we need to provide fix width.
To achieve that you can use Expanded widget, it will take all space available such that it fits the constraint of the row.
SizedBox(
width: MediaQuery.of(context).size.width * 0.7,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Expanded(
child: Text(
'FRI, 4 MAR · 16:00',
),
),
],
),
Row(
children: [
Expanded(
child: Text(
'Chess Competition at Tetuan',
),
),
],
),
],
),
),
You may use Flexible to resize the widgets in rows and columns. It's mainly used to adjust the space of the different child widgets while keeping the relation with their parent widgets.
Meanwhile, Expanded changes the constraints sent to the children of rows and columns; it helps to fill the available spaces there. Therefore, when you wrap your child in an Expanded widget it fills up the empty spaces.
Providing these videos from the Flutter's Official YouTube channel just to help out people, who might look for this in the upcoming future...
1.Expanded
2.Flexible
Example:
Flexible(
child: Text()),
Expanded(
child: Text()),
Wrap your text widget in a Flexible widget as follows-
SizedBox(
width: MediaQuery.of(context).size.width * 0.7,
child:
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(children: [
Text('FRI, 4 MAR · 16:00', style: dateStyle),
],),
Row(children: [
//Start of flexible widget
Flexible(
child:Text('Chess Competition at Tetuan',
style: eventStyle, textAlign: TextAlign.left),
],),),
),

Clip the 1st widget with ellipsis (on overflow) in a Row of 3 Text widgets

I have a Row of 3 text widgets. I want the first two to stay together and the third one to be aligned at the end of the row. The first text has the potential to cause an overflow, and when that happens, I only want the first text to be clipped by an ellipsis. Note that the 2nd and 3rd text can vary in length too, so the width at which clipping should be done varies.
I have tried various things, with RichText, Spacer, Expanded, Flexible, etc. The closest I have come to a solution is with this:
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Flexible(
child: Text(
"Left most text that overflows the screen and creates an ellipsis",
overflow: TextOverflow.ellipsis,
),
),
Text(
"Xyz",
style: customStyle1,
),
Text(
" 10:20",
style: customStyle2,
),
],
),
This is what it leads to (with added colors). If the time stays to the right in the first row, the problem will be solved.
Adding a spacer between the last two texts causes the first to occupy just half the screen, i.e., the ellipsis gets added prematurely. Adding an Expanded to last Text causes the
"NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE" bug.
The first and the last row in this image are the desired outputs.
How do I accomplish this? And can anyone explain the issue with using a Spacer or an Expanded as mentioned above?
We can use two rows to align items, Flexible and Text softWrap to fix the overflow
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Expanded(
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: const [
Flexible(
child: Text(
"Left most text that overflows the screen and creates an ellipsis",
overflow: TextOverflow.ellipsis,
softWrap: false,
),
),
Text("Xyz"),
],
),
),
Text(
" 10:20",
),
],
),
You can use a constrained box
Row(
children: [
ConstrainedBox(
constraints: BoxConstraints(
maxWidth: MediaQuery.of(context).size.width / 2,
),
child: const Text(
"Left most text that overflows the screen and creates an ellipsis",
overflow: TextOverflow.ellipsis,
),
),
const Text('xyz'),
const Spacer(),
const Text('10:20'),
],
),
///you can use SizedBox and assign width and you can set your text. This is not the perfect solution but this works for me.
import 'package:flutter/material.dart';
class MyAseet extends StatefulWidget {
const MyAseet({Key? key}) : super(key: key);
#override
_MyAseetState createState() => _MyAseetState();
}
class _MyAseetState extends State<MyAseet> {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Padding(
padding: const EdgeInsets.only(left:20.0,top: 50),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.start,
children: [
SizedBox(
width: 200,
child: Text(
"Left most text that overflows the screen and creates an ellipsis",
overflow: TextOverflow.ellipsis,
maxLines: 1,
),
),
Text(
"Xyz",
style: TextStyle(color: Colors.grey),
),
Container(
color: Colors.blue,
child: Text(
" 10:20",
),
),
],
),
Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.start,
children: [
SizedBox(
width: 200,
child: Text(
"Left most text that overflows the screen and creates an ellipsis",
overflow: TextOverflow.ellipsis,
maxLines: 1,
),
),
Text(
"Xyz",
style: TextStyle(color: Colors.grey),
),
Container(
color: Colors.blue,
child: Text(
" 10:20",
),
),
],
),
],
),
),
);
}
}

Flutter Row Text Overflow

I can't get this to layout properly.
I'm trying to achieve
This is what I tried so far that gets me the closest to it but isn't exactly right.
Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
Flexible(
child: Row(children: [
PrefixIcon(),
Flexible(
child: DefaultTextStyle(
overflow: TextOverflow.ellipsis,
child: TextGoesHere(),
),
),
SuffixIcon(),
]),
),
TrailingIcon(),
]),
But Flexible with FlexFit.loose acts like Expanded so the SuffixIcon gets pushed to the end even though TextGoesHere is a short text.
I got so far
Please help.
If you consider ..from TextOverflow.ellipsis, this I've got so far with row.
color container just for visual
Widget
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text("Prefix"),
Expanded(
child: Container(
color: Colors.deepOrange,
child: Row(
children: [
Flexible(
child: Text(
"Very LongggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggText",
maxLines: 1,
),
),
Icon(
Icons.tag_sharp,
)
],
),
),
),
Text("TrailingIcon"),
],
),
Else, we can use LayoutBuilder if we know the prefix and Icons size.

Alignment inside a Row

I'm trying to space this two items inside a row, I need to fix the text on the center and the icon in the right in the Row, but it's not working.
Here is the image of how looks now
Here is the current code
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
myText(text, FontWeight.w700, Colors.white),
Icon(Icons.arrow_drop_down,color: Colors.white),
],
);
This Medium post is a cheat sheet for layouts in Flutter. Under the paragraph Expanded an attribute called flex is explained.
Play around with flex, but this example should get you going:
Row(
children: <Widget>[
Expanded(
child: Container(),
flex: 1,
),
Expanded(
child: myText(text, FontWeight.w700, Colors.white),
flex: 2,
),
Expanded(
child: Icon(Icons.arrow_drop_down,color: Colors.white),
flex: 1,
),
],
),