How to use text overflow inside nested row/columns in flutter? - flutter

Here's my widget tree that's causing me the problems:
Container(
margin: EdgeInsets.all(15),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
ClipRRect(
borderRadius: cardBorderRadius,
child: Image(
image: NetworkImage(widget.article["thumbnail"]),
width: MediaQuery.of(context).size.width * .18,
height: MediaQuery.of(context).size.width * .18,
fit: BoxFit.cover,
),
),
Container(
margin: EdgeInsets.only(left: 15),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
padding: EdgeInsets.only(top: 15),
child: Text(
widget.article["title"],
overflow: TextOverflow.clip,
style: TextStyle(
fontWeight: FontWeight.w700,
fontSize: 28,
),
),
),
Container(
padding: EdgeInsets.only(top: 20),
child: Row(
children: <Widget>[
Container(
margin: EdgeInsets.only(right: 5),
child: Icon(Icons.star_border, size: 15),
),
Text(
'${widget.article["readTime"] ~/ 60} min',
overflow: TextOverflow.clip,
maxLines: 3,
softWrap: true,
style: TextStyle(
fontWeight: FontWeight.w500,
fontSize: 14,
),
),
],
),
),
],
),
),
],
),
);
Now the problem is I am getting this effect rather than text get clipped.
Note: I have tried other answers as well using the Expanded & Flexible widgets but none of them worked for me.
Any help will be appreciated!

Put the textfield into a flexible widget like this (work for me):
Row(
children: <Widget>[
Flexible(
child: Text(
"some text",
textAlign: TextAlign.start,
overflow: TextOverflow.ellipsis,
),
),
... more widgets
],
),

Related

how to build Proper ListTIle with image

i always build ListTile like this, the problem is the upper text 'Bluckcurrent' and the bottom text 'STOCK: 10' is not in the same line with the end of image. I want it to match the image height automatically. Sorry for my bad explanation, if you need more information please tell me i will update the question.
Container(
padding: EdgeInsets.fromLTRB(16, 8, 16, 8),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Container(
width: 90,
height: 100,
decoration: BoxDecoration(
color: Constants.blueLightColor.withOpacity(0.2),
borderRadius: BorderRadius.circular(16),
),
child: Icon(
Icons.image_outlined,
size: 42,
color: Constants.blueLightColor,
),
),
SizedBox(width: 10),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
'BLACKCURRENT MOUNT TEA',
style: TextStyle(
fontWeight: Constants.bold,
),
maxLines: 2,
),
Text(
'250ml',
style: TextStyle(
height: 1.4,
fontSize: 12,
color: Constants.greyColor,
),
),
Text(
'Rp. 8.000',
style: TextStyle(
height: 1.7,
fontWeight: Constants.bold,
),
),
Text(
'STOCK : 10',
style: TextStyle(
height: 1.7,
fontWeight: Constants.bold,
),
),
],
),
),
SizedBox(width: 16),
],
),
),
Add a sizedBox with specific height wrapping the row widget and the let the image fetch its width. And in the Column of the Text widgets add a spacer where you wish to add extra space
SizedBox(
height: 100,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Image.network(
"https://www.bastiaanmulder.nl/wp-content/uploads/2013/11/dummy-image-portrait.jpg",
fit: BoxFit.cover,
),
SizedBox(
width: 10,
),
SizedBox(
height: 200,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Text(
"Some title here",
style:
TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
),
SizedBox(
height: 10,
),
Text("Price: \$60"),
Spacer(),
Text(
"Stock : 10",
style:
TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
)
],
),
),
],
),
),
Just to show it will align properly, increasing the height to 200
Try: set Container height 100
child: Container(
height: 100,
padding: EdgeInsets.fromLTRB(16, 8, 16, 8),...
How about using MainAxisSize.min ?
It will fix column height to children height.
Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
mainAxisSize: MainAxisSize.min,
children: [
...
],
),
try this.
since image height is dynamic, then it will align vertically with all the column children on the right side
Card(
child: Container(
padding: EdgeInsets.all(8),
width: double.infinity,
child: Stack(
children: [
Positioned(
child: SizedBox(
// no need to set height
// SizedBox will strech following widget on the right
width: 50,
child: Image.network(
'https://picsum.photos/250?image=9',
fit: BoxFit.cover, // you can modified as you need
),
),
top: 0, //
left: 0,
bottom: 0), //
Container(
margin: EdgeInsets.only(left: 60),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'WWWWWWWWWWWWWWWWW',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 18,
),
),
SizedBox(
height: 10,
),
Text("lorem ipsum"),
SizedBox(
height:5,
),
Text("lorem ipsum"),
],
),
),
],
),
),
),
You can use IntrinsicHeight widget to wrap your Row.
IntrinsicHeight(child: Row())

