Flutter框架跨平台鸿蒙开发——Text样式详解

一、TextStyle完整属性

TextStyle是Text组件样式的核心,提供了丰富的样式配置选项。

TextStyle属性分类

TextStyle
字体属性
颜色属性
装饰属性
间距属性
其他属性
fontSize
fontWeight
fontFamily
fontStyle
color
backgroundColor
foreground
decoration
decorationColor
decorationStyle
decorationThickness
letterSpacing
wordSpacing
height

二、字体属性详解

字体大小

dart 复制代码
Column(
  children: [
    Text('超大文本', style: TextStyle(fontSize: 48)),
    Text('大文本', style: TextStyle(fontSize: 32)),
    Text('中号文本', style: TextStyle(fontSize: 20)),
    Text('小号文本', style: TextStyle(fontSize: 14)),
    Text('超小文本', style: TextStyle(fontSize: 10)),
  ],
)

字体粗细

dart 复制代码
Column(
  children: [
    Text('Thin (100)', style: TextStyle(fontWeight: FontWeight.w100)),
    Text('Light (300)', style: TextStyle(fontWeight: FontWeight.w300)),
    Text('Regular (400)', style: TextStyle(fontWeight: FontWeight.w400)),
    Text('Medium (500)', style: TextStyle(fontWeight: FontWeight.w500)),
    Text('Bold (700)', style: TextStyle(fontWeight: FontWeight.w700)),
    Text('Black (900)', style: TextStyle(fontWeight: FontWeight.w900)),
  ],
)

字体样式

dart 复制代码
Column(
  children: [
    Text('正常字体', style: TextStyle(fontStyle: FontStyle.normal)),
    Text('斜体字体', style: TextStyle(fontStyle: FontStyle.italic)),
  ],
)

三、颜色属性

文本颜色

dart 复制代码
Column(
  children: [
    Text('红色文本', style: TextStyle(color: Colors.red)),
    Text('蓝色文本', style: TextStyle(color: Colors.blue)),
    Text('绿色文本', style: TextStyle(color: Colors.green)),
    Text('自定义颜色', style: TextStyle(color: Color(0xFF6C63FF))),
    Text('透明度颜色', style: TextStyle(color: Colors.blue.withOpacity(0.5))),
  ],
)

背景颜色

dart 复制代码
Column(
  children: [
    Container(
      padding: EdgeInsets.all(8),
      color: Colors.yellow.shade200,
      child: Text('带背景的文本'),
    ),
    Container(
      padding: EdgeInsets.all(8),
      color: Colors.blue.shade100,
      child: Text(
        '带背景的文本',
        style: TextStyle(
          backgroundColor: Colors.blue.shade50,
          color: Colors.blue.shade900,
        ),
      ),
    ),
  ],
)

四、装饰属性

装饰类型

dart 复制代码
Column(
  children: [
    Text('下划线',
        style: TextStyle(decoration: TextDecoration.underline)),
    Text('上划线',
        style: TextStyle(decoration: TextDecoration.overline)),
    Text('删除线',
        style: TextStyle(decoration: TextDecoration.lineThrough)),
    Text('组合装饰',
        style: TextStyle(decoration: TextDecoration.underline | TextDecoration.lineThrough)),
  ],
)

装饰样式

TextDecorationStyle
solid 实线
double 双线
dotted 点线
dashed 虚线
wavy 波浪线

dart 复制代码
Column(
  children: [
    Text('实线下划线',
        style: TextStyle(
          decoration: TextDecoration.underline,
          decorationStyle: TextDecorationStyle.solid,
        )),
    Text('双线下划线',
        style: TextStyle(
          decoration: TextDecoration.underline,
          decorationStyle: TextDecorationStyle.double,
        )),
    Text('点线下划线',
        style: TextStyle(
          decoration: TextDecoration.underline,
          decorationStyle: TextDecorationStyle.dotted,
        )),
    Text('虚线下划线',
        style: TextStyle(
          decoration: TextDecoration.underline,
          decorationStyle: TextDecorationStyle.dashed,
        )),
    Text('波浪线下划线',
        style: TextStyle(
          decoration: TextDecoration.underline,
          decorationStyle: TextDecorationStyle.wavy,
        )),
  ],
)

五、间距属性

字符间距

dart 复制代码
Column(
  children: [
    Text('正常间距', style: TextStyle(letterSpacing: 0)),
    Text('紧凑间距', style: TextStyle(letterSpacing: -0.5)),
    Text('标准间距', style: TextStyle(letterSpacing: 1.0)),
    Text('宽松间距', style: TextStyle(letterSpacing: 2.0)),
    Text('极宽间距', style: TextStyle(letterSpacing: 4.0)),
  ],
)

单词间距

dart 复制代码
Column(
  children: [
    Text('正常单词间距', style: TextStyle(wordSpacing: 0)),
    Text('宽松单词间距', style: TextStyle(wordSpacing: 2.0)),
    Text('极宽单词间距', style: TextStyle(wordSpacing: 10.0)),
  ],
)

行高

