///高亮模型 //不同颜色的高亮 enum HighlightColor { yellow, orange, green, blue, pink } ///高亮模型 // 关联书籍和章节 // 记录选中的文本范围 // 支持不同颜色的高亮 // 记录创建时间 class Highlight { //唯一标识符 - 使用UUID确保全局唯一 final String id; //对应的书籍id final String bookId; // 对应章节id final String? chapterId; // 选中文本 final String selectedText; // 开始索引 final int startIndex; // 结尾索引 final int endIndex; // 高亮颜色 final HighlightColor color; // 创建时间 final DateTime createdTime; const Highlight( {required this.id, required this.bookId, this.chapterId, required this.selectedText, required this.startIndex, required this.endIndex, required this.color, required this.createdTime}); /// copyWith方法 - 创建对象的副本 Highlight copyWith({ String? id, String? bookId, String? chapterId, String? selectedText, int? startIndex, int? endIndex, HighlightColor? color, DateTime? createdTime, }) { return Highlight( id: id ?? this.id, bookId: bookId ?? this.bookId, chapterId: chapterId ?? this.chapterId, // 修复:添加缺失的参数 selectedText: selectedText ?? this.selectedText, startIndex: startIndex ?? this.startIndex, endIndex: endIndex ?? this.endIndex, color: color ?? this.color, createdTime: createdTime ?? this.createdTime, ); } /// toMap方法 - 将对象转换为Map Map toMap() { return { 'id': id, 'bookId': bookId, 'chapterId': chapterId, 'selectedText': selectedText, 'startIndex': startIndex, 'endIndex': endIndex, 'color': color.name, // 枚举值转换为字符串 'createdTime': createdTime.toIso8601String(), // DateTime转换为字符串 }; } /// fromMap构造函数 - 从Map创建Highlight对象 factory Highlight.fromMap(Map map) { return Highlight( id: map['id'], bookId: map['bookId'], chapterId: map['chapterId'], selectedText: map['selectedText'], startIndex: map['startIndex'], endIndex: map['endIndex'], color: HighlightColor.values.firstWhere((e) => e.name == map['color']), createdTime: DateTime.parse(map['createdTime']), ); } /// 创建一个新Highlight实例的工厂方法 /// 这是一个便利方法,用于创建基本的高亮对象 /// 通常在用户选中文本时使用 factory Highlight.create({ required String bookId, required String selectedText, required int startIndex, required int endIndex, HighlightColor color = HighlightColor.yellow, // 默认颜色 String? chapterId, }) { return Highlight( id: _generateId(), // 生成唯一ID bookId: bookId, chapterId: chapterId, selectedText: selectedText, startIndex: startIndex, endIndex: endIndex, color: color, createdTime: DateTime.now(), // 当前时间作为创建时间 ); } /// 生成唯一ID的私有方法 static String _generateId() { return '${DateTime.now().millisecondsSinceEpoch}_${(DateTime.now().microsecond).toString()}'; } /// 重写toString方法,便于调试 @override String toString() { return 'Highlight(id: $id, bookId: $bookId, chapterId: $chapterId, selectedText: $selectedText)'; } /// 重写equals和hashCode,用于对象比较 @override bool operator ==(Object other) { if (identical(this, other)) return true; return other is Highlight && other.id == id; } @override int get hashCode => id.hashCode; }