简体   繁体   English

Flutter 文本小部件未居中

[英]Flutter text widget not centered

I am trying to have text centered inside a container.我试图让文本在容器内居中。 No matter what I did, text is never centered.无论我做什么,文本都不会居中。 Here is my code:这是我的代码:

Container(
  alignment: Alignment.center,
  width: 47,
  height: 47,
  decoration: BoxDecoration(color: Colors.grey, shape: BoxShape.circle),
  child: Text(
    firstName == null && lastName == null
        ? 'UU'
        : '${firstName[0]}${lastName[0]}'.toUpperCase(),
    style: TextStyle(
      fontFamily: 'AvenirLTStd-Roman',
      fontSize: 18,
      color: Colors.white,
      fontWeight: FontWeight.w300,
    ),
    textAlign: TextAlign.center,
  ),
)

And this is how the screen is generated:这就是屏幕的生成方式:

在此处输入图像描述

If you notice text is not centered, the bottom part of the container seems to have more "free space" compared to the top.如果您注意到文本未居中,则与顶部相比,容器的底部似乎有更多的“可用空间”。 When I open this in widget inspector and highlight Text Widget I see this:当我在小部件检查器中打开它并突出显示文本小部件时,我看到了:

在此处输入图像描述

Does anyone know what am I missing here and why is text not centered?有谁知道我在这里缺少什么以及为什么文本不居中? I want text to be in the middle of the container and have equal space between top and bottom.我希望文本位于容器的中间,并且顶部和底部之间的间距相等。

UPDATE:更新:

Using Center widget does not help:使用中心小部件没有帮助:

在此处输入图像描述

Try to wrap your Container with Center.尝试用 Center 包裹你的容器。 Center -> Container中心 -> 容器

Wrap the text with Center().用 Center() 包裹文本。 This might be works.这可能是有效的。

Simply do this只需这样做

Container(
  alignment: Alignment.center,
  width: 47,
  height: 47,
  decoration: BoxDecoration(color: Colors.grey, shape: BoxShape.circle),
  child: Center(child: Text(
    firstName == null && lastName == null
        ? 'UU'
        : '${firstName[0]}${lastName[0]}'.toUpperCase(),
    style: TextStyle(
      fontFamily: 'AvenirLTStd-Roman',
      fontSize: 18,
      color: Colors.white,
      fontWeight: FontWeight.w300,
    ),
    textAlign: TextAlign.center,
  ),),
)

Try to尝试

Container(
      alignment: Alignment.center,
      width: 47,
      height: 47,
      decoration: BoxDecoration(color: Colors.grey, shape: BoxShape.circle),
      child: Center(child: Text(
        firstName == null && lastName == null
            ? 'UU'
            : '${firstName[0]}${lastName[0]}'.toUpperCase(),
          style: TextStyle(
          fontFamily: 'AvenirLTStd-Roman',
          fontSize: 18,
          color: Colors.white,
          fontWeight: FontWeight.w300,
        ),
        textAlign: TextAlign.center,
      ),),
    )

在此处输入图像描述

Try Like this像这样试试

Container(
  alignment: Alignment.center,
  height: 47,
  width: 47,
  decoration: BoxDecoration(color: Colors.grey, shape: BoxShape.circle),
  child: Text(
  'UU',
  style: Theme.of(context).textTheme.headline6,
),)

图片

Please remove or change fontFamily it will fix it.请删除或更改 fontFamily 它将修复它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM