style.dart 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. import 'package:flutter/material.dart';
  2. import 'package:tuple/tuple.dart';
  3. class EditorStyles extends InheritedWidget {
  4. const EditorStyles({
  5. required this.data,
  6. required Widget child,
  7. Key? key,
  8. }) : super(key: key, child: child);
  9. final DefaultStyles data;
  10. @override
  11. bool updateShouldNotify(covariant EditorStyles oldWidget) =>
  12. data != oldWidget.data;
  13. static DefaultStyles? getStyles(BuildContext context, bool nullOk) {
  14. final widget = context.dependOnInheritedWidgetOfExactType<EditorStyles>();
  15. if (widget == null && nullOk) {
  16. return null;
  17. }
  18. assert(widget != null);
  19. return widget!.data;
  20. }
  21. }
  22. class DefaultTextBlockStyle {
  23. DefaultTextBlockStyle(
  24. this.style,
  25. this.verticalSpacing,
  26. this.lineSpacing,
  27. this.decoration,
  28. );
  29. final TextStyle style;
  30. final Tuple2<double, double> verticalSpacing;
  31. final Tuple2<double, double> lineSpacing;
  32. final BoxDecoration? decoration;
  33. }
  34. class DefaultStyles {
  35. DefaultStyles({
  36. this.h1,
  37. this.h2,
  38. this.h3,
  39. this.h4,
  40. this.h5,
  41. this.h6,
  42. this.paragraph,
  43. this.bold,
  44. this.italic,
  45. this.underline,
  46. this.strikeThrough,
  47. this.sizeSmall,
  48. this.sizeLarge,
  49. this.sizeHuge,
  50. this.link,
  51. this.color,
  52. this.placeHolder,
  53. this.lists,
  54. this.quote,
  55. this.code,
  56. this.indent,
  57. this.align,
  58. this.leading,
  59. });
  60. final DefaultTextBlockStyle? h1;
  61. final DefaultTextBlockStyle? h2;
  62. final DefaultTextBlockStyle? h3;
  63. final DefaultTextBlockStyle? h4;
  64. final DefaultTextBlockStyle? h5;
  65. final DefaultTextBlockStyle? h6;
  66. final DefaultTextBlockStyle? paragraph;
  67. final TextStyle? bold;
  68. final TextStyle? italic;
  69. final TextStyle? underline;
  70. final TextStyle? strikeThrough;
  71. final TextStyle? sizeSmall;
  72. final TextStyle? sizeLarge;
  73. final TextStyle? sizeHuge;
  74. final TextStyle? link;
  75. final Color? color;
  76. final DefaultTextBlockStyle? placeHolder;
  77. final DefaultTextBlockStyle? lists;
  78. final DefaultTextBlockStyle? quote;
  79. final DefaultTextBlockStyle? code;
  80. final DefaultTextBlockStyle? indent;
  81. final DefaultTextBlockStyle? align;
  82. final DefaultTextBlockStyle? leading;
  83. static DefaultStyles getInstance(BuildContext context) {
  84. final themeData = Theme.of(context);
  85. final defaultTextStyle = DefaultTextStyle.of(context);
  86. final baseStyle = defaultTextStyle.style.copyWith(
  87. fontSize: 16,
  88. height: 1.3,
  89. );
  90. const baseSpacing = Tuple2<double, double>(6, 0);
  91. String fontFamily;
  92. switch (themeData.platform) {
  93. case TargetPlatform.iOS:
  94. case TargetPlatform.macOS:
  95. fontFamily = 'Menlo';
  96. break;
  97. case TargetPlatform.android:
  98. case TargetPlatform.fuchsia:
  99. case TargetPlatform.windows:
  100. case TargetPlatform.linux:
  101. fontFamily = 'Roboto Mono';
  102. break;
  103. default:
  104. throw UnimplementedError();
  105. }
  106. final defaultSmallHeaderTextStyle = DefaultTextBlockStyle(
  107. defaultTextStyle.style.copyWith(
  108. fontSize: 16,
  109. color: defaultTextStyle.style.color!.withOpacity(0.70),
  110. height: 1.25,
  111. fontWeight: FontWeight.w500,
  112. ),
  113. const Tuple2(8, 0),
  114. const Tuple2(0, 0),
  115. null,
  116. );
  117. return DefaultStyles(
  118. h1: DefaultTextBlockStyle(
  119. defaultTextStyle.style.copyWith(
  120. fontSize: 34,
  121. color: defaultTextStyle.style.color!.withOpacity(0.70),
  122. height: 1.15,
  123. fontWeight: FontWeight.w300,
  124. ),
  125. const Tuple2(16, 0),
  126. const Tuple2(0, 0),
  127. null,
  128. ),
  129. h2: DefaultTextBlockStyle(
  130. defaultTextStyle.style.copyWith(
  131. fontSize: 24,
  132. color: defaultTextStyle.style.color!.withOpacity(0.70),
  133. height: 1.15,
  134. fontWeight: FontWeight.normal,
  135. ),
  136. const Tuple2(8, 0),
  137. const Tuple2(0, 0),
  138. null,
  139. ),
  140. h3: DefaultTextBlockStyle(
  141. defaultTextStyle.style.copyWith(
  142. fontSize: 20,
  143. color: defaultTextStyle.style.color!.withOpacity(0.70),
  144. height: 1.25,
  145. fontWeight: FontWeight.w500,
  146. ),
  147. const Tuple2(8, 0),
  148. const Tuple2(0, 0),
  149. null,
  150. ),
  151. h4: defaultSmallHeaderTextStyle,
  152. h5: defaultSmallHeaderTextStyle,
  153. h6: defaultSmallHeaderTextStyle,
  154. paragraph: DefaultTextBlockStyle(
  155. baseStyle,
  156. const Tuple2(0, 0),
  157. const Tuple2(0, 0),
  158. null,
  159. ),
  160. bold: const TextStyle(
  161. fontWeight: FontWeight.bold,
  162. ),
  163. italic: const TextStyle(
  164. fontStyle: FontStyle.italic,
  165. ),
  166. underline: const TextStyle(
  167. decoration: TextDecoration.underline,
  168. ),
  169. strikeThrough: const TextStyle(
  170. decoration: TextDecoration.lineThrough,
  171. ),
  172. link: TextStyle(
  173. color: themeData.colorScheme.secondary,
  174. decoration: TextDecoration.underline,
  175. ),
  176. placeHolder: DefaultTextBlockStyle(
  177. defaultTextStyle.style.copyWith(
  178. fontSize: 20,
  179. height: 1.5,
  180. color: Colors.grey.withOpacity(0.6),
  181. ),
  182. const Tuple2(0, 0),
  183. const Tuple2(0, 0),
  184. null,
  185. ),
  186. lists: DefaultTextBlockStyle(
  187. baseStyle,
  188. baseSpacing,
  189. const Tuple2(0, 6),
  190. null,
  191. ),
  192. quote: DefaultTextBlockStyle(
  193. TextStyle(color: baseStyle.color!.withOpacity(0.6)),
  194. baseSpacing,
  195. const Tuple2(6, 2),
  196. BoxDecoration(
  197. border: Border(
  198. left: BorderSide(width: 4, color: Colors.grey.shade300),
  199. ),
  200. ),
  201. ),
  202. code: DefaultTextBlockStyle(
  203. TextStyle(
  204. color: Colors.blue.shade900.withOpacity(0.9),
  205. fontFamily: fontFamily,
  206. fontSize: 13,
  207. height: 1.15,
  208. ),
  209. baseSpacing,
  210. const Tuple2(0, 0),
  211. BoxDecoration(
  212. color: Colors.grey.shade50,
  213. borderRadius: BorderRadius.circular(2),
  214. ),
  215. ),
  216. indent: DefaultTextBlockStyle(
  217. baseStyle,
  218. baseSpacing,
  219. const Tuple2(0, 6),
  220. null,
  221. ),
  222. align: DefaultTextBlockStyle(
  223. baseStyle,
  224. const Tuple2(0, 0),
  225. const Tuple2(0, 0),
  226. null,
  227. ),
  228. leading: DefaultTextBlockStyle(
  229. baseStyle,
  230. const Tuple2(0, 0),
  231. const Tuple2(0, 0),
  232. null,
  233. ),
  234. sizeSmall: const TextStyle(
  235. fontSize: 10,
  236. ),
  237. sizeLarge: const TextStyle(
  238. fontSize: 18,
  239. ),
  240. sizeHuge: const TextStyle(
  241. fontSize: 22,
  242. ),
  243. );
  244. }
  245. DefaultStyles merge(DefaultStyles other) {
  246. return DefaultStyles(
  247. h1: other.h1 ?? h1,
  248. h2: other.h2 ?? h2,
  249. h3: other.h3 ?? h3,
  250. h4: other.h4 ?? h4,
  251. h5: other.h5 ?? h5,
  252. h6: other.h6 ?? h6,
  253. paragraph: other.paragraph ?? paragraph,
  254. bold: other.bold ?? bold,
  255. italic: other.italic ?? italic,
  256. underline: other.underline ?? underline,
  257. strikeThrough: other.strikeThrough ?? strikeThrough,
  258. link: other.link ?? link,
  259. color: other.color ?? color,
  260. placeHolder: other.placeHolder ?? placeHolder,
  261. lists: other.lists ?? lists,
  262. quote: other.quote ?? quote,
  263. code: other.code ?? code,
  264. indent: other.indent ?? indent,
  265. align: other.align ?? align,
  266. leading: other.leading ?? leading,
  267. sizeSmall: other.sizeSmall ?? sizeSmall,
  268. sizeLarge: other.sizeLarge ?? sizeLarge,
  269. sizeHuge: other.sizeHuge ?? sizeHuge,
  270. );
  271. }
  272. }