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),
                ),
              ],
            ),
          ),
相关推荐
太子釢14 小时前
Flutter 与 Native 混合开发策略调研与方案对比
flutter
西西学代码14 小时前
Flutter---Stack
flutter
ZZZMMM.zip19 小时前
基于鸿蒙PC与鸿蒙Flutter框架构建AI本地生活服务平台
人工智能·flutter·华为·harmonyos·鸿蒙
木叶丸20 小时前
AI 编程概念与 Harness 工程实践入门
人工智能·flutter·ios
程序员老刘1 天前
腾讯混元Hy3在Flutter项目中的实战体验
flutter·ai编程
ailinghao2 天前
flutter常用的内置特性
flutter
子非鱼94272 天前
10-Flutter调用鸿蒙原生能力前置课:MethodChannel和PlatformView准备
学习·flutter·华为·harmonyos
xd1855785552 天前
鸿蒙PC时代的AI快递查询应用:基于鸿蒙Flutter框架的跨端创新实践
人工智能·flutter·华为·harmonyos·鸿蒙
ZZZMMM.zip2 天前
基于鸿蒙PC与鸿蒙Flutter框架的AI紧急求助应用开发实践
人工智能·flutter·华为·harmonyos·鸿蒙
ZZZMMM.zip2 天前
鸿蒙生态新征程:基于鸿蒙Flutter框架的AI菜谱营养应用深度解析
人工智能·flutter·华为·harmonyos·鸿蒙·鸿蒙系统