问题描述
我目前正在尝试让文本以特定的行高应用并将文本垂直居中对齐。由于 TextStyle 上没有垂直对齐的选项,我尝试使用 StrutStyle 并导致 大约 将其与中心对齐。但是设置了行距后,行距和带有TextStyle高度的Text的高度是不一样的。
演示可以在dartpad查看。
Text(
'label A',style: const TextStyle(
fontSize: 16.0,color: Colors.red,backgroundColor: Colors.blue,),strutStyle: StrutStyle(
fontSize: 16.0,leading: (70.0 / 16.0)/2,forceStrutHeight: true,
解决方法
Wrap With Align
Widget 这将帮助您随心所欲地对齐。
Align (
alignment : Alignment.topCenter;
child : Text(
'label A',style: const TextStyle(
fontSize: 16.0,color: Colors.red,backgroundColor: Colors.blue,),strutStyle: StrutStyle(
fontSize: 16.0,leading: (70.0 / 16.0)/2,forceStrutHeight: true,