How can I set the width of a text based in the flexible row above in Flutter? - flutter

I have this card with a flexible Row that is setting the width size of the card and beneath it I have a text field that should only be as large as the Row above, but as the text is bigger, it streches the Card.
How can I fix the width of the Text to be the same as the flexible Row without jumping line with \n?
Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(14)
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
mainAxisSize: MainAxisSize.min,
children: [
Padding(
padding: const EdgeInsets.only(top: 24.0, right: 12),
child: Image.asset('assets/card_recommendation.png'),
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.only(top: 16.0),
child: Text(
'Elimine',
style: TextStyles.paragraphSmall12DarkerGreyMedium,
),
),
],
)
]
),
Padding(
padding: const EdgeInsets.symmetric(vertical: 20, horizontal: 24),
child: Text(
'Zerando a anuidade do seu cartão de crédito',
style: TextStyles.paragraphSmall12DarkerGreyMedium,
),
)
],
),
),

Try use FittedBox
If the text is too large, FittedBox will automatically decrease the font size so that it fits inside the Card's space.

Related

Stack inside column not working. What should I use instead?

I want to create this widget. The pseudocode I tried is just like this:
Column
Container (My Widget)
Column
Image
Stack
Message Text
Positioned
Status Text
Constraints:
Status must look like it stays inside the text widget. But it shouldn't be on it.
Text and image sizes are not fixed !!!
Problem: Stack inside column without size is not working. So status text appears under the text widget as centered.
return MessageBallon(
directory: message.direction,
childElement: Column(
children: [
message.hasMedia
? Container(
padding: EdgeInsets.only(bottom: height * .05),
child: CustomMedia(
src: message.media,
),
)
: const SizedBox(),
Stack(
children: [
Container(
padding: const EdgeInsets.only(
bottom: 13,
top: 18,
),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: [
Container(
constraints: BoxConstraints(maxWidth: width * .53),
child: Text(
message.content,
style: TextStyles.normalTextBlack,
),
),
],
),
),
Positioned(
bottom: 0,
right: 0,
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Padding(
padding: const EdgeInsets.only(right: 10),
child: Text(
message.messageDate,
style: TextStyles.detailText,
),
),
message.direction == SmsDirection.incoming
? Align(
alignment: Alignment.centerRight,
child: messageStatus,
)
: const SizedBox(),
],
),
)
],
)
],
),
);
Just use Scaffold with FloatingActionButton for your status.
Rest Image and Text can be inside a column.
You can achieve something like the below only with these widgets.

Row mainAxisAlignment spaceBetween property not working - Flutter

I'm building a message tile for chat screen in the flutter. I'm making the use of spaceBetween property of mainAxisAlignment in row to keep both text widget apart from each other but it's not making any impact
here is my code
Row(
mainAxisAlignment: messageModel.sender == FirebaseAuth.instance.currentUser!.uid.trim() ? MainAxisAlignment.start : MainAxisAlignment.end,
children: [
Container(
margin: const EdgeInsets.only(bottom: 15.0),
padding: const EdgeInsets.symmetric(vertical: 10.0, horizontal: 15.0),
decoration: BoxDecoration(
color: const Colors.greenAccent,
borderRadius: BorderRadius.circular(15.0)
),
child: Column( // Used expanded property on this, but that didn't work too
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(messageModel.message.toString(), style: GoogleFonts.oswald(color: Colors.white)),
const SizedBox(height: 2.0),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween, // not working
children: [
Text(DateFormat.jm().format(messageModel.createdOn!).toString(), style: GoogleFonts.oswald(color: Colors.white, fontSize: 12.0,)),
// also tried to make the use of spacer widget, but that didn't work too
const Icon(CustomIcons.check, color: Colors.white, size: 12.0)
],
)
],
),
)
],
);
I'm trying to keep time and tick icon apart from each other
the problem here is that the row is occupying the least amount of width possible for what it stores inside it, and that is that there is no separation since there is no space available inside the row for said separation, you could do the following to solve this . At least it works for me to handle it that way.
you need to wrap the row in a container to which you assign a min value of width and a max value if you want to anyway. I only left it with minimum value
Container(
constraints: const BoxConstraints(minWidth: 120),
child: Row(
mainAxisAlignment: MainAxisAlignment
.spaceBetween, // not working
mainAxisSize: MainAxisSize.max,
children: [
Text('fecha X',
style: GoogleFonts.oswald(
color: Colors.white,
fontSize: 12.0,
)), // also tried to make the use of spacer widget, but that didn't work too
const Icon(Icons.check,
color: Colors.white, size: 12.0)
],
),
)

how to make padding align right with row in flutter

Now I am using row to horizon some component in flutter, this is my code:
Row(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Padding(
padding: const EdgeInsets.only(top: 8, bottom: 8.0),
child: Text(
item.subName,
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w600,
),
),
),
Padding(
padding: const EdgeInsets.only(top: 8,bottom: 8.0,right: 1),
child: ButtonTheme(
minWidth: 50,
height: 30.0,
child:RaisedButton(
color: Theme.of(context).primaryColor,
shape: new RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(5.0)),
onPressed: () async {
print("pressed");
},
child: Text("Subscribe"),
)
),
)
],
)
now I want the button subscribe align right, what should I do to make it work? I am tried like this but failed:
padding: const EdgeInsets.only(top: 8,bottom: 8.0,right: 1),
I am tried to make the button with one pixel of the screen, but it seems not working.
If you want something like this:
You can set the mainAxisAlignment property of Row to spaceBetween
What you are trying to add is padding which only surrounds the widget, it will not align the widget to right. it will add padding to the right side of the Widget.
In Row() widget make mainAxisAlignment: MainAxisAlignment.spaceBetween. This will align the Subscribe button to right.
If you want space between the widgets of the row.
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
),
To Align the widgets completely on the right.
Row(
mainAxisAlignment: MainAxisAlignment.end,
),
To make the widgets take up the space evenly in the row.
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
),

