import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; /// 应用主题配置类 class AppTheme { // 私有构造函数,防止实例化 AppTheme._(); /// 主题色彩配置 static const Color primaryColor = Color(0xFF2196F3); static const Color secondaryColor = Color(0xFF03DAC6); static const Color backgroundColor = Color(0xFFF5F5F5); static const Color surfaceColor = Colors.white; static const Color errorColor = Color(0xFFB00020); /// 文字颜色 static const Color textPrimary = Color(0xFF212121); static const Color textSecondary = Color(0xFF757575); static const Color textHint = Color(0xFFBDBDBD); /// 边框和分割线颜色 static const Color dividerColor = Color(0xFFE0E0E0); static const Color borderColor = Color(0xFFCCCCCC); /// 浅色主题 static ThemeData lightTheme = ThemeData( useMaterial3: true, brightness: Brightness.light, primaryColor: primaryColor, colorScheme: const ColorScheme.light( primary: primaryColor, secondary: secondaryColor, surface: surfaceColor, background: backgroundColor, error: errorColor, onPrimary: Colors.white, onSecondary: Colors.black, onSurface: textPrimary, onBackground: textPrimary, onError: Colors.white, ), // 应用栏主题 appBarTheme: const AppBarTheme( backgroundColor: surfaceColor, foregroundColor: textPrimary, elevation: 0, centerTitle: true, systemOverlayStyle: SystemUiOverlayStyle( statusBarColor: Colors.transparent, statusBarIconBrightness: Brightness.dark, statusBarBrightness: Brightness.light, ), ), // 卡片主题 cardTheme: CardTheme( elevation: 2, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(8.0), ), margin: const EdgeInsets.all(8.0), ), // 按钮主题 elevatedButtonTheme: ElevatedButtonThemeData( style: ElevatedButton.styleFrom( elevation: 2, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(8.0), ), padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 12.0), ), ), // 输入框主题 inputDecorationTheme: InputDecorationTheme( filled: true, fillColor: Colors.grey[100], border: OutlineInputBorder( borderRadius: BorderRadius.circular(8.0), borderSide: const BorderSide(color: borderColor), ), enabledBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(8.0), borderSide: const BorderSide(color: borderColor), ), focusedBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(8.0), borderSide: const BorderSide(color: primaryColor, width: 2.0), ), contentPadding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 12.0), ), // 文字主题 textTheme: const TextTheme( displayLarge: TextStyle(fontSize: 57.0, fontWeight: FontWeight.w400, color: textPrimary), displayMedium: TextStyle(fontSize: 45.0, fontWeight: FontWeight.w400, color: textPrimary), displaySmall: TextStyle(fontSize: 36.0, fontWeight: FontWeight.w400, color: textPrimary), headlineLarge: TextStyle(fontSize: 32.0, fontWeight: FontWeight.w400, color: textPrimary), headlineMedium: TextStyle(fontSize: 28.0, fontWeight: FontWeight.w400, color: textPrimary), headlineSmall: TextStyle(fontSize: 24.0, fontWeight: FontWeight.w400, color: textPrimary), titleLarge: TextStyle(fontSize: 22.0, fontWeight: FontWeight.w400, color: textPrimary), titleMedium: TextStyle(fontSize: 16.0, fontWeight: FontWeight.w500, color: textPrimary), titleSmall: TextStyle(fontSize: 14.0, fontWeight: FontWeight.w500, color: textPrimary), bodyLarge: TextStyle(fontSize: 16.0, fontWeight: FontWeight.w400, color: textPrimary), bodyMedium: TextStyle(fontSize: 14.0, fontWeight: FontWeight.w400, color: textPrimary), bodySmall: TextStyle(fontSize: 12.0, fontWeight: FontWeight.w400, color: textSecondary), labelLarge: TextStyle(fontSize: 14.0, fontWeight: FontWeight.w500, color: textPrimary), labelMedium: TextStyle(fontSize: 12.0, fontWeight: FontWeight.w500, color: textPrimary), labelSmall: TextStyle(fontSize: 11.0, fontWeight: FontWeight.w500, color: textPrimary), ), // 图标主题 iconTheme: const IconThemeData( color: textSecondary, size: 24.0, ), // 分割线主题 dividerTheme: const DividerThemeData( color: dividerColor, thickness: 1.0, space: 0, ), ); /// 深色主题 static ThemeData darkTheme = ThemeData( useMaterial3: true, brightness: Brightness.dark, primaryColor: primaryColor, colorScheme: const ColorScheme.dark( primary: primaryColor, secondary: secondaryColor, surface: Color(0xFF121212), background: Color(0xFF121212), error: errorColor, onPrimary: Colors.white, onSecondary: Colors.black, onSurface: Colors.white, onBackground: Colors.white, onError: Colors.white, ), // 应用栏主题(深色模式保持一定对比度) appBarTheme: const AppBarTheme( backgroundColor: Color(0xFF1E1E1E), foregroundColor: Colors.white, elevation: 0, centerTitle: true, systemOverlayStyle: SystemUiOverlayStyle( statusBarColor: Colors.transparent, statusBarIconBrightness: Brightness.light, statusBarBrightness: Brightness.dark, ), ), // 卡片主题 cardTheme: CardTheme( elevation: 4, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(8.0), ), margin: const EdgeInsets.all(8.0), color: const Color(0xFF1E1E1E), ), // 按钮主题 elevatedButtonTheme: ElevatedButtonThemeData( style: ElevatedButton.styleFrom( elevation: 2, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(8.0), ), padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 12.0), backgroundColor: primaryColor, foregroundColor: Colors.white, ), ), // 输入框主题 inputDecorationTheme: InputDecorationTheme( filled: true, fillColor: const Color(0xFF2C2C2C), border: OutlineInputBorder( borderRadius: BorderRadius.circular(8.0), borderSide: const BorderSide(color: Color(0xFF3C3C3C)), ), enabledBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(8.0), borderSide: const BorderSide(color: Color(0xFF3C3C3C)), ), focusedBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(8.0), borderSide: const BorderSide(color: primaryColor, width: 2.0), ), contentPadding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 12.0), ), // 文字主题(深色模式保持可读性) textTheme: const TextTheme( displayLarge: TextStyle(fontSize: 57.0, fontWeight: FontWeight.w400, color: Colors.white), displayMedium: TextStyle(fontSize: 45.0, fontWeight: FontWeight.w400, color: Colors.white), displaySmall: TextStyle(fontSize: 36.0, fontWeight: FontWeight.w400, color: Colors.white), headlineLarge: TextStyle(fontSize: 32.0, fontWeight: FontWeight.w400, color: Colors.white), headlineMedium: TextStyle(fontSize: 28.0, fontWeight: FontWeight.w400, color: Colors.white), headlineSmall: TextStyle(fontSize: 24.0, fontWeight: FontWeight.w400, color: Colors.white), titleLarge: TextStyle(fontSize: 22.0, fontWeight: FontWeight.w400, color: Colors.white), titleMedium: TextStyle(fontSize: 16.0, fontWeight: FontWeight.w500, color: Colors.white), titleSmall: TextStyle(fontSize: 14.0, fontWeight: FontWeight.w500, color: Colors.white), bodyLarge: TextStyle(fontSize: 16.0, fontWeight: FontWeight.w400, color: Colors.white), bodyMedium: TextStyle(fontSize: 14.0, fontWeight: FontWeight.w400, color: Colors.white), bodySmall: TextStyle(fontSize: 12.0, fontWeight: FontWeight.w400, color: Color(0xFFB0B0B0)), labelLarge: TextStyle(fontSize: 14.0, fontWeight: FontWeight.w500, color: Colors.white), labelMedium: TextStyle(fontSize: 12.0, fontWeight: FontWeight.w500, color: Colors.white), labelSmall: TextStyle(fontSize: 11.0, fontWeight: FontWeight.w500, color: Colors.white), ), // 图标主题 iconTheme: const IconThemeData( color: Color(0xFFB0B0B0), size: 24.0, ), // 分割线主题 dividerTheme: const DividerThemeData( color: Color(0xFF3C3C3C), thickness: 1.0, space: 0, ), ); /// 获取主题模式对应的主题数据 static ThemeData getTheme(Brightness brightness) { return brightness == Brightness.dark ? darkTheme : lightTheme; } /// 获取当前主题的对比色(用于保证可读性) static Color getContrastColor(Color backgroundColor) { // 计算颜色的亮度 final brightness = backgroundColor.computeLuminance(); // 亮度大于0.5使用深色,否则使用浅色 return brightness > 0.5 ? Colors.black : Colors.white; } }