theme.dart 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. import 'package:flowy_infra/size.dart';
  2. import 'package:flowy_infra/text_style.dart';
  3. import 'package:flutter/material.dart';
  4. import 'color_extension.dart';
  5. Brightness themeTypeFromString(String name) {
  6. Brightness themeType = Brightness.light;
  7. if (name == "dark") {
  8. themeType = Brightness.dark;
  9. }
  10. return themeType;
  11. }
  12. String themeTypeToString(Brightness brightness) {
  13. switch (brightness) {
  14. case Brightness.light:
  15. return "light";
  16. case Brightness.dark:
  17. return "dark";
  18. }
  19. }
  20. // Color Palettes
  21. const _black = Color(0xff000000);
  22. const _white = Color(0xFFFFFFFF);
  23. class AppTheme {
  24. Brightness brightness;
  25. late Color surface;
  26. late Color hover;
  27. late Color selector;
  28. late Color red;
  29. late Color yellow;
  30. late Color green;
  31. late Color shader1;
  32. late Color shader2;
  33. late Color shader3;
  34. late Color shader4;
  35. late Color shader5;
  36. late Color shader6;
  37. late Color shader7;
  38. late Color bg1;
  39. late Color bg2;
  40. late Color bg3;
  41. late Color bg4;
  42. late Color tint1;
  43. late Color tint2;
  44. late Color tint3;
  45. late Color tint4;
  46. late Color tint5;
  47. late Color tint6;
  48. late Color tint7;
  49. late Color tint8;
  50. late Color tint9;
  51. late Color textColor;
  52. late Color iconColor;
  53. late Color disableIconColor;
  54. late Color main1;
  55. late Color main2;
  56. late Color shadow;
  57. late String font;
  58. late String monospaceFont;
  59. /// Default constructor
  60. AppTheme({this.brightness = Brightness.light});
  61. factory AppTheme.fromName({
  62. required String themeName,
  63. required String font,
  64. required String monospaceFont,
  65. }) {
  66. switch (themeTypeFromString(themeName)) {
  67. case Brightness.light:
  68. return AppTheme(brightness: Brightness.light)
  69. ..surface = Colors.white
  70. ..hover = const Color(0xFFe0f8ff)
  71. ..selector = const Color(0xfff2fcff)
  72. ..red = const Color(0xfffb006d)
  73. ..yellow = const Color(0xffffd667)
  74. ..green = const Color(0xff66cf80)
  75. ..shader1 = const Color(0xff333333)
  76. ..shader2 = const Color(0xff4f4f4f)
  77. ..shader3 = const Color(0xff828282)
  78. ..shader4 = const Color(0xffbdbdbd)
  79. ..shader5 = const Color(0xffe0e0e0)
  80. ..shader6 = const Color(0xfff2f2f2)
  81. ..shader7 = const Color(0xffffffff)
  82. ..bg1 = const Color(0xfff7f8fc)
  83. ..bg2 = const Color(0xffedeef2)
  84. ..bg3 = const Color(0xffe2e4eb)
  85. ..bg4 = const Color(0xff2c144b)
  86. ..tint1 = const Color(0xffe8e0ff)
  87. ..tint2 = const Color(0xffffe7fd)
  88. ..tint3 = const Color(0xffffe7ee)
  89. ..tint4 = const Color(0xffffefe3)
  90. ..tint5 = const Color(0xfffff2cd)
  91. ..tint6 = const Color(0xfff5ffdc)
  92. ..tint7 = const Color(0xffddffd6)
  93. ..tint8 = const Color(0xffdefff1)
  94. ..tint9 = const Color(0xffe1fbff)
  95. ..main1 = const Color(0xff00bcf0)
  96. ..main2 = const Color(0xff00b7ea)
  97. ..textColor = _black
  98. ..iconColor = _black
  99. ..shadow = _black
  100. ..disableIconColor = const Color(0xffbdbdbd)
  101. ..font = font
  102. ..monospaceFont = monospaceFont;
  103. case Brightness.dark:
  104. return AppTheme(brightness: Brightness.dark)
  105. ..surface = const Color(0xff292929)
  106. ..hover = const Color(0xff1f1f1f)
  107. ..selector = const Color(0xff333333)
  108. ..red = const Color(0xfffb006d)
  109. ..yellow = const Color(0xffffd667)
  110. ..green = const Color(0xff66cf80)
  111. ..shader1 = _white
  112. ..shader2 = const Color(0xffffffff)
  113. ..shader3 = const Color(0xff828282)
  114. ..shader4 = const Color(0xffbdbdbd)
  115. ..shader5 = _white
  116. ..shader6 = _black
  117. ..shader7 = _black
  118. ..bg1 = _black
  119. ..bg2 = _black
  120. ..bg3 = const Color(0xff4f4f4f)
  121. ..bg4 = const Color(0xff2c144b)
  122. ..tint1 = const Color(0xffc3adff)
  123. ..tint2 = const Color(0xffffadf9)
  124. ..tint3 = const Color(0xffffadad)
  125. ..tint4 = const Color(0xffffcfad)
  126. ..tint5 = const Color(0xfffffead)
  127. ..tint6 = const Color(0xffe6ffa3)
  128. ..tint7 = const Color(0xffbcffad)
  129. ..tint8 = const Color(0xffadffe2)
  130. ..tint9 = const Color(0xffade4ff)
  131. ..main1 = const Color(0xff00bcf0)
  132. ..main2 = const Color(0xff009cc7)
  133. ..textColor = _white
  134. ..iconColor = _white
  135. ..shadow = _black
  136. ..disableIconColor = const Color(0xff333333)
  137. ..font = font
  138. ..monospaceFont = monospaceFont;
  139. }
  140. }
  141. ThemeData getThemeData(Locale locale) {
  142. // Poppins and SF Mono are not well supported in some languages, so use the
  143. // built-in font for the following languages.
  144. final useBuiltInFontLanguages = [
  145. const Locale('zh', 'CN'),
  146. const Locale('zh', 'TW'),
  147. ];
  148. TextStyles textTheme;
  149. if (useBuiltInFontLanguages.contains(locale)) {
  150. textTheme = TextStyles(font: '', color: shader1);
  151. } else {
  152. textTheme = TextStyles(font: font, color: shader1);
  153. }
  154. return ThemeData(
  155. brightness: brightness,
  156. textTheme: textTheme.generateTextTheme(),
  157. textSelectionTheme: TextSelectionThemeData(
  158. cursorColor: main2,
  159. selectionHandleColor: main2,
  160. ),
  161. primaryIconTheme: IconThemeData(color: hover),
  162. iconTheme: IconThemeData(color: shader1),
  163. scrollbarTheme: ScrollbarThemeData(
  164. thumbColor: MaterialStateProperty.all(Colors.transparent),
  165. ),
  166. materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
  167. canvasColor: shader6,
  168. dividerColor: shader6,
  169. hintColor: shader3,
  170. disabledColor: shader4,
  171. highlightColor: main1,
  172. indicatorColor: main1,
  173. toggleableActiveColor: main1,
  174. colorScheme: ColorScheme(
  175. brightness: brightness,
  176. primary: main1,
  177. onPrimary: _white,
  178. primaryContainer: main2,
  179. onPrimaryContainer: _white,
  180. secondary: hover,
  181. onSecondary: shader1,
  182. secondaryContainer: selector,
  183. onSecondaryContainer: shader1,
  184. background: surface,
  185. onBackground: shader1,
  186. surface: surface,
  187. onSurface: shader1,
  188. onError: shader7,
  189. error: red,
  190. outline: shader4,
  191. surfaceVariant: bg1,
  192. shadow: shadow,
  193. ),
  194. extensions: [
  195. AFThemeExtension(
  196. warning: yellow,
  197. success: green,
  198. tint1: tint1,
  199. tint2: tint2,
  200. tint3: tint3,
  201. tint4: tint4,
  202. tint5: tint5,
  203. tint6: tint6,
  204. tint7: tint7,
  205. tint8: tint8,
  206. tint9: tint9,
  207. greyHover: bg2,
  208. greySelect: bg3,
  209. lightGreyHover: shader6,
  210. toggleOffFill: shader5,
  211. code: textTheme.getFontStyle(fontFamily: monospaceFont),
  212. callout: textTheme.getFontStyle(
  213. fontSize: FontSizes.s11,
  214. fontColor: shader3,
  215. ),
  216. caption: textTheme.getFontStyle(
  217. fontSize: FontSizes.s11,
  218. fontWeight: FontWeight.w400,
  219. fontColor: shader3,
  220. ),
  221. )
  222. ],
  223. );
  224. }
  225. Color shift(Color c, double d) =>
  226. ColorUtils.shiftHsl(c, d * (brightness == Brightness.dark ? -1 : 1));
  227. }
  228. class ColorUtils {
  229. static Color shiftHsl(Color c, [double amt = 0]) {
  230. var hslc = HSLColor.fromColor(c);
  231. return hslc.withLightness((hslc.lightness + amt).clamp(0.0, 1.0)).toColor();
  232. }
  233. static Color parseHex(String value) =>
  234. Color(int.parse(value.substring(1, 7), radix: 16) + 0xFF000000);
  235. static Color blend(Color dst, Color src, double opacity) {
  236. return Color.fromARGB(
  237. 255,
  238. (dst.red.toDouble() * (1.0 - opacity) + src.red.toDouble() * opacity)
  239. .toInt(),
  240. (dst.green.toDouble() * (1.0 - opacity) + src.green.toDouble() * opacity)
  241. .toInt(),
  242. (dst.blue.toDouble() * (1.0 - opacity) + src.blue.toDouble() * opacity)
  243. .toInt(),
  244. );
  245. }
  246. }