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),
                ),
              ],
            ),
          ),
相关推荐
嵩风抚4 小时前
一款HK银行APP的业务+技术
android·flutter·react native·html5
不羁的木木6 小时前
给鸿蒙 App 增加广播收发能力 —— flutter_broadcasts 的鸿蒙使用指南
flutter·harmonyos
不羁的木木6 小时前
给鸿蒙 App 增加打开外部网页能力 —— flutter_web_browser 的鸿蒙使用指南
前端·flutter·harmonyos
安好说AI7 小时前
Flutter 三方库 sound_mode 的鸿蒙化适配指南:免权限读取与受限写入的契约对齐
flutter·harmonyos·鸿蒙
●VON8 小时前
Flutter 鸿蒙 disk_space_2 1.0.13 使用实战:下载前检查磁盘空间
flutter·华为·harmonyos·鸿蒙
●VON17 小时前
Flutter 鸿蒙插件适配实战:用 device_screen_brightness 2.0.0 控制并监听屏幕亮度
flutter·华为·harmonyos
风早爽太1 天前
Flutter 开发笔记:share_plus 分享插件
笔记·flutter
●VON1 天前
Flutter 鸿蒙插件适配实战:用 boot_time_plugin 1.0.0 读取启动时间与运行时长
flutter·华为·harmonyos
不羁的木木1 天前
给鸿蒙 App 增加唤起系统邮件发送能力 —— flutter_email_sender 的鸿蒙使用指南
flutter·华为·harmonyos
●VON1 天前
Flutter 鸿蒙插件适配实战:用 accurate_storage_info 0.1.1 查询总量、可用量与已用量
flutter·华为·harmonyos