dart 复制代码
Column(
  crossAxisAlignment: CrossAxisAlignment.start,
  children: [
    Text('行高1.0\n第二行\n第三行', style: TextStyle(height: 1.0)),
    SizedBox(height: 8),
    Text('行高1.5\n第二行\n第三行', style: TextStyle(height: 1.5)),
    SizedBox(height: 8),
    Text('行高2.0\n第二行\n第三行', style: TextStyle(height: 2.0)),
  ],
)

六、综合示例

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Text样式')),
      body: SingleChildScrollView(
        padding: EdgeInsets.all(16),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            _buildSection('字体大小'),
            _buildFontSizeExamples(),
            SizedBox(height: 24),
            _buildSection('字体粗细'),
            _buildFontWeightExamples(),
            SizedBox(height: 24),
            _buildSection('颜色示例'),
            _buildColorExamples(),
            SizedBox(height: 24),
            _buildSection('装饰效果'),
            _buildDecorationExamples(),
          ],
        ),
      ),
    );
  }

  Widget _buildSection(String title) {
    return Text(
      title,
      style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
    );
  }

  Widget _buildFontSizeExamples() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text('超大文本 (48px)', style: TextStyle(fontSize: 48)),
        Text('大文本 (32px)', style: TextStyle(fontSize: 32)),
        Text('中号文本 (20px)', style: TextStyle(fontSize: 20)),
        Text('小号文本 (14px)', style: TextStyle(fontSize: 14)),
      ],
    );
  }

  Widget _buildFontWeightExamples() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text('Regular (400)', style: TextStyle(fontWeight: FontWeight.w400)),
        Text('Medium (500)', style: TextStyle(fontWeight: FontWeight.w500)),
        Text('Bold (700)', style: TextStyle(fontWeight: FontWeight.w700)),
      ],
    );
  }

  Widget _buildColorExamples() {
    return Column(
      children: [
        Text('红色文本', style: TextStyle(color: Colors.red)),
        Text('蓝色文本', style: TextStyle(color: Colors.blue)),
        Text('绿色文本', style: TextStyle(color: Colors.green)),
      ],
    );
  }

  Widget _buildDecorationExamples() {
    return Column(
      children: [
        Text('下划线',
            style: TextStyle(decoration: TextDecoration.underline)),
        Text('删除线',
            style: TextStyle(decoration: TextDecoration.lineThrough)),
        Text('波浪线',
            style: TextStyle(
              decoration: TextDecoration.underline,
              decorationStyle: TextDecorationStyle.wavy,
            )),
      ],
    );
  }
}

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

相关推荐
亚历克斯神1 天前
Flutter for OpenHarmony: Flutter 三方库 mutex 为鸿蒙异步任务提供可靠的临界资源互斥锁(并发安全基石)
android·数据库·安全·flutter·华为·harmonyos
钛态1 天前
Flutter 三方库 smartstruct 鸿蒙化字段映射适配指南:介入静态预编译引擎扫除视图及数据模型双向强转类型错乱隐患,筑稳如磐石的企业级模型治理防线-适配鸿蒙 HarmonyOS ohos
flutter·华为·harmonyos
键盘鼓手苏苏1 天前
Flutter 组件 csv2json 适配鸿蒙 HarmonyOS 实战:高性能异构数据转换,构建 CSV 流式解析与全栈式数据映射架构
flutter·harmonyos·鸿蒙·openharmony
左手厨刀右手茼蒿1 天前
Flutter 组件 http_requests 适配鸿蒙 HarmonyOS 实战:极简网络请求,构建边缘端轻量级 RESTful 通讯架构
网络·flutter·http
雷帝木木1 天前
Flutter 三方库 hrk_logging 的鸿蒙化适配指南 - 实现标准化分层日志记录、支持多目的地输出与日志分级过滤
flutter·harmonyos·鸿蒙·openharmony·hrk_logging
左手厨刀右手茼蒿1 天前
Flutter 三方库 dio_compatibility_layer 的鸿蒙化适配指南 - 实现 Dio 跨主版本的平滑迁移、支持遗留拦截器兼容与网络请求架构稳定升级
flutter·harmonyos·鸿蒙·openharmony·dio_compatibility_layer
雷帝木木1 天前
Flutter 三方库 hashids2 基于鸿蒙安全内核的深度隐匿映射适配:数字指纹泄露防御层、生成短小精悍唯一不可逆加盐哈希,护航全链路请求 URL 隐私-适配鸿蒙 HarmonyOS ohos
安全·flutter·harmonyos
王码码20351 天前
Flutter 组件 inappwebview_cookie_manager 适配 鸿蒙Harmony 实战 - 驾驭核心大 Web 容器缓存隧道、构建金融级政企应用绝对防串号跨域大隔离基座
flutter·harmonyos·鸿蒙·openharmony·inappwebview_cookie_manager
左手厨刀右手茼蒿1 天前
Flutter 组件 ews 的适配 鸿蒙Harmony 实战 - 驾驭企业级 Exchange Web Services 协议、实现鸿蒙端政企办公同步与高安通讯隔离方案
flutter·harmonyos·鸿蒙·openharmony
钛态1 天前
Flutter 三方库 react 泛前端核心范式框架鸿蒙原生层生态级双向超能适配:跨时空重塑响应式单向数据流拓扑与高度精密生命周期树引擎解耦视图渲染控制中枢(适配鸿蒙 HarmonyOS ohos)
前端·flutter·react.js