Flutter---Text

概念

Text 是 Flutter 中最基础的文本渲染组件,它不仅仅显示文字,而是一个完整的文本排版引擎,支持样式、溢出处理、富文本、文本选择等高级功能。

源码层级
Dart 复制代码
// Text 的继承链
Text → StatelessWidget
  ↓
RichText (LeafRenderObjectWidget)
  ↓
RenderParagraph (RenderObject)
  ↓
dart:ui Paragraph (Skia 引擎)


理解

//Text 是 RichText 的包装器(语法糖)

//RichText 才是真正的文本渲染组件

//最终由 Skia 引擎(C++)绘制
属性
Dart 复制代码
Text(
 "Hello Flutter Hello Flutter Hello Flutter Hello Flutter Hello Flutter  Hello Flutter Hello Flutter", //文本
  style: TextStyle(
    // 字体相关
    fontFamily: 'PingFang SC',        // 字体家族
    fontSize: 20,                      // 字号
    fontWeight: FontWeight.bold,       // 粗细
    fontStyle: FontStyle.italic,       // 斜体
    letterSpacing: 1.5,               // 字间距
    wordSpacing: 2.0,                 // 词间距
    
    // 颜色相关
    color: Colors.blue,               // 文字颜色
    backgroundColor: Colors.yellow,   // 背景色
    
    //划线:underline下划线/lineThrough删除线/overline上划线// 下划线
    decoration: TextDecoration.overline,
    decorationColor: Colors.red,           // 划线颜色
    decorationStyle: TextDecorationStyle.dashed, // 虚线
    
    // 阴影
    shadows: [
      Shadow(
        color: Colors.black26,
        blurRadius: 4,
        offset: Offset(2, 2),
      ),
    ],
    
    // 其他
    height: 1.5,                      // 行高(倍数)
    overflow: TextOverflow.ellipsis,  // 溢出处理(style 里设置无效,要在 Text 里设置)
    inherit: true,                    // 是否继承父级样式
    
  ),

    softWrap: true,//开启自动换行
    maxLines: 2, //最大行
    //超出显示省略号,fade:超出部分淡出;clip:超出部分直接裁剪
    overflow: TextOverflow.ellipsis,
    textAlign: TextAlign.center, // 文本居中对齐,
    //textAlign: TextAlign.left,    // 左对齐(默认)
    //textAlign: TextAlign.right,   // 右对齐
    //textAlign: TextAlign.center,  // 居中对齐
    //textAlign: TextAlign.justify, // 两端对齐(英文效果明显)
    //textAlign: TextAlign.start,   // 根据 TextDirection 决定左/右

)
富文本

Text.rich和TextSpan

Dart 复制代码
// 一段文字多种样式
        Text.rich(
          TextSpan(
            children: [
              TextSpan(
                text: '这是 ',
                style: TextStyle(color: Colors.black),
              ),
              TextSpan(
                text: '红色',
                style: TextStyle(
                  color: Colors.red,
                  fontWeight: FontWeight.bold,
                ),
              ),
              TextSpan(
                text: ' 和 ',
                style: TextStyle(color: Colors.black),
              ),
              TextSpan(
                text: '蓝色',
                style: TextStyle(
                  color: Colors.blue,
                  fontSize: 20,
                ),
              ),
            ],
          ),
        )
相关推荐
君赏2 天前
棉宇宙 Flutter 热更新是如何落地的
flutter
恋猫de小郭2 天前
AndroidX 新增 Security State ,支持可编程的系统安全状态查询
android·前端·flutter
zihan5182 天前
自己做软件日记9/21 得到了一个良好的文件结构框架
后端·flutter·前端框架·android studio
ZFJ_张福杰3 天前
【Flutter】Flutter 中有哪些耗时操作?从 UI 卡顿到 Isolate 性能优化
flutter·性能优化·卡顿·isolate
sakiko_3 天前
Flutter:Dart学习笔记1:基础UI组件等
flutter
9765033353 天前
iOS 上架 4.3a 被拒【uniapp专讲】
flutter·ios·objective-c·uniapp·swift
传奇开心果编程4 天前
【Flutter入门练中学】第2课:布局系统
学习·flutter·ui
君赏4 天前
FlutterPatch:把 Shorebird 热更新的控制面搬回自己服务器
flutter
用户2181697049304 天前
Flutter tootip 轻量级提示
flutter
用户2181697049304 天前
Flutter 拖拽 Draggable DragTarget
flutter