Flutter框架跨平台鸿蒙开发——Text组件基础使用

一、Text组件简介

Text是Flutter中最基础、最常用的组件,用于显示文本内容。了解Text组件是掌握Flutter UI开发的第一步。

Text组件的基本结构

Text组件
data属性
style属性
布局属性
文本字符串
TextStyle对象
textAlign
maxLines
overflow

属性 说明 必需 默认值
data 要显示的文本字符串 是 无
style 文本样式 否 默认样式
textAlign 文本对齐方式 否 TextAlign.start
maxLines 最大行数 否 无限制
overflow 溢出处理方式 否 TextOverflow.clip

二、创建简单文本

最基本的Text组件

dart 复制代码
Text('Hello, Flutter!')

添加样式的Text

dart 复制代码
Text(
  'Hello, Flutter!',
  style: TextStyle(
    fontSize: 24,
    fontWeight: FontWeight.bold,
    color: Colors.blue,
  ),
)

带颜色和背景的Text

dart 复制代码
Container(
  padding: EdgeInsets.all(16),
  color: Colors.yellow.shade100,
  child: Text(
    '带背景的文本',
    style: TextStyle(
      fontSize: 20,
      color: Colors.blue.shade800,
    ),
  ),
)

三、文本样式属性

TextStyle核心属性

TextStyle
字体
fontSize
fontWeight
fontFamily
颜色
color
backgroundColor
decorationColor
装饰
decoration
decorationStyle
间距
letterSpacing
wordSpacing

常用样式示例

dart 复制代码
// 大标题
Text(
  '大标题文本',
  style: TextStyle(
    fontSize: 32,
    fontWeight: FontWeight.bold,
    color: Colors.black87,
  ),
)

// 小标题
Text(
  '小标题文本',
  style: TextStyle(
    fontSize: 20,
    fontWeight: FontWeight.w600,
    color: Colors.black54,
  ),
)

// 正文
Text(
  '这是一段正文文本,使用默认字体大小和颜色。',
  style: TextStyle(
    fontSize: 14,
    color: Colors.black87,
  ),
)

// 辅助文本
Text(
  '辅助说明文本',
  style: TextStyle(
    fontSize: 12,
    color: Colors.grey.shade600,
  ),
)

四、文本装饰

下划线、删除线等装饰

dart 复制代码
Column(
  children: [
    // 下划线
    Text(
      '带下划线的文本',
      style: TextStyle(
        decoration: TextDecoration.underline,
        decorationColor: Colors.blue,
      ),
    ),

    // 删除线
    Text(
      '带删除线的文本',
      style: TextStyle(
        decoration: TextDecoration.lineThrough,
        decorationColor: Colors.red,
      ),
    ),

    // 上划线
    Text(
      '带上划线的文本',
      style: TextStyle(
        decoration: TextDecoration.overline,
        decorationColor: Colors.green,
      ),
    ),
  ],
)

装饰样式

dart 复制代码
Text(
  '双下划线',
  style: TextStyle(
    decoration: TextDecoration.underline,
    decorationStyle: TextDecorationStyle.double,
    decorationColor: Colors.blue,
    decorationThickness: 2,
  ),
)

五、完整示例

dart 复制代码
class BasicTextExample extends StatelessWidget {
  const BasicTextExample({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('基础Text组件')),
      body: Padding(
        padding: EdgeInsets.all(16),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            // 简单文本
            Text('Hello, Flutter!',
                style: TextStyle(fontSize: 24, color: Colors.blue)),
            SizedBox(height: 16),

            // 大标题
            Text('这是大标题',
                style: TextStyle(
                    fontSize: 32,
                    fontWeight: FontWeight.bold,
                    color: Colors.black87)),
            SizedBox(height: 8),

            // 小标题
            Text('这是小标题',
                style: TextStyle(
                    fontSize: 20,
                    fontWeight: FontWeight.w600,
                    color: Colors.black54)),
            SizedBox(height: 8),

            // 正文
            Text('这是正文文本,展示了基本的文本显示功能。',
                style: TextStyle(fontSize: 14, color: Colors.black87)),
            SizedBox(height: 16),

            // 带装饰的文本
            Text('带下划线的文本',
                style: TextStyle(
                    decoration: TextDecoration.underline,
                    decorationColor: Colors.blue)),
            SizedBox(height: 8),

            Text('带删除线的文本',
                style: TextStyle(
                    decoration: TextDecoration.lineThrough,
                    decorationColor: Colors.red)),
          ],
        ),
      ),
    );
  }
}

六、最佳实践

实践 说明 效果
使用const 避免不必要的重建 提升性能
合理设置字号 层次分明 提升可读性
使用语义化颜色 考虑主题一致性 便于主题切换
提取样式常量 复用样式 代码简洁

欢迎加入开源鸿蒙跨平台社区:https://openharmonycrossplatform.csdn.net

相关推荐
程序员老刘1 天前
跨平台开发地图 | 2026年10月
flutter·ai编程·客户端
林瞅瞅1 天前
Flutter 开发环境从零搭建与调试记录
flutter
FungLeo2 天前
成为全栈·Flutter App 篇·Markdown 编辑器:编辑、预览与离开保护
flutter·移动开发·markdown编辑器·表单设计·成为全栈·草稿保存
FungLeo2 天前
成为全栈·Flutter App 篇·会员中心:资料、密码、通知与私有数据缓存
flutter·dart·身份认证·数据隔离·成为全栈·会员中心
穆梓兰煊2 天前
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·鸿蒙