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富文本居中对齐

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

相关推荐
王二蛋与他的张大花13 小时前
后台内存 827MB,我一行业务逻辑都没改就砍到 100MB
android·flutter·性能优化
恋猫de小郭20 小时前
Flutter 的FFI/JNI 插件终于能区分不同的 FlutterEngine 了
android·前端·flutter
晚霞的不甘2 天前
Flutter/OpenHarmony 三方库适配实战:google_maps_flutter 地图
flutter·macos·华为·cocoa·harmonyos·1024程序员节
恋猫de小郭2 天前
Flutter HC 旋转 ANR,而且还和之前的 HCPP 问题类似
android·前端·flutter
恋猫de小郭3 天前
Flutter Perfetto:Dart Heap Sampling 终于要接入 Android 原生内存分析了
android·前端·flutter
程序员老刘3 天前
Flutter版本选择指南:尽早升级3.47.9 | 2026年11月
flutter·ai编程·客户端
恋猫de小郭4 天前
Android Keep Rules 解读,直接影响 Google Play 上架和曝光
android·前端·flutter
恋猫de小郭5 天前
Flutter A2UI 1.0 ,原来的 genui 也正式换成了这一套 SDK 了
android·前端·flutter
LinXunFeng5 天前
A2UI 1.0 详解:让 AI 生成 Flutter 界面,还不怕它乱写
前端·flutter·agent
好的佩奇6 天前
Dart 之元数据与注解
android·flutter·dart