当前位置: 首页 > news >正文

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

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

http://www.lryc.cn/news/377937.html

相关文章:

  • 智慧消防新篇章:可视化数据分析平台引领未来
  • u8g2 使用IIC驱动uc1617 lcd有时候某些像素显示不正确
  • 使用opencv合并两个图像
  • k8s学习笔记(一)
  • 自学前端——JavaScript篇
  • 高考毕业季--浅谈自己感想
  • 遥感图像地物覆盖分类,数据集制作-分类模型对比-分类保姆级教程
  • 【Android面试八股文】Kotlin内置标准函数let的原理是什么?
  • 网工面试总结1
  • [stm32]密码锁
  • 优化yarn在任务执行时核数把控不准确的问题
  • 2024年,收付通申请开通流程
  • Django使用django-apscheduler实现定时任务
  • python数据分析:修改数据
  • 【免费API推荐】:解锁无限创意,让您的应用更具竞争力(8)
  • 日语 11 12
  • STM32程序启动过程
  • 天才简史——Diederik P. Kingma与他的Adam优化器
  • 两个src案例分享
  • 宕机了, redis如何保证数据不丢?
  • 太速科技-基于XCVU9P+ C6678的100G光纤的加速卡
  • Maven:一个下载jar依赖失败的问题解决方案
  • Java 枚举的基本使用
  • Semaphroe + CountDown
  • 解读代理 IP差异:ISP 代理与住宅代理
  • maven-jar-plugin maven打包插件笔记
  • 云服务器部署Neo4j
  • day64 图论 图论理论基础 深搜 广搜 98. 所有可达路径
  • 从0进入微服务需要了解的基础知识
  • MySQL之复制(七)