首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >Flutter 零基础入门(四十一):Flutter 主题与自定义样式 —— App 美化必学

Flutter 零基础入门(四十一):Flutter 主题与自定义样式 —— App 美化必学

作者头像
LarryLan
发布2026-03-03 15:50:15
发布2026-03-03 15:50:15
180
举报

Flutter 主题与自定义样式 —— App 美化必学

到目前为止,你已经掌握了:

  • 动态页面与异步操作
  • 多页面 App 框架(TabBar + BottomNavigationBar)
  • 列表刷新与分页
  • 用户操作反馈(SnackBar / AlertDialog)

但是一个 App 不仅要功能完整,还要美观一致。 Flutter 提供了 Theme / ThemeData 来统一管理样式。


一、Theme 基础

1️⃣ 全局主题

在 MaterialApp 中配置:

代码语言:javascript
复制
MaterialApp(
  title: 'Flutter 美化示例',
  theme: ThemeData(
    primarySwatch: Colors.blue,
    brightness: Brightness.light,
    accentColor: Colors.orange,
    textTheme: TextTheme(
      bodyText1: TextStyle(fontSize: 16, color: Colors.black),
      bodyText2: TextStyle(fontSize: 14, color: Colors.grey[700]),
    ),
    elevatedButtonTheme: ElevatedButtonThemeData(
      style: ElevatedButton.styleFrom(
        primary: Colors.blue,
        onPrimary: Colors.white,
        textStyle: TextStyle(fontSize: 16),
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(8),
        ),
      ),
    ),
  ),
  home: HomePage(),
);

📌 说明:

  • primarySwatch → App 主色
  • brightness → 明暗主题
  • accentColor → 点缀颜色(部分组件使用)
  • textTheme → 全局文字样式
  • elevatedButtonTheme → 按钮统一样式

2️⃣ 局部主题覆盖

代码语言:javascript
复制
Theme(
  data: Theme.of(context).copyWith(
    primaryColor: Colors.red,
  ),
  child: AppBar(title: Text('局部主题')),
)

📌 ThemeData 可以在局部覆盖 → 灵活定制部分页面样式


二、常用组件自定义样式

1️⃣ AppBar

代码语言:javascript
复制
AppBar(
  title: Text('自定义 AppBar'),
  backgroundColor: Colors.deepPurple,
  centerTitle: true,
  elevation: 4,
)

2️⃣ ElevatedButton

代码语言:javascript
复制
ElevatedButton(
  onPressed: () {},
  child: Text('提交'),
  style: ElevatedButton.styleFrom(
    primary: Colors.green,
    onPrimary: Colors.white,
    textStyle: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
    shape: RoundedRectangleBorder(
      borderRadius: BorderRadius.circular(12),
    ),
  ),
)

3️⃣ TextField

代码语言:javascript
复制
TextField(
  decoration: InputDecoration(
    labelText: '用户名',
    labelStyle: TextStyle(color: Colors.grey[600]),
    border: OutlineInputBorder(
      borderRadius: BorderRadius.circular(8),
    ),
    focusedBorder: OutlineInputBorder(
      borderSide: BorderSide(color: Colors.blue, width: 2),
    ),
  ),
)

三、统一颜色管理

创建一个 colors.dart 文件:

代码语言:javascript
复制
import 'package:flutter/material.dart';

class AppColors {
  static const primary = Color(0xFF2196F3);
  static const accent = Color(0xFFFF9800);
  static const background = Color(0xFFF5F5F5);
  static const textPrimary = Color(0xFF212121);
  static const textSecondary = Color(0xFF757575);
}

然后在 ThemeData 和组件中使用:

代码语言:javascript
复制
theme: ThemeData(
  primaryColor: AppColors.primary,
  accentColor: AppColors.accent,
)

📌 好处:

  • App 颜色统一
  • 后续维护更方便
  • 设计稿颜色易对齐

四、统一字体管理

pubspec.yaml 添加自定义字体:

代码语言:javascript
复制
fonts:
  - family: Roboto
    fonts:
      - asset: fonts/Roboto-Regular.ttf
      - asset: fonts/Roboto-Bold.ttf
        weight: 700

然后在 ThemeData 使用:

代码语言:javascript
复制
theme: ThemeData(
  fontFamily: 'Roboto',
)

📌 全局文字风格统一 → App 更专业


五、快速美化按钮与卡片

  • 使用 ElevatedButtonThemeData 统一按钮样式
  • 使用 Card + RoundedRectangleBorder + elevation 提升卡片质感
代码语言:javascript
复制
Card(
  elevation: 4,
  shape: RoundedRectangleBorder(
    borderRadius: BorderRadius.circular(12),
  ),
  child: Padding(
    padding: EdgeInsets.all(16),
    child: Text('卡片内容'),
  ),
)

六、常见坑

❌ 忘记在 MaterialApp 配置 theme → 主题无效 ❌ 多处直接写颜色 / 字体 → 维护困难 ❌ 局部 ThemeData 与全局冲突 → 样式混乱

📌 建议:

  • 统一 ThemeData + colors.dart + fonts
  • 局部覆盖时用 Theme.of(context).copyWith
  • 避免硬编码颜色与字体

七、本篇你真正掌握了什么?

你已经学会:

  • App 全局主题 ThemeData
  • 自定义颜色、字体
  • 按钮 / TextField / Card 样式美化
  • 局部覆盖主题
  • 提升 App 一致性与专业感

📌 到这里为止:

你的 Flutter App 已经可以看起来像“专业作品”了


八、一句话总结

ThemeData + 统一颜色字体 + 组件样式 App 美化统一且易维护


🔜 下一篇预告

《Flutter 零基础入门(四十二):Flutter 路由与导航进阶 —— 命名路由与参数传递实战》

下一篇我们将学习:

  • 命名路由使用
  • 页面之间传递参数
  • 返回值获取
  • 多页面复杂导航实战

🚀 让你的 App 页面跳转更高效、可维护

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2026-03-02,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Larry的Hub 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Flutter 主题与自定义样式 —— App 美化必学
    • 一、Theme 基础
      • 1️⃣ 全局主题
      • 2️⃣ 局部主题覆盖
    • 二、常用组件自定义样式
      • 1️⃣ AppBar
      • 2️⃣ ElevatedButton
      • 3️⃣ TextField
    • 三、统一颜色管理
    • 四、统一字体管理
    • 五、快速美化按钮与卡片
    • 六、常见坑
    • 七、本篇你真正掌握了什么?
    • 八、一句话总结
    • 🔜 下一篇预告
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档