flutter开发实战-RichText富文本居中对齐

flutter开发实战-RichText富文本居中对齐

在开发过程中,经常会使用到RichText,当使用RichText时候,不同文本字体大小默认没有居中对齐。这里记录一下设置过程。

一、使用RichText

我这里使用RichText设置不同字体大小的文本

复制代码
Container(
            decoration: BoxDecoration(
              color: Colors.amber,
            ),
            alignment: Alignment.center,
            child: RichText(
              textAlign: TextAlign.center,
              text: TextSpan(children: [
                TextSpan(
                    text: 'Hello\s',
                    style: TextStyle(
                      fontSize: 30,
                    )),
                TextSpan(
                  text: 'Flutter',
                  style: TextStyle(fontSize: 50),
                ),
                TextSpan(
                    text: 'let\s go',
                    style: TextStyle(
                      fontSize: 30,
                    )),
              ]),
            ),
          ),

在设置后发现文本没有在竖直方向没有居中对齐。

下面需要使用到WidgetSpan

WidgetSpan是官方提供用于针对图文混排的非常便捷的实现方式。WidgetSpan可以接入任何你需要的Widget。

在WidgetSpan支持在文本中插入指定的Widget,可以提升富文本自定义效果。

复制代码
Text.rich(TextSpan(
      children: <InlineSpan>[
        TextSpan(text: 'Hello World!'),
        WidgetSpan(
            child: SizedBox(
          width: 100,
          height: 50,
          child: Card(
              color: Colors.red,
              child: Center(child: Text('flutter'))),
        )),
       
        TextSpan(text: '加油!'),
      ],
    )

二、RichText富文本居中对齐

RichText富文本居中对齐,我们可以将文本进行使用WidgetSpan进行嵌套。

复制代码
WidgetSpan buildCenteredTextSpan({required String text, required TextStyle style}) {
    return WidgetSpan(
      alignment: PlaceholderAlignment.middle,
      child: Text(text, style: style),
    );
  }

调整之后的代码如下

复制代码
Container(
            decoration: BoxDecoration(
              color: Colors.amber,
            ),
            alignment: Alignment.center,
            child: RichText(
              textAlign: TextAlign.center,
              text: TextSpan(children: [
                buildCenteredTextSpan(text: 'Hello\s', style: TextStyle(
                fontSize: 30,
              )),
                buildCenteredTextSpan(text: 'Flutter', style: TextStyle(
                  fontSize: 50,
                )),
                buildCenteredTextSpan(text: ' let\s go', style: TextStyle(
                  fontSize: 30,
                )),
                
              ]),
            ),
          ),

效果图如下

三、小结

flutter开发实战-RichText富文本居中对齐

学习记录,每天不停进步。

相关推荐
GitLqr13 小时前
Flutter 实战:使用 local_auth 实现生物识别(指纹/Face ID)
安全·flutter·全栈
大龄秃头程序员15 小时前
【Flutter 性能踩坑小记】相册选个图卡了
flutter
ljt27249606611 天前
Flutter笔记--get_it&injectable
flutter
恋猫de小郭1 天前
Flutter iOS 的深度优化 PR,搞笑的是贡献者被 Gemini 评审折磨
android·前端·flutter
GitLqr2 天前
Flutter + Unity 混合开发:用 unity_kit 实现高效的双向通信
flutter·unity3d·全栈
程序员老刘2 天前
Flutter版本选择指南:3.47压哨发布,9月大限只剩一周 | 2026年8月
flutter·ai编程·客户端
xcyxiner2 天前
flutter 运行到模拟器上
android·前端·flutter
末代iOS程序员华仔2 天前
iOS 语聊直播/聊天APP4.3条例被拒解决
flutter·ios·swift
恋猫de小郭2 天前
Dart 3.13 的到底改了什么?为什么很重要?有什么坑?
android·前端·flutter
xcyxiner3 天前
flutter wsl2安装记录(使用宿主机虚拟机)
前端·flutter