Flutter 中使用 Color 的最优方案

在 Flutter 中使用 Color 的最优方案,核心是统一管理 + 按需扩展,既保证代码可维护性,又兼顾灵活性。

  1. 基础:统一颜色管理(核心方案)

避免在 UI 中硬写Color(0xFFxxxxxx),通过专门的类集中管理颜色,是最关键的优化。

推荐实现:创建AppColors工具类

Dart 复制代码
class AppColors {
  // 主色调(品牌色)
  static const Color primary = Color(0xFF2196F3); // 蓝色主色
  static const Color primaryDark = Color(0xFF1976D2); // 主色深色
  static const Color primaryLight = Color(0xFFBBDEFB); // 主色浅色

  // 辅助色(功能色:成功、警告、错误等)
  static const Color success = Color(0xFF4CAF50);
  static const Color warning = Color(0xFFFFC107);
  static const Color error = Color(0xFFF44336);

  // 中性色(文本、背景、边框等)
  static const Color textPrimary = Color(0xFF333333); // 主要文本
  static const Color textSecondary = Color(0xFF666666); // 次要文本
  static const Color bgMain = Color(0xFFFFFFFF); // 主背景
  static const Color border = Color(0xFFE0E0E0); // 边框色
}

使用场景:UI 中直接调用,如 Text("内容", style: TextStyle(color: AppColors.textPrimary)),后续修改颜色只需改AppColors,无需全局搜索。

相关推荐
恋猫de小郭5 小时前
Flutter 下 AOT 一个你怎么都想不出来的 Bug ,JIT 正常的计算,在 AOT 居然算错了
android·前端·flutter
好的佩奇21 小时前
Dart 之扩展方法
android·flutter·dart
好的佩奇1 天前
蓝牙之经典蓝牙
android·flutter·蓝牙
传奇开心果编程1 天前
【Flutter踩坑排雷】第19课:路由硬编码坑|字符串路由书写错误,页面跳转失败排查
android·flutter·ios·移动开发·跨平台·ui框架·声明式ui
Nerd(BUG版)2 天前
Flutter学习day5--事件手势与命中测试
学习·flutter
传奇开心果2 天前
【Flutter踩坑排雷】第5课:扩展方法冲突坑|同名扩展引发编译歧义,故障规避手段
android·flutter
雪铃儿3 天前
Flutter 双端热更新新方案:基于 Dart 官方动态模块的 flutter_patcher 1.0.0
flutter·hotfix
好的佩奇3 天前
Dart 之方法、操作符与可调用对象
android·flutter·dart
恋猫de小郭3 天前
Flutter Impeller Vulkan 内存持续上涨,问题没想到是文字阴影
android·前端·flutter