@@ -3,14 +3,14 @@ import 'package:flutter/foundation.dart';
33import 'package:flutter/material.dart' ;
44import 'package:flutter/services.dart' ;
55import 'package:get/get.dart' ;
6- import 'package:flutter_td_getx_template /res/json_res.dart' ;
7- import 'package:tdesign_flutter/tdesign_flutter. dart' ;
6+ import 'package:flutter_template /res/json_res.dart' ;
7+ import 'dart:convert ' ;
88
99import 'core/util/storage/storage_util.dart' ;
1010
1111class Application {
12- /// 主题 - 使用响应式变量
13- static final Rx <TDThemeData > themeData = TDThemeData . defaultData ( ).obs;
12+ /// 主题 - 使用响应式变量 (Material 3)
13+ static final Rx <ThemeData > themeData = _themeFromSeed ( const Color ( 0xFF2f46ec ) ).obs;
1414
1515 /// Alice 网络请求调试工具
1616 static late Alice alice;
@@ -52,17 +52,67 @@ class Application {
5252
5353 /// 设置主题样式
5454 static setTheme () async {
55- // 主题配置
56- TDTheme .needMultiTheme ();
57- var jsonString = await rootBundle.loadString (JsonRes .theme);
58- themeData.value = TDThemeData .fromJson ('theme' , jsonString)! ;
55+ final jsonString = await rootBundle.loadString (JsonRes .theme);
56+ final seed = _extractBrandColor7 (jsonString, 'theme' ) ?? const Color (0xFF2f46ec );
57+ themeData.value = _themeFromSeed (seed);
5958 }
6059
6160 /// 更新主题
62- static void updateTheme (TDThemeData newTheme) {
61+ static void updateTheme (ThemeData newTheme) {
6362 themeData.value = newTheme;
6463 }
6564
65+ /// 提供外部调用的种子主题构建
66+ static ThemeData themeFromSeed (Color seed) => _themeFromSeed (seed);
67+
68+ /// 从 JSON 中提取 brandColor7 作为种子色
69+ static Color ? _extractBrandColor7 (String jsonString, String key) {
70+ try {
71+ final Map <String , dynamic > data = json.decode (jsonString) as Map <String , dynamic >;
72+ final Map <String , dynamic >? section = data[key] as Map <String , dynamic >? ;
73+ if (section == null ) return null ;
74+ final Map <String , dynamic >? color = section['color' ] as Map <String , dynamic >? ;
75+ final String ? hex = color? ['brandColor7' ] as String ? ;
76+ if (hex == null ) return null ;
77+ return _colorFromHex (hex);
78+ } catch (_) {
79+ return null ;
80+ }
81+ }
82+
83+ static ThemeData _themeFromSeed (Color seed) {
84+ final colorScheme = ColorScheme .fromSeed (seedColor: seed, brightness: Brightness .light);
85+ return ThemeData (
86+ useMaterial3: true ,
87+ colorScheme: colorScheme,
88+ appBarTheme: AppBarTheme (
89+ backgroundColor: colorScheme.surface,
90+ foregroundColor: colorScheme.onSurface,
91+ elevation: 0 ,
92+ centerTitle: true ,
93+ ),
94+ navigationBarTheme: NavigationBarThemeData (
95+ backgroundColor: colorScheme.surface,
96+ indicatorColor: colorScheme.secondaryContainer,
97+ iconTheme: WidgetStateProperty .all (IconThemeData (color: colorScheme.onSurfaceVariant)),
98+ labelTextStyle: WidgetStateProperty .all (TextStyle (color: colorScheme.onSurfaceVariant)),
99+ ),
100+ filledButtonTheme: FilledButtonThemeData (
101+ style: FilledButton .styleFrom (
102+ backgroundColor: colorScheme.primary,
103+ foregroundColor: colorScheme.onPrimary,
104+ ),
105+ ),
106+ );
107+ }
108+
109+ static Color _colorFromHex (String hex) {
110+ final buffer = StringBuffer ();
111+ if (hex.length == 6 || hex.length == 7 ) buffer.write ('ff' );
112+ buffer.write (hex.replaceFirst ('#' , '' ));
113+ return Color (int .parse (buffer.toString (), radix: 16 ));
114+ }
115+
66116 /// 初始化 Alice
67117 static initAlice () {
68118 // 仅在调试模式下初始化 Alice
0 commit comments