fresh_blue.dart 7.0 KB

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