flutter - 在颤动中居中一列容器,crossAxis不起作用
问题描述
我试图将一列从底部垂直开始的容器居中,但是我不能让它们水平居中。
mainAxisAlignment 可以很好地将它们放在底部,但是 crossAxis 根本没有反应。
class Home extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
mainAxisAlignment: MainAxisAlignment.end,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
SizedBox(height: 25,),
Container(color: Colors.red, width: 150, height: 80,),
SizedBox(height: 10,),
Container(color: Colors.red, width: 150, height: 80,),
SizedBox(height: 10,),
Container(color: Colors.red, width: 150, height: 80,),
SizedBox(height: 10,),
Container(color: Colors.red, width: 150, height: 80,),
SizedBox(height: 10,),
Container(color: Colors.red, width: 150, height: 80,),
SizedBox(height: 10,),
Container(color: Colors.red, width: 150, height: 80,),
]
),
);
}
}
我也试过用 Align 包裹,但没有运气。
(我正在使用 SizedBox 来分隔它们,不知道这是否是最佳方法。)
解决方案
Column
用小部件包裹Center
。
class Home extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.end,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
SizedBox(
height: 25,
),
Container(
color: Colors.red,
width: 150,
height: 80,
),
SizedBox(
height: 10,
),
Container(
color: Colors.red,
width: 150,
height: 80,
),
SizedBox(
height: 10,
),
Container(
color: Colors.red,
width: 150,
height: 80,
),
SizedBox(
height: 10,
),
Container(
color: Colors.red,
width: 150,
height: 80,
),
SizedBox(
height: 10,
),
Container(
color: Colors.red,
width: 150,
height: 80,
),
SizedBox(
height: 10,
),
Container(
color: Colors.red,
width: 150,
height: 80,
),
],
),
),
);
}
}
推荐阅读
- php - PHP / Laravel - 有条件地显示刀片视图
- sql - 如何构建我的数据以在一张表中预聚合不同的天数/月数/年数?
- android - android studio - 我想改变圆形按钮的背景颜色
- sql - 如何删除值的第一个字母并根据第一个字母更改值
- java - Java spring - 从拦截的异常中获取原始请求对象
- sql - 索引如何影响插入
- sql-server - 如何转义列别名中的方括号?
- javascript - Kendo UI TreeView Checkbox 提交按钮
- python - 在python中根据条件索引添加子列表元素
- scala - 方法作为参数,枚举值要使其类型化