多行文字与行高垂直居中对齐



我目前正在尝试让Text应用特定的行高,并将文本垂直居中对齐。由于TextStyle上没有垂直对齐的选项,我尝试使用StrutStyle,并将其引导到大约将其对齐到中心。但是,设置前导后,前导的高度和Text with TextStyle的高度不同。

演示可以在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,
),
),
  1. 想问一下是否有合适的方法将其居中对齐?文本是多行
  2. 如果引导方法是目前这种情况下的推荐方法,是否有任何计算可以使用引导同步正确的高度

Align小部件包装,它将帮助您在任何需要的地方对齐。

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,
),
),
),

最新更新