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 天前
跨平台开发地图 | 2026年10月
flutter·ai编程·客户端
林瞅瞅2 天前
Flutter 开发环境从零搭建与调试记录
flutter
FungLeo2 天前
成为全栈·Flutter App 篇·Markdown 编辑器:编辑、预览与离开保护
flutter·移动开发·markdown编辑器·表单设计·成为全栈·草稿保存
FungLeo2 天前
成为全栈·Flutter App 篇·会员中心:资料、密码、通知与私有数据缓存
flutter·dart·身份认证·数据隔离·成为全栈·会员中心
穆梓兰煊3 天前
Flutter端云协同推理架构解析
flutter·github
FungLeo3 天前
成为全栈·Flutter App 篇·内置 WebView:网页历史、App 返回与外链安全
flutter·网络安全·移动开发·webview·路由管理·成为全栈
m0_738185823 天前
Flutter 鸿蒙化实战:volume_controller 适配 OpenHarmony,系统音量控制
flutter·华为·harmonyos·鸿蒙
FungLeo3 天前
成为全栈·Flutter App 篇·Flutter Markdown 阅读器:支持范围与渲染边界
flutter·markdown·dart·代码高亮·成为全栈·移动阅读
m0_738185823 天前
Flutter 鸿蒙化实战:webview_cookie_manager 适配 OpenHarmony,WebView Cookie 管理
flutter·华为·harmonyos·鸿蒙