How to design below card in Flutter

I need to design a card having an address whose size is unknown(as shown in bottom middle). The card need to grow according to the size of that address. The alignments should as shown in the image.
I tried Wrap, Flexible, ListView and some other ways but none of them worked.
You can fix the width in a container, and the height will be automatically adjusted based on the child size.
Center(
child: Card(
color: Colors.white,
child: Padding(
padding: const EdgeInsets.all(20.0),
child: Container(
width: 100,
child: Text('your text here...'),
),
),
),
),
You can add more widgets by using columns and rows, make sure that the mainAxisSize of your column is MainAxisSize.min. This will ensure that the column will try to minimize the height it takes..
Card(
color: Colors.white,
child: Padding(
padding: const EdgeInsets.all(20.0),
child: Container(
width: 100,
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
'Industry',
style: TextStyle(fontWeight: FontWeight.bold),
),
Text('your text here...'),
],
),
),
),
),

Dynamic height of listview builder item

I am unable to set the dynamic size to my list item of list view builder, every time it shows blank screen and when I specify a constant size it works.
I tried by using column by setting mainAxisSize=minimum and by using container as we know container wraps the child height but nothing works
listItem (GuideLines news) =>Column(
mainAxisAlignment: MainAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: <Widget>[Container(
decoration: BoxDecoration(image: new DecorationImage(image: AdvancedNetworkImage(
"${news.featured_image}",
useDiskCache: true,
cacheRule: CacheRule(maxAge: const Duration(days: 7)),
),fit: BoxFit.cover)),
margin: EdgeInsets.only(bottom: 10.0),
child: ListTile(
onTap: (){
print(news.web_link);
Navigator.push(context, MaterialPageRoute(builder: (context) => NewsDetailsPage(news)));
},
title: new Container(
margin: EdgeInsets.only(left: 30.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.max,
children: <Widget>[
Padding(
padding: const EdgeInsets.only(top: 20.0),
child: new Text("${DateFormat("E, d MMM y").format(CommonService.dateFormat(news.publish_date.toString()))}", style: TextStyle(fontFamily: 'SF-Display-Regular' ,fontSize: 13.0 ,color: Colors.white),),
),
SizedBox(height: 13.0),
new Flexible(
child: new Container( width: MediaQuery.of(context).size.width *0.45,
child: Padding(
padding: const EdgeInsets.only(bottom: 20.0),
child: new Text("${news.title}" ,maxLines: 3, overflow: TextOverflow.ellipsis, style: TextStyle(fontFamily: 'SF-Display-Semibold' ,fontSize: 22.0 ,color: Colors.white),),
)
),
)
],
)),
trailing: Padding(
padding: const EdgeInsets.only(right: 20),
child: Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[Icon(Icons.arrow_forward_ios, color: Colors.white),SizedBox(width: 8,)],
),
),
),
)],
);
Just add these two properties of shrinkWrap and physics to make the height of list dynamic
ListView.builder(
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(), //Optional
itemCount: size,
itemBuilder: (context, position) {}
),
The problem with your code is, that you used a Flexible widget in a Column. A Flexible widget expands to the remaining space of the Column or Row. However, this only works if you have restricted the size of the Column or Row widget. Because otherwise, the size of the element in the Column would expand to infinity as the remaining space is not restricted and therefore also infinity.
When using Flexible or Expanded widgets you always need to restrict their parent size, else you get this error:
RenderFlex children have non-zero flex but incoming height constraints
are unbounded. When a column is in a parent that does not provide a
finite height constraint, for example if it is in a vertical
scrollable, it will try to shrink-wrap its children along the vertical
axis. Setting a flex on a child (e.g. using Expanded) indicates that
the child is to expand to fill the remaining space in the vertical
direction.
The solution and some cleanup of your code:
Widget listItem(GuideLines news) {
return Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AdvancedNetworkImage(
"${news.featured_image}",
useDiskCache: true,
cacheRule: CacheRule(maxAge: const Duration(days: 7)),
),
fit: BoxFit.cover),
),
margin: EdgeInsets.only(bottom: 10.0),
child: ListTile(
onTap: () {
print(news.web_link);
Navigator.push(context, MaterialPageRoute(builder: (context) => NewsDetailsPage(news)));
},
title: Container(
margin: EdgeInsets.only(left: 30.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Padding(
padding: const EdgeInsets.only(top: 20.0, bottom: 13.0),
child: Text(
"${DateFormat("E, d MMM y").format(CommonService.dateFormat(news.publish_date.toString()))}",
style: TextStyle(fontFamily: 'SF-Display-Regular', fontSize: 13.0, color: Colors.white),
),
),
Container(
width: MediaQuery.of(context).size.width * 0.45,
padding: const EdgeInsets.only(bottom: 20.0),
child: new Text(
"${news.title}",
maxLines: 3,
overflow: TextOverflow.ellipsis,
style: TextStyle(fontFamily: 'SF-Display-Semibold', fontSize: 22.0, color: Colors.white),
),
)
],
),
),
trailing: Padding(
padding: const EdgeInsets.only(right: 28),
child: Icon(Icons.arrow_forward_ios, color: Colors.white),
),
),
);
}
In your specific case, this Flexible widget was redundant anyway.