How to place image & textfiled both at center in flutter

How to place image & textfield both at center in flutter.
Below is code what i have tried so far any help is appreciated!
Container(
child: Padding(
padding: const EdgeInsets.only(top: 32.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
child: Image.asset(
'images/Warning.png',
),
),
Padding(
padding: const EdgeInsets.only(left: 8.0),
child: Container(
alignment: Alignment.center,
child: Text(
'OTP is valid for 5 minutes only.',
style: TextStyle(
color: Color(0xFF4B4B4B),
fontSize: 12,
fontFamily: 'Soleil_Regular',
),
),
),
),
],
),
),
),
trying as some thing shown in the picture both text & image is in same line any help is appreciated!
Simple way of doing it by using RichText
RichText(
textAlign: TextAlign.center,
text: TextSpan(children: [
WidgetSpan(child: Icon(Icons.ac_unit)), // use image in your case instead of icon
TextSpan(text: "This is Text")
]))
Full Snippet
Container(
child: Padding(
padding: const EdgeInsets.only(top: 32.0),
child: Text.rich(TextSpan(
style: TextStyle(
color: Color(0xFF4B4B4B),
fontSize: 12,
fontFamily: 'Soleil_Regular'),
children: [
WidgetSpan(
child: Image.asset(
'images/Warning.png',
alignment: Alignment.center,
),
),
WidgetSpan(
child: SizedBox(
width: 10,
)),
TextSpan(
text: 'OTP is valid for 5 minutes only.',
),
],
)),
),
),
Give your image a size and you will get your result
Container(
child: Padding(
padding: const EdgeInsets.only(top: 32.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
child: Image.network(
'http://orbitsystemsinc.com/images/banner-round-bg.png',width: 40,height: 40,
),
),
Padding(
padding: const EdgeInsets.only(left: 8.0),
child: Container(
alignment: Alignment.center,
child: Text(
'OTP is valid for 5 minutes only.',
style: TextStyle(
color: Color(0xFF4B4B4B),
fontSize: 12,
fontFamily: 'Soleil_Regular',
),
),
),
),
],
),
),
)

How to right align a Row in a card within a ListView.Builder?

I'm having an issue aligning a Row at the end of a card within a ListView.Builder. When I apply a
mainAxisAlignment: MainAxisAlignment.end it doesn't go to right side of the card like I want. The distance right it goes varies on how long the data before it is. What Am I doing wrong? Any advice is welcome! Thanks!
This is what it looks like. I want the dollar amounts to be all the way to the right.
Here is the code for the ListView.Builder
return ListView.builder(
itemCount: transaction.length,
shrinkWrap: true,
itemBuilder: (context, index) {
return Padding(
padding: EdgeInsets.symmetric(vertical: 1.0, horizontal: 4.0),
child: Card(
color: (index % 2 == 0) ? greycolor : Colors.white,
child: Container(
height: 60,
child: Row(
children: <Widget>[
Expanded(
flex: 1,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
margin: EdgeInsets.only(left: 5, top: 13),
child: AutoSizeText(transaction[index].date,
maxLines: 1,
style: TextStyle(
fontSize: 14, color: Colors.black),
textAlign: TextAlign.left),
),
],
),),
Flexible(
flex: 3,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: EdgeInsets.only(top: 13, left: 10),
child: AutoSizeText(transaction[index].tester,
maxLines: 1,
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.black,
fontFamily: 'Montserrat'),)
),
Padding(
padding: EdgeInsets.only(left: 10),
child:
AutoSizeText(
'${transaction[index].test}',
overflow: TextOverflow.ellipsis,
style: TextStyle(
color: Colors.black,
fontStyle: FontStyle.italic),
),
),
],
),),
Flexible(
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Padding(
padding:
EdgeInsets.only(top: 13),
child: Container(
child: Text(
'\$${transaction[index].amount}',
style: TextStyle(
fontSize: 16,
color: Colors.black),
textAlign: TextAlign.right),
),
),
],
),)
],
)),
),
);
},
);
just replace Flexible with Expanded
Flexible takes only the needed space, and Expanded takes all available space
see this To clarify more
I hope this is helpful

