Arrange Elements In Row At Start And Center - flutter

I have a dilemma that I can't seem to solve. I have an existing row in Flutter. Essentially what I want to accomplish is to have the first element(yellow) at the start of the row, and the second element(red) in the center of the element. I know Row has the mainAxisAlignment, but I need a different alignment for each each element (i.e. both MainAxisAlignment.start & MainAxisAlignment.center). Thanks for any help!
The closest I have been able to come to this is this:
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
yellowElement,
Expanded(child: redElement),
],
);
But this is not perfectly centered, it is centered in the available space left over after yellow has taken its space.

I had this problem few weeks ago and the only solution I found was Stack... like this
Stack(
children: <Widget>[
Container(
color: Colors.grey,
width: 250,
height: 50,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
Container(width: 50, color: Colors.red),
],
),
),
Positioned(
top: 0,
bottom: 0,
child: Container(
width: 20,
color: Colors.yellow,
),
)
],
)

Related

Flutter column with elements aligning to different sides without stretching the width of the column

I'd like to have a column take the width of the content, and have some elements align to right and some to the left.
Is this possible?
If I use Align widget on the children the whole column stretches in width. Is there another way?
Thank you
Edit: Example
Center(
child: Container(
color: Colors.yellow,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
color: Colors.red,
width: 50,
height: 50,
),
Text('Here goes some text with variable length'),
Container(
color: Colors.blue,
width: 50,
height: 50,
),
],
),
),
);
I want the column above with the red square to the left and the blue to the right (and the column not stretching to the available width).
Try this-
Center(
child: Container(
color: Colors.yellow,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Container(
color: Colors.red,
width: 50,
height: 50,
),
],
),
SizedBox(width: 100),
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Container(
color: Colors.blue,
width: 50,
height: 50,
),
],
),
],
),
),
)
You can simply try to put the contents of the column in a row, so you would have elements inside containers for their width and height as you like.
Column -> Row -> aligned containers.

flutter: Align / pin widgets to bottom of row

How is it possible to align all items to the bottom of a Row in flutter? I have got one widget in the Row which changes its height at runtime and due to that, all other widgets of the Row automatically center themselves, too. My question is: how do you prevent these other widgets from moving and pin them to the bottom?
What you're looking for is CrossAxisAlignment:
Row(
crossAxisAlignment: CrossAxisAlignment.end,
children: <Widget>[
Container(width: 100, height: 200, color: Colors.red),
Container(width: 100, height: 300, color: Colors.blue),
SizedBox(
width: 100,
height: 150,
child: FittedBox(
fit: BoxFit.contain,
child: const FlutterLogo(),
),
),
],
)
Row(
crossAxisAlignment: CrossAxisAlignment.end,
)
I hope you succeeded

In a Flutter Row, is there a way to center-align a child when using MainAxisAlignment.spaceBetween?

I have a Row filling a Container which may have between one and three fixed-sized children. I'm using MainAxisAlignment.spaceBetween, and when there's more than one child, it works as expected: with two children, they end up at the edges, and if there are three, two end up at the edges and one in the center.
Container(
width: 200,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
SizedBox(width: 30, height: 20),
SizedBox(width: 30, height: 20),
SizedBox(width: 30, height: 20),
],
),
);
However, when there's only one child, it's placed at the start of the Row, and I'd like to have it at the center. I can certainly achieve what I need by checking the number of children beforehand and changing the MainAxisAlignment to center, but I was wondering if there's a more elegant way to combine spaceBetween and center.
add crossAxisAlignment: CrossAxisAlignment.center, property to Raw Widget:
the code :
Container(
width: 200,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
SizedBox(width: 30, height: 20),
SizedBox(width: 30, height: 20),
SizedBox(width: 30, height: 20),
],
),
);
You can use MainAxisAlignment.spaceEvenly (or spaceAround) to center the single child. To make the two children float left and right (because in spaceEvenly there will be extra spaces in left and right), you can add Spacer to the children of the Row (works with any number of children).
Code:
List<Widget> rowChildren = [
SizedBox(width: 30, height: 20, child: Container(color: Colors.red)),
SizedBox(width: 30, height: 20, child: Container(color: Colors.yellow)),
SizedBox(width: 30, height: 20, child: Container(color: Colors.green)),
];
#override
void initState() {
super.initState();
int l = rowChildren.length;
for (int i = 1; i < 2 * l - 1; i += 2) {
rowChildren.insert(i, Spacer()); // adding Spacer between the two children
}
}
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: rowChildren
),
Result (a single child):
Result (two children):
Result (three children):

Flutter/Dart row is not centering within the remaining space

I am trying to center a group of objects within a row using Flutter/Dart. I have tried various methods and all have failed. I want the icon to be to the left of the screen and the remaining variables (trips, followers, and following) to be centered in the remaining space. I have tried it a variety of ways using the MainAxisAlignment and CrossAxisAlignment classes based on some solutions I found on SO but none have worked. What am I doing wrong?
Column(
children: <Widget>[
ClipOval(
child: Material(
color: Colors.grey, // button color
child: InkWell(
child: SizedBox(
width: 80,
height: 80,
child: Align(
alignment: Alignment.center,
child: Icon(
Icons.person,
size: 65.0,
),
),
),
),
),
)
],
),
Column(
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
trips, followers, following,
],
),
],
),
Image 1: How it currently looks
Image 2: Flutter Inspector View
You want to evenly distribute the space right from the icon between the three texts, right?
I would solve this by wrapping the 3 widgets in another row for the rest of the space without the icon and then wrapp the Text in an expanded widget that fills the rest of the space.
i assume that trips, followers and following are columns.
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Column(
children: <Widget>[
ClipOval(
....(your stuff)
)
]
),
Row(
children: <Widget>[
Expanded(
child: trips
),
Expanded(
child: followers
),
Expanded(
child: following
),
]
)
]
) //Row End
In the Row documentation you can see how the expanded takes a third of the available space. [1]

Column's MainAxisAlignment has no effect when nested in Column->Row

I'm trying to get working the MainAxisAlignment property on the Column, but it's not working quite expected. Here is my code.
Scaffold(
appBar: AppBar(
title: Text('Account'),
),
body: Container(
child: Column(
children: <Widget>[
Card(
margin: EdgeInsets.only(left: 0.0, right: 0.0, top: 0.0, bottom: 20.0),
child: InkWell(
onTap: () {},
child: Padding(
padding: EdgeInsets.all(8.0),
child: Row(
children: <Widget>[
Image.network(
'https://cdn.mydomain.com/Color/PNG/512/profile-512.png',
height: 72.0,
),
SizedBox(width: 8.0),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.spaceAround, // <<<=== this line
children: <Widget>[
Text('User\'s display name'),
Text('user#company.com'),
],
)
],
),
),
),
)
],
),
),
)
My hierarchy is Container->Column->Card->InkWell->Row->Column. The innermost Column widget has a problem. I figured out that If I remove the first Column widget then the MainAxisAlignment just works fine.
But I can't do that as I want to build a Column layout with various Cards inside it, as you can see in the code.
Could somebody please let me know what is this weird behaviour and a solution around this?
Regards.
Try to wrap this column in a Container or SizedBox and set a double.infinity height for it.
The problem that's happening it's because a Column or any component which you don't set a height or width, will automatically adjust to the widgets inside of it.
Container(
height: MediaQuery.of(context).size.height, //Add here
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
Text('User\'s display name'),
Text('user#company.com'),
],
),
),
OBS: I personally like to use MediaQuery for this. eg: MediaQuery.of(context).size.height to get the full height of the device, then, you can manipulate it, dividing by 2, 3, subtracting...