theme_config.dart 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. import 'package:flutter/cupertino.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter/services.dart';
  4. import 'package:get/get.dart';
  5. import '../../constants/color_constants.dart';
  6. class ThemeConfig {
  7. static ThemeData createTheme({
  8. required Brightness brightness,
  9. required Color background,
  10. required Color primaryText,
  11. Color? secondaryText,
  12. required Color accentColor,
  13. Color? divider,
  14. Color? buttonBackground,
  15. required Color buttonText,
  16. Color? cardBackground,
  17. Color? disabled,
  18. required Color error,
  19. }) {
  20. final baseTextTheme = brightness == Brightness.dark ? Typography.blackMountainView : Typography.whiteMountainView;
  21. return ThemeData(
  22. brightness: brightness,
  23. canvasColor: background,
  24. cardColor: background,
  25. dividerColor: divider,
  26. backgroundColor: background,
  27. primaryColor: accentColor,
  28. toggleableActiveColor: accentColor,
  29. errorColor: error,
  30. unselectedWidgetColor: hexToColor('#DADCDD'),
  31. dividerTheme: DividerThemeData(
  32. color: divider,
  33. space: 1,
  34. thickness: 1,
  35. ),
  36. cardTheme: CardTheme(
  37. color: cardBackground,
  38. margin: EdgeInsets.zero,
  39. clipBehavior: Clip.antiAliasWithSaveLayer,
  40. ),
  41. textSelectionTheme: TextSelectionThemeData(
  42. selectionColor: accentColor,
  43. selectionHandleColor: accentColor,
  44. cursorColor: accentColor,
  45. ),
  46. appBarTheme: AppBarTheme(
  47. color: cardBackground,
  48. titleTextStyle: TextStyle(
  49. color: secondaryText,
  50. fontSize: 18,
  51. ),
  52. iconTheme: IconThemeData(
  53. color: secondaryText,
  54. ),
  55. ),
  56. iconTheme: IconThemeData(
  57. color: secondaryText,
  58. size: 16.0,
  59. ),
  60. buttonTheme: ButtonThemeData(
  61. textTheme: ButtonTextTheme.primary,
  62. colorScheme: ColorScheme(
  63. brightness: brightness,
  64. primary: accentColor,
  65. secondary: accentColor,
  66. surface: background,
  67. background: background,
  68. error: error,
  69. onPrimary: buttonText,
  70. onSecondary: buttonText,
  71. onSurface: buttonText,
  72. onBackground: buttonText,
  73. onError: buttonText,
  74. ),
  75. padding: const EdgeInsets.all(16.0),
  76. ),
  77. cupertinoOverrideTheme: CupertinoThemeData(
  78. brightness: brightness,
  79. primaryColor: accentColor,
  80. ),
  81. inputDecorationTheme: InputDecorationTheme(
  82. errorStyle: TextStyle(color: error),
  83. labelStyle: TextStyle(
  84. fontFamily: 'Rubik',
  85. fontWeight: FontWeight.w600,
  86. fontSize: 16.0,
  87. color: primaryText.withOpacity(0.5),
  88. ),
  89. hintStyle: TextStyle(
  90. color: secondaryText,
  91. fontSize: 13.0,
  92. fontWeight: FontWeight.w300,
  93. ),
  94. ),
  95. textTheme: TextTheme(
  96. headline1: baseTextTheme.headline1!.copyWith(
  97. color: primaryText,
  98. fontSize: 34.0,
  99. fontWeight: FontWeight.bold,
  100. ),
  101. headline2: baseTextTheme.headline2!.copyWith(
  102. color: primaryText,
  103. fontSize: 22,
  104. fontWeight: FontWeight.bold,
  105. ),
  106. headline3: baseTextTheme.headline3!.copyWith(
  107. color: secondaryText,
  108. fontSize: 20,
  109. fontWeight: FontWeight.w600,
  110. ),
  111. headline4: baseTextTheme.headline4!.copyWith(
  112. color: primaryText,
  113. fontSize: 18,
  114. fontWeight: FontWeight.w600,
  115. ),
  116. headline5: baseTextTheme.headline5!.copyWith(
  117. color: primaryText,
  118. fontSize: 16,
  119. fontWeight: FontWeight.w700,
  120. ),
  121. headline6: baseTextTheme.headline6!.copyWith(
  122. color: primaryText,
  123. fontSize: 14,
  124. fontWeight: FontWeight.w700,
  125. ),
  126. bodyText1: baseTextTheme.bodyText1!.copyWith(
  127. color: secondaryText,
  128. fontSize: 15,
  129. ),
  130. bodyText2: baseTextTheme.bodyText2!.copyWith(
  131. color: primaryText,
  132. fontSize: 12,
  133. fontWeight: FontWeight.w400,
  134. ),
  135. button: baseTextTheme.button!.copyWith(
  136. color: primaryText,
  137. fontSize: 12.0,
  138. fontWeight: FontWeight.w700,
  139. ),
  140. caption: baseTextTheme.caption!.copyWith(
  141. color: primaryText,
  142. fontSize: 11.0,
  143. fontWeight: FontWeight.w300,
  144. ),
  145. overline: baseTextTheme.overline!.copyWith(
  146. color: secondaryText,
  147. fontSize: 11.0,
  148. fontWeight: FontWeight.w500,
  149. ),
  150. subtitle1: baseTextTheme.subtitle1!.copyWith(
  151. color: primaryText,
  152. fontSize: 16.0,
  153. fontWeight: FontWeight.w700,
  154. ),
  155. subtitle2: baseTextTheme.subtitle2!.copyWith(
  156. color: secondaryText,
  157. fontSize: 11.0,
  158. fontWeight: FontWeight.w500,
  159. ),
  160. ),
  161. );
  162. }
  163. static ThemeData get lightTheme => createTheme(
  164. brightness: Brightness.light,
  165. background: ColorConstants.lightScaffoldBackgroundColor,
  166. cardBackground: ColorConstants.secondaryAppColor,
  167. primaryText: Colors.black,
  168. secondaryText: Colors.black,
  169. accentColor: ColorConstants.secondaryAppColor,
  170. divider: ColorConstants.secondaryAppColor,
  171. buttonBackground: Colors.black38,
  172. buttonText: ColorConstants.secondaryAppColor,
  173. disabled: ColorConstants.secondaryAppColor,
  174. error: Colors.red,
  175. );
  176. static ThemeData get darkTheme => createTheme(
  177. brightness: Brightness.dark,
  178. background: ColorConstants.darkScaffoldBackgroundColor,
  179. cardBackground: ColorConstants.secondaryDarkAppColor,
  180. primaryText: Colors.white,
  181. secondaryText: Colors.white,
  182. accentColor: ColorConstants.secondaryDarkAppColor,
  183. divider: Colors.black45,
  184. buttonBackground: Colors.white,
  185. buttonText: ColorConstants.secondaryDarkAppColor,
  186. disabled: ColorConstants.secondaryDarkAppColor,
  187. error: Colors.red,
  188. );
  189. //亮色模式模式下的状态栏与导航栏设置 (默认黑色状态栏文本,白色导航栏)
  190. static SystemUiOverlayStyle get systemUiOverlayStyleLightThemeBlack {
  191. return SystemUiOverlayStyle.light.copyWith(
  192. statusBarColor: Colors.transparent,
  193. //顶部导航栏的状态栏背景颜色
  194. statusBarBrightness: Brightness.light,
  195. //顶部状态栏颜色模式
  196. statusBarIconBrightness: Brightness.dark,
  197. //顶部状态栏Icon与文本颜色模式
  198. systemNavigationBarDividerColor: Colors.transparent,
  199. //底部导航栏分割线颜色
  200. systemNavigationBarColor: Colors.white,
  201. //底部导航栏背景颜色
  202. systemNavigationBarIconBrightness: Brightness.dark, //底部导航栏三大金刚键的颜色模式
  203. );
  204. }
  205. //亮色模式模式下的状态栏与导航栏设置 (设置白色状态栏文本,白色导航栏)
  206. static SystemUiOverlayStyle get systemUiOverlayStyleLightThemeWhite {
  207. return SystemUiOverlayStyle.light.copyWith(
  208. statusBarColor: Colors.transparent,
  209. statusBarBrightness: Brightness.dark,
  210. statusBarIconBrightness: Brightness.light,
  211. systemNavigationBarDividerColor: Colors.transparent,
  212. systemNavigationBarColor: Colors.white,
  213. systemNavigationBarIconBrightness: Brightness.dark,
  214. );
  215. }
  216. //黑暗模式下,统一使用白色状态栏文本,黑色导航栏
  217. static SystemUiOverlayStyle get systemUiOverlayStyleDarkTheme {
  218. return SystemUiOverlayStyle.dark.copyWith(
  219. statusBarColor: Colors.transparent,
  220. statusBarBrightness: Brightness.dark,
  221. statusBarIconBrightness: Brightness.light,
  222. );
  223. }
  224. // ===================================== 切换样式 ↓ =====================================
  225. //强制更新整个App的样式
  226. static void _updateTheme(bool isDark) {
  227. Future.delayed(const Duration(milliseconds: 500), () {
  228. Get.forceAppUpdate();
  229. if (isDark) {
  230. if (GetPlatform.isAndroid) {
  231. //指定全局页面的状态栏,导航栏等设置
  232. SystemChrome.setSystemUIOverlayStyle(ThemeConfig.systemUiOverlayStyleDarkTheme);
  233. }
  234. } else {
  235. if (GetPlatform.isAndroid) {
  236. //指定全局页面的状态栏,导航栏等设置
  237. SystemChrome.setSystemUIOverlayStyle(ThemeConfig.systemUiOverlayStyleLightThemeBlack);
  238. }
  239. }
  240. });
  241. }
  242. static void changeThemeLight() {
  243. //如果是深色模式才切换亮色模式
  244. Get.changeThemeMode(ThemeMode.light);
  245. Get.changeTheme(ThemeConfig.lightTheme);
  246. _updateTheme(false);
  247. }
  248. static void changeThemeDark() {
  249. //如果是亮色模式才切换深色模式
  250. Get.changeThemeMode(ThemeMode.dark);
  251. Get.changeTheme(ThemeConfig.darkTheme);
  252. _updateTheme(true);
  253. }
  254. }