annotation.dart 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import 'package:flutter/material.dart';
  2. /// 批注模型
  3. class Annotation {
  4. final String id;
  5. final String text;
  6. final Offset start;
  7. final Offset end;
  8. final AnnotationType type;
  9. final String? suggestion;
  10. final String? documentId;
  11. final DateTime createdAt;
  12. Annotation({
  13. required this.id,
  14. required this.text,
  15. required this.start,
  16. required this.end,
  17. required this.type,
  18. this.suggestion,
  19. this.documentId,
  20. required this.createdAt,
  21. });
  22. /// 从JSON创建
  23. factory Annotation.fromJson(Map<String, dynamic> json) {
  24. return Annotation(
  25. id: json['id'] as String,
  26. text: json['text'] as String,
  27. start: Offset(
  28. (json['start'] as Map)['x'] as double,
  29. (json['start'] as Map)['y'] as double,
  30. ),
  31. end: Offset(
  32. (json['end'] as Map)['x'] as double,
  33. (json['end'] as Map)['y'] as double,
  34. ),
  35. type: AnnotationType.values.firstWhere(
  36. (e) => e.toString().split('.').last == json['type'],
  37. orElse: () => AnnotationType.highlight,
  38. ),
  39. suggestion: json['suggestion'] as String?,
  40. documentId: json['documentId'] as String?,
  41. createdAt: DateTime.parse(json['createdAt'] as String),
  42. );
  43. }
  44. /// 转换为JSON
  45. Map<String, dynamic> toJson() {
  46. return {
  47. 'id': id,
  48. 'text': text,
  49. 'start': {'x': start.dx, 'y': start.dy},
  50. 'end': {'x': end.dx, 'y': end.dy},
  51. 'type': type.toString().split('.').last,
  52. 'suggestion': suggestion,
  53. 'documentId': documentId,
  54. 'createdAt': createdAt.toIso8601String(),
  55. };
  56. }
  57. /// 复制并更新
  58. Annotation copyWith({
  59. String? id,
  60. String? text,
  61. Offset? start,
  62. Offset? end,
  63. AnnotationType? type,
  64. String? suggestion,
  65. String? documentId,
  66. DateTime? createdAt,
  67. }) {
  68. return Annotation(
  69. id: id ?? this.id,
  70. text: text ?? this.text,
  71. start: start ?? this.start,
  72. end: end ?? this.end,
  73. type: type ?? this.type,
  74. suggestion: suggestion ?? this.suggestion,
  75. documentId: documentId ?? this.documentId,
  76. createdAt: createdAt ?? this.createdAt,
  77. );
  78. }
  79. }
  80. /// 批注类型
  81. enum AnnotationType {
  82. highlight, // 高亮
  83. strikethrough, // 删除线
  84. suggestion, // 建议
  85. }
  86. extension AnnotationTypeExtension on AnnotationType {
  87. String get label {
  88. switch (this) {
  89. case AnnotationType.highlight:
  90. return '高亮';
  91. case AnnotationType.strikethrough:
  92. return '删除线';
  93. case AnnotationType.suggestion:
  94. return '建议';
  95. }
  96. }
  97. }