flutter - 如何在具有圆形边界的容器内居中图标小部件
问题描述
我面临一个问题,最终其他人已经有了解决方案。
我想 Flutter Container 圆形框形状,大小 >=1 的边框和作为子项的 Icon 完美地居中于容器的中间。
然而,Flutter 做了各种奇怪的事情,搞砸了图标的中心位置。对齐中心和中心小部件都没有帮助。
我尝试使用 Stack 和 position 但我认为这不是这样做的方法,因为必须有一种直接的方法来让 Container 中的图标完全居中并强制 Flutter 不要更改图标的大小或添加填充或移位身边的孩子。
这是一个代码片段
if (widget.selected) {
return Container(
width: widget.size,
height: widget.size,
constraints: BoxConstraints.tightFor(width: widget.size, height: widget.size),
decoration: BoxDecoration(
shape: BoxShape.circle,
border: Border.all(color: Colors.blue, width: 5),
color: Colors.yellow,
),
alignment: Alignment.center,
child: Icon(Icons.check_circle, size: widget.size-5, color: Colors.lightGreen),
);
}
else
return Container(
width: widget.size,
height: widget.size,
constraints: BoxConstraints.tightFor(width: widget.size, height: widget.size),
decoration: BoxDecoration(
shape: BoxShape.circle,
border: Border.all(color: Colors.blue, width: 5),
color: Colors.transparent,
),
);
我还检查了小部件树,发现发生了我不想在那里发生的事情。
解决方案
使用Stack
代替 aColumn
并将 theIcon
和Container
.
Stack(
children: [
Align(
alignment: Alignment.center,
child: Container(
height: 100,
width: 100,
decoration: BoxDecoration(
shape: BoxShape.circle,
border: Border.all(color: Colors.blue, width: 5),
color: Colors.yellow,
),
),
),
Align(
alignment: Alignment.center,
child: Icon(Icons.check_circle,
size: 190, color: Colors.lightGreen)),
],
),
推荐阅读
- ruby-on-rails - Rails 注册表单不会创建
- flutter - SingleChildScrollView - 从图像移动到图像
- reactjs - 如何在 reactjs 中动态地将标签组转换为文本框
- python - discord.py 将变量添加到命令
- java - 使用 Java MongoClient 更新 MongoDB 中嵌入文档中的项目
- javascript - TippyJS 工具提示的位置很奇怪,但在滚动页面或调整窗口大小后正确显示
- javascript - SCRIPT1010:编译时 IE11 中的预期标识符
- powershell - Powershell 过滤器问题缺少结果
- sql - Bigquery iso8601 字符串到时间戳
- javascript - 在自定义 Cypress 命令中为每个循环中断