304 lines
12 KiB
Dart
304 lines
12 KiB
Dart
import 'package:flutter/material.dart';
|
||
|
||
enum AppThemeMode { light, dark, system }
|
||
|
||
/// 可选主题配色:切换种子色即改变全站强调色与 surface 色调。
|
||
enum AppThemeColor {
|
||
zl2Green('ZL2 绿', Color(0xFF4CAF50)),
|
||
oceanBlue('海蓝', Color(0xFF2196F3)),
|
||
violet('星紫', Color(0xFF9C27B0)),
|
||
sunsetOrange('暖橙', Color(0xFFFF9800)),
|
||
sakuraPink('樱粉', Color(0xFFE91E63));
|
||
|
||
const AppThemeColor(this.label, this.seed);
|
||
final String label;
|
||
final Color seed;
|
||
}
|
||
|
||
/// Material 3 主题(参考 ZalithLauncher 的 M3 实现风格):
|
||
/// - 用种子色生成完整 tonal palette,强调 colorScheme 的 surface container 层级
|
||
/// - 形状体系改用 M3 标准圆角(8 / 12 / 16 / 28),容器类用 16-28 的大圆角
|
||
/// - 卡片/列表靠 surfaceContainer 区分层次,弱化描边、不用阴影
|
||
class AppTheme {
|
||
/// 形状体系(对齐 M3 shape scale)
|
||
static const radiusXS = 8.0; // 小控件 / chip
|
||
static const radiusS = 12.0; // 输入框 / 按钮
|
||
static const radiusM = 16.0; // 卡片 / 列表容器
|
||
static const radiusL = 28.0; // 弹窗 / 底部面板 / FAB
|
||
|
||
/// 默认配色(ZL2 绿)的亮/暗主题:保留 getter,兼容既有调用与官方测试。
|
||
static ThemeData get lightTheme => lightThemeFor(AppThemeColor.zl2Green);
|
||
static ThemeData get darkTheme => darkThemeFor(AppThemeColor.zl2Green);
|
||
|
||
/// 按指定配色生成亮/暗主题;切换配色后整棵 widget 树重建即可生效。
|
||
static ThemeData lightThemeFor(AppThemeColor color) =>
|
||
_base(Brightness.light, color.seed);
|
||
|
||
static ThemeData darkThemeFor(AppThemeColor color) =>
|
||
_base(Brightness.dark, color.seed);
|
||
|
||
/// 把中性灰按种子色轻微染色,让 surface 层次跟随当前配色。
|
||
static Color _tint(Color neutral, Color seed, bool isDark) =>
|
||
Color.alphaBlend(seed.withOpacity(isDark ? 0.07 : 0.05), neutral);
|
||
|
||
static ThemeData _base(Brightness brightness, Color seed) {
|
||
final isDark = brightness == Brightness.dark;
|
||
Color t(Color n) => _tint(n, seed, isDark);
|
||
final scheme = ColorScheme.fromSeed(
|
||
seedColor: seed,
|
||
brightness: brightness,
|
||
// 让主色更饱和、更接近 ZL2 那种"绿色主色调穿透整个界面"的效果
|
||
dynamicSchemeVariant: DynamicSchemeVariant.fidelity,
|
||
).copyWith(
|
||
surface: isDark ? t(const Color(0xFF101310)) : t(const Color(0xFFF7F9F7)),
|
||
surfaceContainerLowest: isDark ? t(const Color(0xFF0B0D0B)) : Colors.white,
|
||
surfaceContainerLow: isDark ? t(const Color(0xFF141714)) : t(const Color(0xFFF1F4F1)),
|
||
surfaceContainer: isDark ? t(const Color(0xFF1A1D1A)) : t(const Color(0xFFEBEFEB)),
|
||
surfaceContainerHigh: isDark ? t(const Color(0xFF222622)) : t(const Color(0xFFE3E8E3)),
|
||
surfaceContainerHighest: isDark ? t(const Color(0xFF2A2F2A)) : t(const Color(0xFFDCE2DC)),
|
||
);
|
||
|
||
final cardColor = isDark ? scheme.surfaceContainer : scheme.surfaceContainerLowest;
|
||
final outline = isDark ? t(const Color(0xFF2E332E)) : t(const Color(0xFFDDE3DD));
|
||
|
||
final shapeXS = RoundedRectangleBorder(borderRadius: BorderRadius.circular(radiusXS));
|
||
final shapeS = RoundedRectangleBorder(borderRadius: BorderRadius.circular(radiusS));
|
||
final shapeM = RoundedRectangleBorder(borderRadius: BorderRadius.circular(radiusM));
|
||
final shapeL = RoundedRectangleBorder(borderRadius: BorderRadius.circular(radiusL));
|
||
|
||
return ThemeData(
|
||
useMaterial3: true,
|
||
colorScheme: scheme,
|
||
scaffoldBackgroundColor: scheme.surface,
|
||
// M3 强调排版层级:标题加粗收紧字距,正文行高放宽
|
||
textTheme: Typography.material2021(platform: TargetPlatform.android)
|
||
.black
|
||
.apply(
|
||
bodyColor: scheme.onSurface,
|
||
displayColor: scheme.onSurface,
|
||
)
|
||
.copyWith(
|
||
titleLarge: TextStyle(
|
||
fontSize: 22,
|
||
fontWeight: FontWeight.w700,
|
||
letterSpacing: -0.2,
|
||
color: scheme.onSurface,
|
||
),
|
||
titleMedium: TextStyle(
|
||
fontSize: 16,
|
||
fontWeight: FontWeight.w600,
|
||
letterSpacing: 0,
|
||
color: scheme.onSurface,
|
||
),
|
||
bodyMedium: TextStyle(
|
||
fontSize: 14,
|
||
height: 1.5,
|
||
color: scheme.onSurface,
|
||
),
|
||
labelLarge: TextStyle(
|
||
fontSize: 14,
|
||
fontWeight: FontWeight.w600,
|
||
letterSpacing: 0.1,
|
||
color: scheme.onSurface,
|
||
),
|
||
),
|
||
appBarTheme: AppBarTheme(
|
||
centerTitle: false,
|
||
elevation: 0,
|
||
scrolledUnderElevation: 0,
|
||
backgroundColor: Colors.transparent,
|
||
surfaceTintColor: Colors.transparent,
|
||
titleTextStyle: TextStyle(
|
||
fontSize: 22,
|
||
fontWeight: FontWeight.w700,
|
||
letterSpacing: -0.2,
|
||
color: scheme.onSurface,
|
||
),
|
||
),
|
||
cardTheme: CardThemeData(
|
||
elevation: 0,
|
||
margin: EdgeInsets.zero,
|
||
clipBehavior: Clip.antiAlias,
|
||
color: cardColor,
|
||
surfaceTintColor: Colors.transparent,
|
||
shape: RoundedRectangleBorder(
|
||
borderRadius: BorderRadius.circular(radiusM),
|
||
side: BorderSide(color: outline, width: 1),
|
||
),
|
||
),
|
||
inputDecorationTheme: InputDecorationTheme(
|
||
filled: true,
|
||
fillColor: isDark ? scheme.surfaceContainerHigh : scheme.surfaceContainerLow,
|
||
contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 14),
|
||
border: OutlineInputBorder(
|
||
borderRadius: BorderRadius.circular(radiusS),
|
||
borderSide: BorderSide.none,
|
||
),
|
||
enabledBorder: OutlineInputBorder(
|
||
borderRadius: BorderRadius.circular(radiusS),
|
||
borderSide: BorderSide.none,
|
||
),
|
||
focusedBorder: OutlineInputBorder(
|
||
borderRadius: BorderRadius.circular(radiusS),
|
||
borderSide: BorderSide(color: scheme.primary, width: 2),
|
||
),
|
||
errorBorder: OutlineInputBorder(
|
||
borderRadius: BorderRadius.circular(radiusS),
|
||
borderSide: BorderSide(color: scheme.error),
|
||
),
|
||
),
|
||
filledButtonTheme: FilledButtonThemeData(
|
||
style: FilledButton.styleFrom(
|
||
shape: shapeS,
|
||
minimumSize: const Size(0, 48),
|
||
padding: const EdgeInsets.symmetric(horizontal: 22),
|
||
textStyle: const TextStyle(fontWeight: FontWeight.w600, fontSize: 14),
|
||
),
|
||
),
|
||
outlinedButtonTheme: OutlinedButtonThemeData(
|
||
style: OutlinedButton.styleFrom(
|
||
shape: shapeS,
|
||
minimumSize: const Size(0, 48),
|
||
side: BorderSide(color: scheme.outlineVariant),
|
||
textStyle: const TextStyle(fontWeight: FontWeight.w600, fontSize: 14),
|
||
),
|
||
),
|
||
textButtonTheme: TextButtonThemeData(
|
||
style: TextButton.styleFrom(shape: shapeXS),
|
||
),
|
||
// M3 的 FAB 是大圆角容器(16),extended 用 16 胶囊
|
||
floatingActionButtonTheme: FloatingActionButtonThemeData(
|
||
shape: const RoundedRectangleBorder(
|
||
borderRadius: BorderRadius.all(Radius.circular(16)),
|
||
),
|
||
elevation: 1,
|
||
highlightElevation: 1,
|
||
),
|
||
dialogTheme: DialogThemeData(
|
||
shape: shapeL,
|
||
backgroundColor: isDark ? scheme.surfaceContainerHigh : scheme.surfaceContainerLowest,
|
||
surfaceTintColor: Colors.transparent,
|
||
),
|
||
bottomSheetTheme: BottomSheetThemeData(
|
||
backgroundColor: isDark ? scheme.surfaceContainer : scheme.surfaceContainerLowest,
|
||
surfaceTintColor: Colors.transparent,
|
||
shape: const RoundedRectangleBorder(
|
||
borderRadius: BorderRadius.vertical(top: Radius.circular(28)),
|
||
),
|
||
),
|
||
snackBarTheme: SnackBarThemeData(
|
||
behavior: SnackBarBehavior.floating,
|
||
shape: shapeXS,
|
||
insetPadding: const EdgeInsets.all(16),
|
||
),
|
||
navigationBarTheme: NavigationBarThemeData(
|
||
height: 72,
|
||
elevation: 0,
|
||
backgroundColor: isDark ? scheme.surfaceContainer : scheme.surfaceContainerLowest,
|
||
surfaceTintColor: Colors.transparent,
|
||
// ZL2 风格:选中项用主色绿 + 浅绿圆角胶囊指示器
|
||
indicatorColor: isDark ? scheme.primary.withOpacity(0.22) : scheme.primaryContainer,
|
||
indicatorShape: const RoundedRectangleBorder(
|
||
borderRadius: BorderRadius.all(Radius.circular(16)),
|
||
),
|
||
labelBehavior: NavigationDestinationLabelBehavior.alwaysShow,
|
||
labelTextStyle: WidgetStateProperty.resolveWith((states) {
|
||
final selected = states.contains(WidgetState.selected);
|
||
return TextStyle(
|
||
fontSize: 12,
|
||
fontWeight: selected ? FontWeight.w700 : FontWeight.w500,
|
||
color: selected ? scheme.primary : scheme.onSurfaceVariant,
|
||
);
|
||
}),
|
||
iconTheme: WidgetStateProperty.resolveWith((states) {
|
||
final selected = states.contains(WidgetState.selected);
|
||
return IconThemeData(
|
||
size: 24,
|
||
color: selected ? scheme.primary : scheme.onSurfaceVariant,
|
||
);
|
||
}),
|
||
),
|
||
progressIndicatorTheme: ProgressIndicatorThemeData(
|
||
linearTrackColor: scheme.surfaceContainerHighest,
|
||
linearMinHeight: 6,
|
||
circularTrackColor: scheme.surfaceContainerHighest,
|
||
),
|
||
chipTheme: ChipThemeData(
|
||
shape: shapeXS,
|
||
side: BorderSide.none,
|
||
backgroundColor: scheme.surfaceContainerHigh,
|
||
selectedColor: scheme.secondaryContainer,
|
||
labelStyle: TextStyle(fontSize: 13, color: scheme.onSurfaceVariant),
|
||
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 4),
|
||
),
|
||
dividerTheme: DividerThemeData(
|
||
thickness: 1,
|
||
space: 1,
|
||
color: scheme.outlineVariant,
|
||
),
|
||
listTileTheme: ListTileThemeData(
|
||
shape: shapeM,
|
||
iconColor: scheme.onSurfaceVariant,
|
||
),
|
||
popupMenuTheme: PopupMenuThemeData(
|
||
shape: shapeM,
|
||
elevation: 2,
|
||
color: isDark ? scheme.surfaceContainerHigh : scheme.surfaceContainerLowest,
|
||
surfaceTintColor: Colors.transparent,
|
||
),
|
||
expansionTileTheme: ExpansionTileThemeData(
|
||
shape: shapeM,
|
||
collapsedShape: shapeM,
|
||
iconColor: scheme.onSurfaceVariant,
|
||
),
|
||
tabBarTheme: TabBarThemeData(
|
||
dividerColor: Colors.transparent,
|
||
indicatorSize: TabBarIndicatorSize.label,
|
||
labelColor: scheme.onSurface,
|
||
unselectedLabelColor: scheme.onSurfaceVariant,
|
||
indicator: UnderlineTabIndicator(
|
||
borderSide: BorderSide(width: 3, color: scheme.primary),
|
||
borderRadius: const BorderRadius.vertical(top: Radius.circular(3)),
|
||
),
|
||
),
|
||
segmentedButtonTheme: SegmentedButtonThemeData(
|
||
style: ButtonStyle(
|
||
shape: WidgetStateProperty.all(
|
||
const RoundedRectangleBorder(
|
||
borderRadius: BorderRadius.all(Radius.circular(radiusS)),
|
||
),
|
||
),
|
||
side: WidgetStateProperty.all(BorderSide(color: outline)),
|
||
textStyle: WidgetStateProperty.all(
|
||
const TextStyle(fontSize: 13, fontWeight: FontWeight.w600),
|
||
),
|
||
),
|
||
),
|
||
switchTheme: SwitchThemeData(
|
||
thumbColor: WidgetStateProperty.resolveWith((states) =>
|
||
states.contains(WidgetState.selected) ? scheme.onPrimary : scheme.outline),
|
||
trackColor: WidgetStateProperty.resolveWith((states) =>
|
||
states.contains(WidgetState.selected) ? scheme.primary : scheme.surfaceContainerHighest),
|
||
),
|
||
bottomNavigationBarTheme: BottomNavigationBarThemeData(
|
||
elevation: 0,
|
||
backgroundColor: isDark ? scheme.surfaceContainer : scheme.surfaceContainerLowest,
|
||
selectedItemColor: scheme.primary,
|
||
unselectedItemColor: scheme.onSurfaceVariant,
|
||
type: BottomNavigationBarType.fixed,
|
||
),
|
||
);
|
||
}
|
||
|
||
static ThemeMode toFlutterThemeMode(AppThemeMode mode) {
|
||
switch (mode) {
|
||
case AppThemeMode.light:
|
||
return ThemeMode.light;
|
||
case AppThemeMode.dark:
|
||
return ThemeMode.dark;
|
||
case AppThemeMode.system:
|
||
return ThemeMode.system;
|
||
}
|
||
}
|
||
}
|