graph.dart 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import 'package:flutter/material.dart';
  2. /// 图节点模型
  3. class GraphNode {
  4. final String id;
  5. final String label;
  6. final NodeType type;
  7. final dynamic data;
  8. final Offset position;
  9. GraphNode({
  10. required this.id,
  11. required this.label,
  12. required this.type,
  13. this.data,
  14. required this.position,
  15. });
  16. /// 从JSON创建
  17. factory GraphNode.fromJson(Map<String, dynamic> json) {
  18. return GraphNode(
  19. id: json['id'] as String,
  20. label: json['label'] as String,
  21. type: NodeType.values.firstWhere(
  22. (e) => e.toString().split('.').last == json['type'],
  23. orElse: () => NodeType.element,
  24. ),
  25. data: json['data'],
  26. position: Offset(
  27. (json['position'] as Map)['x'] as double,
  28. (json['position'] as Map)['y'] as double,
  29. ),
  30. );
  31. }
  32. /// 转换为JSON
  33. Map<String, dynamic> toJson() {
  34. return {
  35. 'id': id,
  36. 'label': label,
  37. 'type': type.toString().split('.').last,
  38. 'data': data,
  39. 'position': {'x': position.dx, 'y': position.dy},
  40. };
  41. }
  42. /// 复制并更新
  43. GraphNode copyWith({
  44. String? id,
  45. String? label,
  46. NodeType? type,
  47. dynamic data,
  48. Offset? position,
  49. }) {
  50. return GraphNode(
  51. id: id ?? this.id,
  52. label: label ?? this.label,
  53. type: type ?? this.type,
  54. data: data ?? this.data,
  55. position: position ?? this.position,
  56. );
  57. }
  58. }
  59. /// 图边模型
  60. class GraphEdge {
  61. final String id;
  62. final String source;
  63. final String target;
  64. final EdgeType type;
  65. final String? label;
  66. GraphEdge({
  67. required this.id,
  68. required this.source,
  69. required this.target,
  70. required this.type,
  71. this.label,
  72. });
  73. /// 从JSON创建
  74. factory GraphEdge.fromJson(Map<String, dynamic> json) {
  75. return GraphEdge(
  76. id: json['id'] as String,
  77. source: json['source'] as String,
  78. target: json['target'] as String,
  79. type: EdgeType.values.firstWhere(
  80. (e) => e.toString().split('.').last == json['type'],
  81. orElse: () => EdgeType.reference,
  82. ),
  83. label: json['label'] as String?,
  84. );
  85. }
  86. /// 转换为JSON
  87. Map<String, dynamic> toJson() {
  88. return {
  89. 'id': id,
  90. 'source': source,
  91. 'target': target,
  92. 'type': type.toString().split('.').last,
  93. 'label': label,
  94. };
  95. }
  96. /// 复制并更新
  97. GraphEdge copyWith({
  98. String? id,
  99. String? source,
  100. String? target,
  101. EdgeType? type,
  102. String? label,
  103. }) {
  104. return GraphEdge(
  105. id: id ?? this.id,
  106. source: source ?? this.source,
  107. target: target ?? this.target,
  108. type: type ?? this.type,
  109. label: label ?? this.label,
  110. );
  111. }
  112. }
  113. /// 节点类型
  114. enum NodeType {
  115. element, // 要素节点
  116. operator, // 运算符节点
  117. result, // 结果节点
  118. }
  119. /// 边类型
  120. enum EdgeType {
  121. calculate, // 计算关系
  122. reference, // 引用关系
  123. contain, // 包含关系
  124. }
  125. extension EdgeTypeExtension on EdgeType {
  126. String get label {
  127. switch (this) {
  128. case EdgeType.calculate:
  129. return '计算';
  130. case EdgeType.reference:
  131. return '引用';
  132. case EdgeType.contain:
  133. return '包含';
  134. }
  135. }
  136. }