theme_extension.dart 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import 'package:flutter/material.dart';
  2. @immutable
  3. class AFThemeExtension extends ThemeExtension<AFThemeExtension> {
  4. final Color? warning;
  5. final Color? success;
  6. final Color tint1;
  7. final Color tint2;
  8. final Color tint3;
  9. final Color tint4;
  10. final Color tint5;
  11. final Color tint6;
  12. final Color tint7;
  13. final Color tint8;
  14. final Color tint9;
  15. final Color textColor;
  16. final Color greyHover;
  17. final Color greySelect;
  18. final Color lightGreyHover;
  19. final Color toggleOffFill;
  20. final Color progressBarBGcolor;
  21. final TextStyle code;
  22. final TextStyle callout;
  23. final TextStyle caption;
  24. const AFThemeExtension({
  25. required this.warning,
  26. required this.success,
  27. required this.tint1,
  28. required this.tint2,
  29. required this.tint3,
  30. required this.tint4,
  31. required this.tint5,
  32. required this.tint6,
  33. required this.tint7,
  34. required this.tint8,
  35. required this.tint9,
  36. required this.greyHover,
  37. required this.greySelect,
  38. required this.lightGreyHover,
  39. required this.toggleOffFill,
  40. required this.textColor,
  41. required this.code,
  42. required this.callout,
  43. required this.caption,
  44. required this.progressBarBGcolor,
  45. });
  46. static AFThemeExtension of(BuildContext context) {
  47. return Theme.of(context).extension<AFThemeExtension>()!;
  48. }
  49. @override
  50. AFThemeExtension copyWith({
  51. Color? warning,
  52. Color? success,
  53. Color? tint1,
  54. Color? tint2,
  55. Color? tint3,
  56. Color? tint4,
  57. Color? tint5,
  58. Color? tint6,
  59. Color? tint7,
  60. Color? tint8,
  61. Color? tint9,
  62. Color? textColor,
  63. Color? greyHover,
  64. Color? greySelect,
  65. Color? lightGreyHover,
  66. Color? toggleOffFill,
  67. Color? progressBarBGcolor,
  68. TextStyle? code,
  69. TextStyle? callout,
  70. TextStyle? caption,
  71. }) {
  72. return AFThemeExtension(
  73. warning: warning ?? this.warning,
  74. success: success ?? this.success,
  75. tint1: tint1 ?? this.tint1,
  76. tint2: tint2 ?? this.tint2,
  77. tint3: tint3 ?? this.tint3,
  78. tint4: tint4 ?? this.tint4,
  79. tint5: tint5 ?? this.tint5,
  80. tint6: tint6 ?? this.tint6,
  81. tint7: tint7 ?? this.tint7,
  82. tint8: tint8 ?? this.tint8,
  83. tint9: tint9 ?? this.tint9,
  84. textColor: textColor ?? this.textColor,
  85. greyHover: greyHover ?? this.greyHover,
  86. greySelect: greySelect ?? this.greySelect,
  87. lightGreyHover: lightGreyHover ?? this.lightGreyHover,
  88. toggleOffFill: toggleOffFill ?? this.toggleOffFill,
  89. progressBarBGcolor: progressBarBGcolor ?? this.progressBarBGcolor,
  90. code: code ?? this.code,
  91. callout: callout ?? this.callout,
  92. caption: caption ?? this.caption,
  93. );
  94. }
  95. @override
  96. ThemeExtension<AFThemeExtension> lerp(
  97. ThemeExtension<AFThemeExtension>? other, double t) {
  98. if (other is! AFThemeExtension) {
  99. return this;
  100. }
  101. return AFThemeExtension(
  102. warning: Color.lerp(warning, other.warning, t),
  103. success: Color.lerp(success, other.success, t),
  104. tint1: Color.lerp(tint1, other.tint1, t)!,
  105. tint2: Color.lerp(tint2, other.tint2, t)!,
  106. tint3: Color.lerp(tint3, other.tint3, t)!,
  107. tint4: Color.lerp(tint4, other.tint4, t)!,
  108. tint5: Color.lerp(tint5, other.tint5, t)!,
  109. tint6: Color.lerp(tint6, other.tint6, t)!,
  110. tint7: Color.lerp(tint7, other.tint7, t)!,
  111. tint8: Color.lerp(tint8, other.tint8, t)!,
  112. tint9: Color.lerp(tint9, other.tint9, t)!,
  113. textColor: Color.lerp(textColor, other.textColor, t)!,
  114. greyHover: Color.lerp(greyHover, other.greyHover, t)!,
  115. greySelect: Color.lerp(greySelect, other.greySelect, t)!,
  116. lightGreyHover: Color.lerp(lightGreyHover, other.lightGreyHover, t)!,
  117. toggleOffFill: Color.lerp(toggleOffFill, other.toggleOffFill, t)!,
  118. progressBarBGcolor:
  119. Color.lerp(progressBarBGcolor, other.progressBarBGcolor, t)!,
  120. code: other.code,
  121. callout: other.callout,
  122. caption: other.caption,
  123. );
  124. }
  125. }
  126. enum FlowyTint {
  127. tint1,
  128. tint2,
  129. tint3,
  130. tint4,
  131. tint5,
  132. tint6,
  133. tint7,
  134. tint8,
  135. tint9;
  136. String toJson() => name;
  137. static FlowyTint fromJson(String json) {
  138. try {
  139. return FlowyTint.values.byName(json);
  140. } catch (_) {
  141. return FlowyTint.tint1;
  142. }
  143. }
  144. Color color(BuildContext context) {
  145. switch (this) {
  146. case FlowyTint.tint1:
  147. return AFThemeExtension.of(context).tint1;
  148. case FlowyTint.tint2:
  149. return AFThemeExtension.of(context).tint2;
  150. case FlowyTint.tint3:
  151. return AFThemeExtension.of(context).tint3;
  152. case FlowyTint.tint4:
  153. return AFThemeExtension.of(context).tint4;
  154. case FlowyTint.tint5:
  155. return AFThemeExtension.of(context).tint5;
  156. case FlowyTint.tint6:
  157. return AFThemeExtension.of(context).tint6;
  158. case FlowyTint.tint7:
  159. return AFThemeExtension.of(context).tint7;
  160. case FlowyTint.tint8:
  161. return AFThemeExtension.of(context).tint8;
  162. case FlowyTint.tint9:
  163. return AFThemeExtension.of(context).tint9;
  164. }
  165. }
  166. }