只有一个字符的小容器无法正确居中 - Flutter

Small container with only one character no centering correctly - Flutter

只有一个字符的小容器没有正确居中,填充或不填充设置为 0。

如果我减小字体大小将呈现居中,但默认文本大小没有

Container(
  decoration: BoxDecoration(
    color: Colors.green,
    shape: BoxShape.circle,
  ),
  width: 20.0,
  height: 20.0,
  alignment: Alignment.center,
  padding: EdgeInsets.all(0.0),
  child: Text(
    '+',
  ),
)

更新: 用图标代替字母的行为完全相同。

如果我将大小从 20 更改为 40:

代码

Container(
            width: 40,
            height: 40,
            alignment: Alignment.center,
            decoration: BoxDecoration(
              shape: BoxShape.circle,
              color: Colors.grey,
            ),
            child: RotatedBox(
              quarterTurns: 3,
              child: Icon(
                Icons.expand_less,
                color: Colors.white,
              ),
            ),
          ),

更新 2:与 Fitted 一起使用图标:

Container(
            width: 20,
            height: 20,
            alignment: Alignment.center,
            decoration: BoxDecoration(
              shape: BoxShape.circle,
              color: Theme.of(context).textTheme.caption.color,
            ),
            child: FittedBox(
              child: RotatedBox(
                quarterTurns: 3,
                child: Icon(
                  Icons.expand_less,
                  color: Theme.of(context).primaryColor,
                ),
              ),
            ),
          ),

我无法重现这个。但我假设您的默认字体大小对于容器来说太大了。您可以尝试将文本包装在 FittedBox 中,这样它会适应容器:

return Container(
      decoration: BoxDecoration(
        color: Colors.green,
        shape: BoxShape.circle,
      ),
      width: 20.0,
      height: 20.0,
      alignment: Alignment.center,
      padding: EdgeInsets.all(0.0),
      child: FittedBox(
        child:Text('+')
     ),
);