首页 > 解决方案 > 多行文本与行高垂直居中对齐

问题描述

我目前正在尝试让文本以特定的行高应用并将文本垂直居中对齐。由于 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,
      ),
    ),
  1. 想问是否有任何适当的方法将其对准中心?文本是多行的。
  2. 如果现在这种情况下推荐使用领先的方法,是否有任何计算可以使用领先同步正确的高度?

标签: flutterflutter-text

解决方案


Wrap With AlignWidget 这将帮助您在任何地方对齐。

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

推荐阅读