Flutter---RichText(混合文本样式)

RichText 是 Flutter 中用于显示富文本 的组件,可以在同一段文字中设置多种样式(不同颜色、大小、字体、加粗、图标等)。

效果图

1.不同格式的文本

Dart 复制代码
//1.不同格式的文本
          RichText(
              text: TextSpan( //文本片段
                  text: "基础文本",
                  style: TextStyle(fontSize: 14,color: Colors.black),
                  children: [
                    TextSpan(
                      text: '特殊文本',
                      style: TextStyle(fontSize: 20,color: Colors.red,fontWeight: FontWeight.bold),
                      recognizer: TapGestureRecognizer()//点击事件
                        ..onTap = (){
                          print("用户点击了特殊文本");
                        }
                    ),
                    TextSpan(
                      text: "普通文本",
                      style: TextStyle(fontSize: 14,color: Colors.grey),
                    )
                  ]
              )
          ),

2.图片嵌入文本

Dart 复制代码
//2.图片嵌入文本
          SizedBox(height: 10,),
          RichText(
            text: TextSpan(
              children: [
                WidgetSpan( //嵌入组件
                  child: Icon(Icons.favorite, color: Colors.red, size: 16),
                  alignment: PlaceholderAlignment.middle, //对齐方式
                ),
                TextSpan(
                  text: '我爱中华人民共和国,我爱中华人民共和国,我爱中华人民共和国,我爱中华人民共和国,我爱中华人民共和国,',
                  style: TextStyle(color: Colors.black, fontSize: 14),
                ),
              ],
            ),

            softWrap: true,//允许换行,这个不写也是默认可以换行的
            maxLines: 2,//最多几行
            overflow: TextOverflow.ellipsis,//超出显示省略号
          ),

3.带删除线的文本

Dart 复制代码
//3.带删除线
          RichText(
            text: TextSpan(
              children: [
                TextSpan(
                  text: '原价¥199',
                  style: TextStyle(
                    color: Colors.grey,
                    decoration: TextDecoration.lineThrough,//删除线,decoration: underline-下划线
                    fontSize: 14,
                  ),
                ),
                TextSpan(text: '  '),
                TextSpan(
                  text: '现价¥99',
                  style: TextStyle(
                    color: Colors.red,
                    fontWeight: FontWeight.bold,
                    fontSize: 18,
                  ),
                ),
              ],
            ),
          ),

4.文字阴影效果

Dart 复制代码
//4.文字阴影效果
          RichText(
            text: TextSpan(
              children: [
                TextSpan(
                  text: '霓虹灯',
                  style: TextStyle(
                    fontSize: 32,
                    fontWeight: FontWeight.bold,
                    color: Colors.cyan,
                    shadows: [
                      Shadow(
                        offset: Offset(0, 0),
                        blurRadius: 10,
                        color: Colors.cyan,
                      ),
                      Shadow(
                        offset: Offset(0, 0),
                        blurRadius: 20,
                        color: Colors.blue,
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),

5.带背景色的文本

Dart 复制代码
//5.带背景色的文本
          RichText(
            text: TextSpan(
              children: [
                TextSpan(
                  text: '高亮显示',
                  style: TextStyle(
                    backgroundColor: Colors.yellow,
                    fontSize: 18,
                  ),
                ),
                TextSpan(
                  text: ' 普通文本',
                  style: TextStyle(fontSize: 18),
                ),
              ],
            ),
          ),
相关推荐
恋猫de小郭2 天前
Android 限制侧载新进展,谷歌联合国内厂商推验证计划
android·前端·flutter
恋猫de小郭2 天前
解读 Android 17 全新内存限制,有没有“豁免”后门?
android·前端·flutter
程序员老刘5 天前
跨平台开发地图 | 2026年6月
flutter·ai编程·客户端
悟空瞎说6 天前
Flutter 架构详解:新手必懂底层原理
flutter
SoaringHeart6 天前
Flutter最佳实践:IM聊天文字链接自动识别跳转
前端·flutter
恋猫de小郭6 天前
KMP / CMP 鸿蒙版本 Beta 发布,他有什么特别之处?
android·前端·flutter
风华圆舞7 天前
Flutter + 鸿蒙 Intents Kit:页面直达能力的完整接入方案
flutter·ui·华为·harmonyos
韩曙亮7 天前
【Flutter】Flutter 组件 ④ ( 组件渲染 的 三棵树理论 | Widget 树 → Element 树 → RenderObject 树 )
flutter·element·widget·renderobject
恋猫de小郭7 天前
Android 17 正式版发布,全新 AI 和各种破坏性更新
android·前端·flutter
kingbal7 天前
Windows:flutter环境搭建
windows·flutter