Flutter Flexible Text is overflowing in Column

I am creating a list tile that has a leading image downloaded from the internet.
The image could be landscape or portrait.
Based on the image size space available for the title text change.
When the image get downloaded from the internet available space get re-adjusted.
To fix this problem I used Flexible widget and TextOverflow.ellipsis.
This works well until I add the Flexible to a column to add a sub heading.
Now I am getting overflow error for the longer title texts.
Any idea how to fix this?
Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
constraints: BoxConstraints(minHeight: 50, maxHeight: 100),
child: ClipRRect(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(8),
bottomLeft: Radius.circular(8)),
child: FadeInImage(
placeholder: AssetImage('images/placeholder-image.png'),
image: NetworkImage(post.imageURL),
fit: BoxFit.cover,
),
),
),
Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Flexible(
child: Container(
padding: const EdgeInsets.fromLTRB(8, 8, 5, 5),
child: new Text(
post.title,
overflow: TextOverflow.ellipsis,
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 15,
color: Theme.of(context).primaryColor),
),
),
),
],
),
],
)
Wrap the Column with a Flexible instead of wrapping the Text with a Flexible:
Flexible(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Container(
padding: const EdgeInsets.fromLTRB(8, 8, 5, 5),
child: new Text(
post.title,
overflow: TextOverflow.ellipsis,
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 15,
color: Theme.of(context).primaryColor),
),
),
],
),
),
Use Expanded
class MyWidget extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
constraints: BoxConstraints(minHeight: 50, maxHeight: 100),
child: ClipRRect(
borderRadius: const BorderRadius.horizontal(
left: Radius.circular(8),
),
child: FadeInImage(
placeholder: NetworkImage('https://picsum.photos/100'),
image: NetworkImage('https://picsum.photos/100'),
fit: BoxFit.cover,
),
),
),
Expanded(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 8.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Title',
overflow: TextOverflow.ellipsis,
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 15,
color: Colors.white,
),
),
Text(
'Subtitle',
overflow: TextOverflow.ellipsis,
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 15,
color: Colors.white,
),
),
],
),
),
),
],
);
}
}

Flutter text is not ellipsized in the list

I have a list of object. There is a name that can be long and according to design it should end with three dots if it can't fit
Container(
height: 72,
constraints: BoxConstraints(minWidth: double.infinity),
child: Row(
children: <Widget>[
Container(
margin: const EdgeInsets.only(left: 16.0, right: 16.0),
child: CircleAvatar(
radius: 20.0,
backgroundImage: NetworkImage(_model._organizerLogo),
backgroundColor: Colors.transparent,
),
),
Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
padding: EdgeInsets.only(right: 8.0),
child: Text(
_model._eventName,
style: TextStyle(
fontSize: 15,
color: Colors.black,
fontWeight: FontWeight.w500),
textAlign: TextAlign.start,
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
),
...
])
],
),
)
Wrapping Container in Flexible or Expandable didn't work.
How to make oversized text ellipsis? Thanks!
Add Expanded to your Column to give it a fixed width based on the remaining space.
Container(
height: 72,
constraints: BoxConstraints(minWidth: double.infinity),
child: Row(
children: <Widget>[
Container(
margin: const EdgeInsets.only(left: 16.0, right: 16.0),
child: CircleAvatar(
radius: 20.0,
backgroundImage: NetworkImage(_model._organizerLogo),
backgroundColor: Colors.transparent,
),
),
Expanded(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
padding: EdgeInsets.only(right: 8.0),
child: Text(
_model._eventName,
style: TextStyle(
fontSize: 15,
color: Colors.black,
fontWeight: FontWeight.w500),
textAlign: TextAlign.start,
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
),
...
])
),
],
),
)
Here is the working solution:
Container(
height: 72,
constraints: BoxConstraints(minWidth: double.infinity),
child: Row(
children: <Widget>[
Container(
margin: const EdgeInsets.only(left: 16.0, right: 16.0),
child: CircleAvatar(
radius: 20.0,
backgroundImage: NetworkImage(poolImageUrl),
backgroundColor: Colors.transparent,
),
),
Expanded(
child: Container(
padding: EdgeInsets.only(right: 8.0),
child: Text(
"This will not overflow now, because we have put it in Expanded, you can try any long string here for test",
style: TextStyle(fontSize: 15, color: Colors.black, fontWeight: FontWeight.w500),
textAlign: TextAlign.start,
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
),
),
],
),
),