I am building a widget to look like Tweets. At the moment, the header is the following row:
Row(
mainAxisAlignment: MainAxisAlignment.start,
mainAxisSize: MainAxisSize.max,
children: [
Flexible(
flex: 3,
child: Container(
margin: const EdgeInsets.only(right: 5.0),
child: Text(
poster.name,
style: Theme.of(context)
.textTheme
.headline4!
.copyWith(fontWeight: FontWeight.bold, fontSize: 14),
),
),
),
Expanded(
flex: 14,
child: Text(
'${poster.handle} · ${timeago.format(wave.createdAt)}',
style:
Theme.of(context).textTheme.headline5!.copyWith(fontSize: 12),
overflow: TextOverflow.ellipsis,
),
),
Spacer(),
if (showPopup)
WaveTilePopup(
poster: poster, wave: wave, user: user, onDeleted: onDeleted),
],
),
Which look like this:
When i increase the flex property of the name text, it will fix the formatting on the name, however shorter names will result in the PopUp being away from the right alignment, which is where I want it to be in a consistent manner:
Any ideas?
Thanks!
Instead of all hard-coded flex, you can just wrap your more button with expanded and align widget like this:
Row(
mainAxisAlignment: MainAxisAlignment.start,
mainAxisSize: MainAxisSize.max,
children: [
Container(
margin: const EdgeInsets.only(right: 5.0),
child: Text(
poster.name,
style: Theme.of(context)
.textTheme
.headline4!
.copyWith(fontWeight: FontWeight.bold, fontSize: 14),
),
),
Text(
'${poster.handle} · ${timeago.format(wave.createdAt)}',
style: Theme.of(context)
.textTheme
.headline5!
.copyWith(fontSize: 12),
overflow: TextOverflow.ellipsis,
),
Expanded(
child: Align(
alignment: Alignment.centerRight,
child: showPopup
? WaveTilePopup(
poster: poster,
wave: wave,
user: user,
onDeleted: onDeleted)
: SizedBox(),
),
),
],
),
Related
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children:const [
Padding(
padding: EdgeInsets.only(top: 30, bottom: 5.0),
child: Text(
"Data#data",
style: TextStyle(color: Colors.white),
),
),
Text(
"title",
style: TextStyle(fontSize: 18),
),
Text(
'datadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadata',
overflow: TextOverflow.ellipsis,
),
],
),
Expanded(
child: Text(
'datadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadata',
overflow: TextOverflow.ellipsis,
),)
Your column is inside a row widget and inside your column you use a long string so for telling column how much space should take you need wrap your column with Expanded widget:
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: const [
Padding(
padding: EdgeInsets.only(top: 30, bottom: 5.0),
child: Text(
"Data#data",
style: TextStyle(color: Colors.white),
),
),
Text(
"title",
style: TextStyle(fontSize: 18),
),
Text(
'datadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadata',
overflow: TextOverflow.ellipsis,
),
],
),
),
You should wrap your Text in a Flexible to let your element know that it's ok for the Text to be narrower than its intrinsic width. Expanded will also work.
you can encapsulate the text with the expanded widget and add the maxlines attribute
Expanded(child:
Text("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
maxLines:3,
overflow:TextOverflow.ellipsis ))
see the video.
I'm displaying text in a ListView.Builder and if some of the text is too long I'm using the overflow: TextOverflow.ellipsis property of the Text widget to ensure it doesn't cause an overflow error. Well when text is too long, it uses the TextOverflow correctly, but it also shifts the text to the right. I want to keep the text all aligned. Could this be caused by the TextOverflow or is this a layout issue?
This is an image of one of the pieces of text that had ellipsis used on it due to it being too long. As you can see it is shifted to the right. This only occurs when an ellipsis is used though. If the Text doesn't need an ellipsis it doesn't get shifted to the right.
This is the code that renders this entire page. The bit that I's being moved to the right is where the the text displays mileage[index].trippurpose I've included the entire code incase it is a layout issue that I keep overlooking.
Container(
height: 60,
child: Row(
children: <Widget>[
Expanded(
flex: 1,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
margin: EdgeInsets.only(left: 5, top: 13),
child: Text(mileage[index].tripdate,
style: TextStyle(
fontSize: 14, color: Colors.black),
textAlign: TextAlign.left),
),
],
),),
Expanded(
flex: 3,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: EdgeInsets.only(top: 13, left: 10),
child: Text(mileage[index].trippurpose,
style: TextStyle(
fontSize: 15,
fontWeight: FontWeight.bold,
color: Colors.black,
fontFamily: 'Montserrat'),
overflow: TextOverflow.ellipsis,
softWrap: true,
textAlign: TextAlign.center),),
Padding(
padding: EdgeInsets.only(left: 10),
child: Row(
children: [Text(
'${mileage[index].tripfrom} - ',
overflow: TextOverflow.ellipsis,
style: TextStyle(
color: Colors.black,
fontStyle: FontStyle.italic),
),
Padding(
padding: EdgeInsets.only(),
child:Text(
mileage[index].tripto,
overflow: TextOverflow.ellipsis,
softWrap: false,
style: TextStyle(
color: Colors.black,
fontStyle: FontStyle.italic
),
),
),
],
),
),
],
),),
Expanded(
flex: 1,
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Padding(
padding:
EdgeInsets.only(top: 13, right: 10),
child: Container(
child: Text(
mileage[index].triptotalmiles,
style: TextStyle(
fontSize: 16,
color: Colors.black),
textAlign: TextAlign.right),
),
),
],
),)
],
))
Here is a sample code, you can use to create design as per your code.
Container(
child: Row(
children: [
Flexible(
flex: 1,
child: Text(
'THIS IS A TEST TEXT',
overflow: TextOverflow.ellipsis,
),
),
Flexible(
flex: 3,
fit: FlexFit.tight,
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'THIS IS A TEST TEXT',
overflow: TextOverflow.ellipsis,
),
Text(
'THIS IS A TEST TEXT',
overflow: TextOverflow.ellipsis,
),
],
),
),
Flexible(
flex: 1,
child: Text(
'THIS IS A TEST TEXT',
overflow: TextOverflow.ellipsis,
),
),
],
),
)
Note :- You are applying wrong logics to your code. flex is best fit with Flexible widget. Also check FlexFit.tight.
I'm building an expenses app, and have:
As you can see, I have a text overflow problem. I want that text to trim with ellipses, but I'm not sure how to do it. Currently I have:
Widget buildCardLeft() {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
child: Text(
this.transaction.title,
style: TextStyle(fontSize: 24),
overflow: TextOverflow.ellipsis,
),
),
SizedBox(
height: 8,
),
Text(
dateFormat.format(this.transaction.date),
style: TextStyle(color: Colors.grey),
)
],
);
What's the correct way to get this text to truncate?
you can say like:
Text(
this.transaction.title.replaceRange(25, this.transaction.title.length, "...",
style: TextStyle(fontSize: 24),
overflow: TextOverflow.ellipsis,
)
Solved with Flexible:
Widget buildCardLeft() {
return Container(
child: Flexible(
flex: 1,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
this.transaction.title,
style: TextStyle(fontSize: 24),
overflow: TextOverflow.ellipsis,
),
SizedBox(
height: 8,
),
Text(
dateFormat.format(this.transaction.date),
style: TextStyle(color: Colors.grey),
)
],
),
),
);
}
i am trying to add a horizontal divider to my current layout but for some reason the divider is not appearing.
below is my code i tried
return Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
mainAxisSize: MainAxisSize.max,
children: [
Expanded(
flex: 1,
child: Column(children: <Widget>[
Text('Total Outstanding', textAlign: TextAlign.center,),
Padding(
padding: EdgeInsets.all(3.0),
child: Text('\$345.55',textAlign: TextAlign.center, style: TextStyle(color: Colors.green,)),
),
])),
VerticalDivider( width: 0.0, indent: 0.0, color: black),
Expanded(
flex: 1,
child: Column(children: [
Text('Total Received', textAlign: TextAlign.center,),
Padding(
padding: EdgeInsets.all(3.0),
child: Text('\$1806.50',textAlign: TextAlign.center, style: TextStyle(color: Colors.green)),
),
],
)
),Divider(color: Colors.black54),
],
);
i am expecting Divider(color: Colors.black54), in my code will draw the divider. see the picture below. the red line i drew should be where the divider should appear. can someone help me modify my code so that the horizontal divider can appear where the red line is? also there should be some padding on the start of the screen and the end of the screen. thanks in advance
Column(
children: <Widget>[
SizedBox(
height: 56,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Expanded(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'Total Outstanding',
textAlign: TextAlign.center,
),
Padding(
padding: EdgeInsets.all(3.0),
child: Text('\$345.55',
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.green,
)),
),
],
),
),
VerticalDivider(width: 1.0, color: Colors.black),
Expanded(
flex: 1,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'Total Received',
textAlign: TextAlign.center,
),
Padding(
padding: EdgeInsets.all(3.0),
child: Text('\$1806.50', textAlign: TextAlign.center, style: TextStyle(color: Colors.green)),
),
],
),
),
],
),
),
SizedBox(height: 12),
Container(height: 2, color: Colors.black54),
],
),
I have a column with three rows:
#override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () {
print('card of course clicked');
},
child: Card(
shape: RoundedRectangleBorder(borderRadius:
BorderRadius.circular(8.0)),
child: Container(
height: 144,
child: Row(children: [
Padding(
padding: EdgeInsets.all(16.0),
child: ClipRRect(
borderRadius: BorderRadius.all(Radius.circular(8.0)),
child: Image.network(_model._schoolThumb))),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
SizedBox(height: 16),
Align(
alignment: Alignment.center,
child: Text(_model._schoolName,
style: TextStyle(
color: Colors.black,
fontWeight: FontWeight.w600,
fontSize: 18)),
),
SizedBox(height: 12),
Row(
children: <Widget>[
Icon(Icons.location_on, color: Colors.grey[700]),
Container(width: 8),
Text(_model._guides,
style: TextStyle(
fontSize: 14,
color: Colors.black,
fontWeight: FontWeight.w400)),
],
),
SizedBox(height: 12),
Row(
children: <Widget>[
Icon(Icons.attach_money, color: Colors.grey[700]),
Container(width: 8),
Text(_model._priceFrom,
style: TextStyle(
fontSize: 14,
color: Colors.black,
fontWeight: FontWeight.w400))
],
)
],
)
]),
),
),
);
}
As a result, I have three rows aligned to start.
How can I align the first Text() in the center? Giving properties to the text and wrapping in Expanded, Container, Stack didn't help, or I did it in the wrong way. Thanks in advance!
EDIT
Whole build() method attached
Problem:
By default, the width of the Column is set to the width of the widest child widget of Column.
Solution:
You need to wrap your Column with either Expanded or Flexible.
Like below.
Expanded(
child: Column(
children: <Widget>[
...
],
),
)
or
Flexible(
child: Column(
children: <Widget>[
...
],
),
)
And then need to use the Align or Center widget to set the alignment to your child widget in your case the Text widget.
Like below:
Align(
alignment: Alignment.center,
child: Text(
"Test",
style: TextStyle(
color: Colors.black,
fontWeight: FontWeight.w600,
fontSize: 18,
),
),
),
or
Center(
child: Text(
"Test",
style: TextStyle(
color: Colors.black,
fontWeight: FontWeight.w600,
fontSize: 18,
),
),
),
UPDATE
You haven't given a width to the column so it does not know the remaining width that it can take. So wrap your column in a flexible which will give it the width remaining to expand.
Flexible(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
SizedBox(height: 16),
Row(