![](/img/trans.png)
[英]Flutter : Why Text Widget on Flutter automatically centered when placed before TextField Widget ? ( Inside Column Widget )
[英]Flutter text widget not centered
我试图让文本在容器内居中。 无论我做什么,文本都不会居中。 这是我的代码:
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,
),
)
这就是屏幕的生成方式:
如果您注意到文本未居中,则与顶部相比,容器的底部似乎有更多的“可用空间”。 当我在小部件检查器中打开它并突出显示文本小部件时,我看到了:
有谁知道我在这里缺少什么以及为什么文本不居中? 我希望文本位于容器的中间,并且顶部和底部之间的间距相等。
更新:
使用中心小部件没有帮助:
尝试用 Center 包裹你的容器。 中心 -> 容器
用 Center() 包裹文本。 这可能是有效的。
只需这样做
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,
),),
)
尝试
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,
),),
)
请删除或更改 fontFamily 它将修复它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.