| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- 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<String, dynamic> 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<String, dynamic> 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<String, dynamic> 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<String, dynamic> 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 '包含';
- }
- }
- }
|