import 'package:flutter/material.dart'; /// 图节点模型 class GraphNode { final String id; final String label; final NodeType type; final dynamic data; final Offset position; GraphNode({ required this.id, required this.label, required this.type, this.data, required this.position, }); /// 从JSON创建 factory GraphNode.fromJson(Map json) { return GraphNode( id: json['id'] as String, label: json['label'] as String, type: NodeType.values.firstWhere( (e) => e.toString().split('.').last == json['type'], orElse: () => NodeType.element, ), data: json['data'], position: Offset( (json['position'] as Map)['x'] as double, (json['position'] as Map)['y'] as double, ), ); } /// 转换为JSON Map toJson() { return { 'id': id, 'label': label, 'type': type.toString().split('.').last, 'data': data, 'position': {'x': position.dx, 'y': position.dy}, }; } /// 复制并更新 GraphNode copyWith({ String? id, String? label, NodeType? type, dynamic data, Offset? position, }) { return GraphNode( id: id ?? this.id, label: label ?? this.label, type: type ?? this.type, data: data ?? this.data, position: position ?? this.position, ); } } /// 图边模型 class GraphEdge { final String id; final String source; final String target; final EdgeType type; final String? label; GraphEdge({ required this.id, required this.source, required this.target, required this.type, this.label, }); /// 从JSON创建 factory GraphEdge.fromJson(Map json) { return GraphEdge( id: json['id'] as String, source: json['source'] as String, target: json['target'] as String, type: EdgeType.values.firstWhere( (e) => e.toString().split('.').last == json['type'], orElse: () => EdgeType.reference, ), label: json['label'] as String?, ); } /// 转换为JSON Map toJson() { return { 'id': id, 'source': source, 'target': target, 'type': type.toString().split('.').last, 'label': label, }; } /// 复制并更新 GraphEdge copyWith({ String? id, String? source, String? target, EdgeType? type, String? label, }) { return GraphEdge( id: id ?? this.id, source: source ?? this.source, target: target ?? this.target, type: type ?? this.type, label: label ?? this.label, ); } } /// 节点类型 enum NodeType { element, // 要素节点 operator, // 运算符节点 result, // 结果节点 } /// 边类型 enum EdgeType { calculate, // 计算关系 reference, // 引用关系 contain, // 包含关系 } extension EdgeTypeExtension on EdgeType { String get label { switch (this) { case EdgeType.calculate: return '计算'; case EdgeType.reference: return '引用'; case EdgeType.contain: return '包含'; } } }