Row mainAxisAlignment spaceBetween property not working - Flutter - 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)
],
),
)

Related

How can I set the width of a text based in the flexible row above in 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.

Center alignment of text with container on the side

I am creating a profile page and need some help with alignment. I need the "username" text to be centred vertically and then have a container hosting a circle next to it.
What I have:
What I need:
The difference might be subtle, but as you can see the "username" in #2, is vertical to the entire screen and the circle then proceeds it. However, in case #1, both the "username" and the circle are vertically centred. How can I fix my alignment to get case #2.
My Code:
Container _buildUsername() {
return Container(
margin: EdgeInsets.only(
top: 1.0,
),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
"username",
style: TextStyle(
color: AppTheme.define().hintColor,
fontFamily: 'RobotoMedium',
fontSize: 11.5,
),
),
Padding(
padding: const EdgeInsets.only(
top: 3.0,
left: 5.0,
),
child: Container(
width: 4.2,
height: 4.2,
decoration: new BoxDecoration(
color: AppTheme.define().primaryColor,
shape: BoxShape.circle,
),
),
),
],
),
);
}
Hope You're Doing Well!
The Easy Way Is To Center The Text Between Two Icons In A Row The First One In Invisible And The Second One Is Your Online Icon (Make Sure To Put The Size Of The First Icon As The Second One)
Something Like This
Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Icon(
Icons.settings_outlined,
color: Colors.black,// this is should be hidden icon
size: 24,
),
Text(
'username',
),
),
Icon(
Icons.settings_outlined,
color: Colors.yellow,
size: 24,
)
],
)

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 fix position child widget in row? flutter

I'm trying to make comment card.
There is one problem.
My avatar and button are not fixed when comment height is growing
I want avatar image and button's position fixed.
How can I make this ?
import 'package:flutter/material.dart';
import '../../size_config.dart';
class CommentItem extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Padding(
padding: EdgeInsets.symmetric(horizontal: 10, vertical: 10),
child: Row(
children: <Widget>[
CircleAvatar(
radius: 25,
child: ClipOval(
child: SizedBox(
height: 1000,
width: 1000,
child: Image.asset(
'assets/images/img2.jpg',
fit: BoxFit.cover,
),
),
),
),
Expanded(
child: Padding(
padding: EdgeInsets.symmetric(horizontal: 10),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text('123',
style:
TextStyle(fontWeight: FontWeight.bold, fontSize: 17)),
SizedBox(height: 3),
Text(
'. If you want to keep these changes, it is re',
style: TextStyle(color: Colors.grey),
maxLines: 4,
),
],
),
),
),
ClipOval(
child: Material(
color: Colors.blue, // button color
child: InkWell(
splashColor: Colors.red, // inkwell color
child: SizedBox(
width: SizeConfig.widthMultiplier * 8.8,
height: SizeConfig.heightMultiplier * 5,
child: Icon(
Icons.person_add,
size: SizeConfig.imageSizeMultiplier * 5.5,
color: Colors.white,
)),
onTap: () {},
),
),
)
],
),
);
}
}
it looks like your post is mostly code; please add some more details
it looks like your post is mostly code; please add some more details
it looks like your post is mostly code; please add some more details
To achieve this you can make use of Row's crossAxisAlignment property it will arrange the widget inside the row respectively
For Center -
crossAxisAlignment: CrossAxisAlignment.center
For Top -
crossAxisAlignment: CrossAxisAlignment.start,
For Bottom -
crossAxisAlignment: CrossAxisAlignment.end,
Did you try using the crossAxisAlignment?
Row(
crossAxisAlignment: CrossAxisAlignment.start,
You can do it using...
crossAxisAlignment: CrossAxisAlignment.start
For this, you can use the Alignment Property of Row
There is two alignment property in Row:
MainAxisAlignment
CrossAxisAlignment
MainAxisAlignment is used for Horizontal Alignment of Widgets and CrossAxisAlignment is used for Vertical Alignment.
In your case you need to set:
Row(
crossAxisAlignment: CrossAxisAlignment.start,

Auto expanding Container in flutter -- for all devices

I need a Container with some text in to auto expand. I have an API call, which can be anything from 5 words to 500 words. I don't want to just have 1 fixed size that's huge, but contains 10 words.
I have tried Expanded() and SizedBox.Expand(), but I might be using them wrong
Card(
elevation: defaultTargetPlatform ==
TargetPlatform.android ? 5.0 : 0.0,
child: Column(
children: <Widget>[
Container(
margin: const EdgeInsets.all(0.0),
padding: const EdgeInsets.all(2.0),
decoration: BoxDecoration(color: Colors.black),
width: _screenSize.width,
height: 250,
child: Column(
children: <Widget>[
Container(
color: Colors.black,
width: _screenSize.width,
height: 35,
child: Padding(
padding: const EdgeInsets.only(
left: 15, top: 11),
child: Text("Title".toUpperCase(),
style: TextStyle(
color: Colors.white
),
),
),
),
Container(
color: Colors.white,
width: _screenSize.width,
height: 210,
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Padding(
padding: const EdgeInsets.only(top: 8, bottom: 5),
child: Text("Title of expanding text", style: TextStyle(
fontSize: 25,
),
),
),
Text("Expanding text", style: TextStyle(
fontSize: 35,
fontWeight: FontWeight.w800
),),
],
),
),
],
),
),
],
),
),
I just need the Container to expand, but stay small/get bigger
Have you tried not specifying height at all? The Container should wrap according to the child in this case.
Otherwise, the widget has a child but no height, no width, no
constraints, and no alignment, and the Container passes the
constraints from the parent to the child and sizes itself to match the
child.
Above is an extract from the official flutter documentation for Container.
Here is the official flutter documentation link.
You can use FittedBox, this will resize your text according to available area.
You can use it like this :
FittedBox(child: Text('...............Your text...............'));
I would suggest you to use Constraints...this will set Container height according to the Text child's requirement. Please see the example...
Container(
constraints: BoxConstraints(
maxHeight: double.infinity,
),
child: Column(
children: [
Text(
'Hello flutter...i like flutter...i like google...',
softWrap: true,
style: TextStyle(
color: Colors.white, fontSize: 20 , ),
),],),)
we just neeed to add mainAxisSize: MainAxisSize.min, properties inside child Column or Row where the child is set to Container
for example
AnythingYourWidget(
child: Container(
child: Column( // For Example Column
mainAxisSize: MainAxisSize.min, // these properties following the children content height available.
children: [
// YourWidget
]
)
)
),
I too had a container with a text widget inside that would not scale as the text increased in character count. Make the widget tree Container -> IntrinsicWidth -> Text/TextField and it seems to play nicely for me.
IntrinsicWidth will scale the size of the container to its child.