[英]How to align three items, two on the sides and one on the center in Flutter?
Obviously, if two items only, MainAxisAlignment.space Between
would be enough, but I have three items, so I have tried with Align
:显然,如果只有两个项目, MainAxisAlignment.space Between
就足够了,但我有三个项目,所以我尝试了Align
:
Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Align(
alignment: Alignment.topLeft,
child: Text('Left',
style: TextStyle(
fontSize: 8.0,
fontWeight: FontWeight.bold,
),),
),
Align(
alignment:Alignment.center,
child: Text('Center',
style: TextStyle(
fontSize: 9.0,
fontWeight: FontWeight.bold),
),
),
Align(
alignment: Alignment.topRight,
child: Text('Right',
style: TextStyle(
fontSize: 9.0,
fontWeight: FontWeight.bold,
)),
),
],
),
But contents are still aligned to center in general, I also tried using Spacer()
before and after the centered element, the result was much like spaceEvenly
but with spaces in between.但是内容通常仍然与中心对齐,我也尝试在居中元素之前和之后使用Spacer()
,结果很像spaceEvenly
,但中间有空格。
What I want is the centered element to be fixed in the center no matter what other elements contentes, the left element to be fixed on the left, the right element on the right.我想要的是无论其他元素内容如何,居中元素都固定在中心,左侧元素固定在左侧,右侧元素固定在右侧。
EDIT编辑
space Between
is not working for sure, see doc : space Between
肯定不起作用,请参阅文档:
MainAxisAlignment.spaceBetween: Divides the extra space evenly between children. MainAxisAlignment.spaceBetween:在孩子之间平均分配额外的空间。
This does not garantie that the centered element go to center.这并不保证居中元素 go 居中。
Like @Yunus Emre Çelik said, you dont need to add Align, spaceBetween is already doing that.正如@Yunus Emre Çelik 所说,您不需要添加 Align,spaceBetween 已经在这样做了。 But, if your problem still persists, maybe you have a Column
as parent of your Row
that need crossAxisAlignment: CrossAxisAlignment.stretch,
.但是,如果您的问题仍然存在,也许您有一个Column
作为需要crossAxisAlignment: CrossAxisAlignment.stretch,
的Row
的父级。 Or if you dont, check if your Row
can expand all width space available.或者,如果您不这样做,请检查您的Row
是否可以扩展所有可用的宽度空间。
Anyway you can set your Row like that:无论如何你可以像这样设置你的行:
Row(
children: [
Expanded(...),
Expanded(...),
Expanded(...),
])
This force to set equals space to each child这种力量设置等于每个孩子的空间
But if you dont want to set equals space for all, can do that:但是如果你不想为所有人设置相等的空间,可以这样做:
Row(
mainAxisAlignment: MainAxisAlignment.center, // <- center items
children: [
Flexible( // <-- flexible space
child: Container(
alignment: Alignment.centerLeft,
color: Colors.red,
child: Text('left left left left left left left left left left left '),
),
),
Container( // <-- center
alignment: Alignment.center,
color: Colors.blue,
child: Text('center'),
),
Flexible( // <-- flexible space
child: Container(
alignment: Alignment.centerRight,
color: Colors.green,
child: Text('right'),
),
),
],
),
Try below code, remove Align Widget refer Flutter Layout here试试下面的代码,删除 Align Widget 参考 Flutter Layout here
Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
'Left',
style: TextStyle(
fontSize: 8.0,
fontWeight: FontWeight.bold,
),
),
Text(
'Center',
style: TextStyle(fontSize: 9.0, fontWeight: FontWeight.bold),
),
Text(
'Right',
style: TextStyle(
fontSize: 9.0,
fontWeight: FontWeight.bold,
),
),
],
),
Wrap your Text widget inside Expanded
or Flexible
Widget将您的文本小部件包裹在Expanded
或Flexible
的小部件中
Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Expanded(
child: Text(
'Leftvsdvjwhgehdxuguhe duyxegfygwedue dw',
style: TextStyle(
fontSize: 8.0,
fontWeight: FontWeight.bold,
),
),
),
Expanded(
child: Text(
'Center',
style: TextStyle(fontSize: 9.0, fontWeight: FontWeight.bold),
),
),
Expanded(
child: Text(
'Right',
style: TextStyle(
fontSize: 9.0,
fontWeight: FontWeight.bold,
),
),
),
],
),
Finally, to get rid of the equal space caused by Expanded
, I removed Expanded
from the centered element, then set the fit
properly to loose
for the left and right elements.最后,为了摆脱由Expanded
引起的相等空间,我从居中的元素中删除了Expanded
,然后将左右元素的拟fit
当地设置为loose
。
To align sides, I surrounded the left and right elements with 'Row', to set 'MainAxisAlignment' to start
and end
respectively, now it works:为了对齐边,我用“Row”包围了左右元素,分别将“MainAxisAlignment”设置为start
和end
,现在它可以工作了:
Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Expanded(
fit: FlexFit.loose,//Added this
child: Row(
//Surround the Left element with Row aligned to start
mainAxisAlignment: MainAxisAlignment.start,
children: [
Text(
'Left',
style: TextStyle(
fontSize: 8.0,
fontWeight: FontWeight.bold,
),
),
]),
),
//No expanded needed to the centered child
Text( 'Center',style: TextStyle(
fontSize: 9.0,
fontWeight: FontWeight.bold,
),),
Expanded(
fit: FlexFit.loose,//Added this too
child: Row( //Surround the right element with Row aligned to end
mainAxisAlignment: MainAxisAlignment.end,
children: [
Text(
'Right',
style: TextStyle(
fontSize: 9.0,
fontWeight: FontWeight.bold,
),
),
]),
),
],
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.