import 'package:flutter/material.dart'; /// 要素模型 class DocumentElement { final String id; final ElementType type; final String label; final String value; final Offset? position; // 在文档中的位置 final String? documentId; final Map? metadata; DocumentElement({ required this.id, required this.type, required this.label, required this.value, this.position, this.documentId, this.metadata, }); /// 从JSON创建 factory DocumentElement.fromJson(Map json) { return DocumentElement( id: json['id'] as String, type: ElementType.values.firstWhere( (e) => e.toString().split('.').last == json['type'], orElse: () => ElementType.other, ), label: json['label'] as String, value: json['value'] as String, position: json['position'] != null ? Offset( (json['position'] as Map)['x'] as double, (json['position'] as Map)['y'] as double, ) : null, documentId: json['documentId'] as String?, metadata: json['metadata'] as Map?, ); } /// 转换为JSON Map toJson() { return { 'id': id, 'type': type.toString().split('.').last, 'label': label, 'value': value, 'position': position != null ? {'x': position!.dx, 'y': position!.dy} : null, 'documentId': documentId, 'metadata': metadata, }; } /// 复制并更新 DocumentElement copyWith({ String? id, ElementType? type, String? label, String? value, Offset? position, String? documentId, Map? metadata, }) { return DocumentElement( id: id ?? this.id, type: type ?? this.type, label: label ?? this.label, value: value ?? this.value, position: position ?? this.position, documentId: documentId ?? this.documentId, metadata: metadata ?? this.metadata, ); } /// 显示文本 String get displayText => '$label: $value'; } /// 要素类型 enum ElementType { amount, // 金额 company, // 公司 person, // 人名 location, // 地名 date, // 日期 other, // 其他 } extension ElementTypeExtension on ElementType { String get label { switch (this) { case ElementType.amount: return '金额'; case ElementType.company: return '公司'; case ElementType.person: return '人名'; case ElementType.location: return '地名'; case ElementType.date: return '日期'; case ElementType.other: return '其他'; } } }