要将TextSpan的文本垂直居中,可以使用Baseline Widget将其包装起来。Baseline Widget可以将其子Widget的基线与给定的baseline对齐。

例如,以下代码将TextSpan的文本垂直居中:

Text.rich(
  TextSpan(
    text: 'Hello ',
    style: TextStyle(fontSize: 20),
    children: [
      TextSpan(
        text: 'world',
        style: TextStyle(fontSize: 30),
      ),
    ],
  ),
  textScaleFactor: 1.5,
).wrapWith(
  (context, child) => Baseline(
    baseline: 0,
    baselineType: TextBaseline.alphabetic,
    child: child,
  ),
);

在这个例子中,我们使用Text.rich创建了一个TextSpan,其中包含两个不同大小的文本片段。然后,我们使用wrapWith方法将其包装在一个Baseline Widget中,将基线设置为0,并将基线类型设置为TextBaseline.alphabetic。这将使文本垂直居中

flutter 设置TextSpan文字垂直居中

原文地址: https://www.cveoy.top/t/topic/d9xK 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录