dark_theme.dart 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import 'package:flutter/painting.dart';
  2. import 'package:o2_flutter/common/models/mindmap/mind_node.dart';
  3. import 'package:o2_flutter/o2.dart';
  4. import 'mind_map_theme.dart';
  5. ///
  6. /// 黑暗主题
  7. ///
  8. class DarkTheme extends BaseTheme {
  9. DarkTheme(): super('dark-theme', 66.0, 44.0) {
  10. //暗黑背景色
  11. canvasBackgroundColor = o2Dark;
  12. // 线条颜色
  13. lineColor = const Color.fromARGB(255, 217, 217, 217);
  14. //文字样式
  15. rootTextStyle = TextStyle(
  16. color: rootTextColor,
  17. fontSize: rootFontSize,
  18. fontWeight: FontWeight.bold,
  19. );
  20. secondTextStyle = TextStyle(
  21. color: secondTextColor,
  22. fontSize: secondFontSize
  23. );
  24. nodeTextStyle = TextStyle(
  25. color: nodeTextColor,
  26. fontSize: nodeFontSize
  27. );
  28. }
  29. final scale = 1.0;
  30. // 中心节点
  31. final _rectCircularBase = 5.0;//圆角
  32. double get rectCircular => _rectCircularBase * scale;//圆角
  33. // 中心节点填充颜色
  34. Color rootRectColor = const Color.fromARGB(255, 89, 149, 247);
  35. // 中心节点文字颜色
  36. Color rootTextColor = const Color(0xFFFFFFFF);
  37. // 中心节点文字样式
  38. late TextStyle rootTextStyle;
  39. // 二级节点填充颜色
  40. Color secondRectColor = const Color.fromARGB(255, 255, 255, 255);
  41. // 二级节点文字颜色
  42. Color secondTextColor = const Color.fromARGB(255, 68, 68, 68);
  43. // 节点文字样式
  44. late TextStyle secondTextStyle;
  45. // 节点文字颜色
  46. Color nodeTextColor = const Color.fromARGB(255, 251, 251, 251);
  47. // 节点文字样式
  48. late TextStyle nodeTextStyle;
  49. @override
  50. NodePaintElement calElementSize(Node root) {
  51. var data = root.data;
  52. NodePaintElement rootPaint = sizeNode(data, NodePaintElement.rootLevel);
  53. var children = root.children;
  54. var list = <NodePaintElement>[];
  55. for(var node in children) {
  56. var child = recursiveNode(node, NodePaintElement.rootLevel + 1);
  57. list.add(child);
  58. }
  59. rootPaint.children = list;
  60. return rootPaint;
  61. }
  62. ///
  63. /// 递归Node
  64. ///
  65. NodePaintElement recursiveNode(Node node, int level) {
  66. var data = node.data;
  67. NodePaintElement nodePaint = sizeNode(data, level);
  68. var children = node.children;
  69. if(children.isNotEmpty) {
  70. var list = <NodePaintElement>[];
  71. for(var child in children) {
  72. var ret = (recursiveNode(child, level + 1)); // children
  73. list.add(ret);
  74. }
  75. nodePaint.children = list;
  76. }
  77. return nodePaint;
  78. }
  79. ///
  80. /// Node内部元素 计算
  81. ///
  82. @override
  83. NodePaintElement sizeNode(NodeData data, int level) {
  84. var elements = Map<NodeElement, PaintElement>();
  85. var nodeWidth = 0.0;
  86. var nodeHeight = 0.0;
  87. ///文字
  88. ///
  89. if (level == 0) { //root
  90. var textPainter = TextPainter(
  91. text: TextSpan(style: rootTextStyle, text: data.text),
  92. textDirection: TextDirection.ltr,
  93. textAlign: TextAlign.center)
  94. ..layout();
  95. elements[NodeElement.text] = TextPaintElement(textPainter);
  96. nodeHeight += textPainter.height;
  97. nodeWidth += textPainter.width;
  98. }else if (level == 1) { //
  99. var textPainter = TextPainter(
  100. text: TextSpan(style: secondTextStyle, text: data.text),
  101. textDirection: TextDirection.ltr,
  102. textAlign: TextAlign.center)
  103. ..layout();
  104. elements[NodeElement.text] = TextPaintElement(textPainter);
  105. nodeHeight += textPainter.height;
  106. nodeWidth += textPainter.width;
  107. }else {
  108. var textPainter = TextPainter(
  109. text: TextSpan(style: nodeTextStyle, text: data.text),
  110. textDirection: TextDirection.ltr,
  111. textAlign: TextAlign.center)
  112. ..layout();
  113. elements[NodeElement.text] = TextPaintElement(textPainter);
  114. nodeHeight += textPainter.height;
  115. nodeWidth += textPainter.width;
  116. }
  117. ///
  118. /// 进度
  119. ///
  120. if(data.progress != null && data.progress! > 0) {
  121. Rect rect = Rect.fromLTWH(0.0, 0.0, progressIconSize, progressIconSize);
  122. PaintStyle style = PaintStyle(color: rootTextColor, style: PaintingStyle.fill);
  123. elements[NodeElement.progress] = ImagePaintElement(rect, style);
  124. nodeHeight = nodeHeight > progressIconSize ? nodeHeight : progressIconSize;
  125. nodeWidth += progressIconSize + elementGap;
  126. }
  127. ///
  128. /// 优先级
  129. ///
  130. if(data.priority != null && data.priority! > 0) {
  131. Rect rect = Rect.fromLTWH(0.0, 0.0, priorityIconSize, priorityIconSize);
  132. PaintStyle style = PaintStyle(color: rootTextColor, style: PaintingStyle.fill);
  133. elements[NodeElement.priority] = ImagePaintElement(rect, style);
  134. nodeHeight = nodeHeight > priorityIconSize ? nodeHeight : priorityIconSize;
  135. nodeWidth += priorityIconSize + elementGap;
  136. }
  137. ///
  138. /// 超链接
  139. ///
  140. if(data.hyperlink != null ) {
  141. Rect rect = Rect.fromLTWH(0.0, 0.0, linkIconSize, linkIconSize);
  142. PaintStyle style = PaintStyle(color: rootTextColor, style: PaintingStyle.fill);
  143. elements[NodeElement.hyperlink] = ImagePaintElement(rect, style);
  144. nodeHeight = nodeHeight > linkIconSize ? nodeHeight : linkIconSize;
  145. nodeWidth += linkIconSize + elementGap;
  146. }
  147. ///
  148. /// 图片
  149. ///
  150. if( (data.image != null && data.image!.isNotEmpty) || (data.imageId != null && data.imageId!.isNotEmpty)) {
  151. var size = data.imageSize ?? ImageSize(width: 40, height: 40);
  152. Rect rect = Rect.fromLTWH(0.0, 0.0, size.width.toDouble(), size.height.toDouble());
  153. PaintStyle style = PaintStyle(color: rootTextColor, style: PaintingStyle.fill);
  154. elements[NodeElement.image] = ImagePaintElement(rect, style);
  155. nodeHeight += size.height.toDouble() + elementGap;
  156. nodeWidth = size.width > nodeWidth ? size.width.toDouble() : nodeWidth;
  157. }
  158. ///
  159. /// 节点外框
  160. ///
  161. if (level == 0) { // root没有设置id
  162. data.id = NodePaintElement.rootId;
  163. nodeWidth += rootRectPadding*2;
  164. nodeHeight += rootRectPadding*2;
  165. Rect rect = Rect.fromLTWH(0.0, 0.0, nodeWidth, nodeHeight);
  166. PaintStyle style = PaintStyle(color: rootRectColor, style: PaintingStyle.fill);
  167. elements[NodeElement.background] = RRectPaintElement(RRect.fromRectAndRadius(rect, Radius.circular(rectCircular)), style);
  168. }else if(level == 1) { // 二级
  169. nodeWidth += secondRectPadding*2;
  170. nodeHeight += secondRectPadding*2;
  171. Rect rect = Rect.fromLTWH(0.0, 0.0, nodeWidth, nodeHeight);
  172. PaintStyle style = PaintStyle(color: secondRectColor, style: PaintingStyle.fill);
  173. elements[NodeElement.background] = RRectPaintElement(RRect.fromRectAndRadius(rect, Radius.circular(rectCircular)), style);
  174. }else {
  175. nodeWidth += nodeRectPadding*2;
  176. nodeHeight += nodeRectPadding*2;
  177. }
  178. return NodePaintElement(
  179. level: level,
  180. data:data,
  181. paintElements:elements,
  182. nodeSize:Size(nodeWidth, nodeHeight)
  183. );
  184. }
  185. }