annotation_provider.dart 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import 'dart:ui';
  2. import 'package:flutter/foundation.dart';
  3. import '../models/annotation.dart';
  4. /// 批注状态管理
  5. class AnnotationProvider with ChangeNotifier {
  6. List<Annotation> _annotations = [];
  7. bool _loading = false;
  8. List<Annotation> get annotations => _annotations;
  9. bool get loading => _loading;
  10. /// 加载批注列表
  11. Future<void> loadAnnotations({String? documentId}) async {
  12. _loading = true;
  13. notifyListeners();
  14. // 模拟网络请求
  15. await Future.delayed(const Duration(milliseconds: 300));
  16. // 模拟批注数据
  17. _annotations = _getMockAnnotations(documentId);
  18. _loading = false;
  19. notifyListeners();
  20. }
  21. /// 添加批注
  22. void addAnnotation(Annotation annotation) {
  23. _annotations.add(annotation);
  24. notifyListeners();
  25. }
  26. /// 更新批注
  27. void updateAnnotation(Annotation annotation) {
  28. final index = _annotations.indexWhere((a) => a.id == annotation.id);
  29. if (index != -1) {
  30. _annotations[index] = annotation;
  31. notifyListeners();
  32. }
  33. }
  34. /// 删除批注
  35. void removeAnnotation(String id) {
  36. _annotations.removeWhere((a) => a.id == id);
  37. notifyListeners();
  38. }
  39. /// 接受批注(应用建议)
  40. void acceptAnnotation(String id) {
  41. final annotation = _annotations.firstWhere((a) => a.id == id);
  42. // 这里可以触发文本替换逻辑
  43. removeAnnotation(id);
  44. }
  45. /// 拒绝批注
  46. void rejectAnnotation(String id) {
  47. removeAnnotation(id);
  48. }
  49. /// 根据文档ID获取批注
  50. List<Annotation> getAnnotationsByDocumentId(String? documentId) {
  51. if (documentId == null) return _annotations;
  52. return _annotations.where((a) => a.documentId == documentId).toList();
  53. }
  54. /// 模拟批注数据
  55. List<Annotation> _getMockAnnotations(String? documentId) {
  56. return [
  57. // 错别字批注
  58. Annotation(
  59. id: 'a1',
  60. text: '智能票据',
  61. start: const Offset(0, 0),
  62. end: const Offset(100, 20),
  63. type: AnnotationType.strikethrough,
  64. suggestion: '智能票据处理',
  65. documentId: documentId,
  66. createdAt: DateTime.now(),
  67. ),
  68. // AI润色建议
  69. Annotation(
  70. id: 'a2',
  71. text: '提升发票、合同、报销单据等票据类文档的处理效率与准确率',
  72. start: const Offset(0, 0),
  73. end: const Offset(200, 20),
  74. type: AnnotationType.suggestion,
  75. suggestion: '建议:可以优化为"显著提升处理效率与准确率"',
  76. documentId: documentId,
  77. createdAt: DateTime.now(),
  78. ),
  79. ];
  80. }
  81. }