You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
77 lines
2.0 KiB
77 lines
2.0 KiB
class Message {
|
|
final bool isUser; //标识信息是否来自用户还是AI
|
|
final String text; //信息文本内容
|
|
final DateTime timestamp; // 消息时间戳
|
|
final bool isRecognizing; // 是否为语音识别中的临时消息
|
|
final bool hasImage; // 是否包含图片
|
|
final String? imagePath; // 图片路径
|
|
final Map? meta; //卡片数据
|
|
|
|
Message({
|
|
// 构造函数
|
|
required this.isUser,
|
|
required this.text,
|
|
DateTime? timestamp,
|
|
this.isRecognizing = false,
|
|
this.hasImage = false,
|
|
this.imagePath,
|
|
this.meta,
|
|
}) : this.timestamp = timestamp ?? DateTime.now();
|
|
|
|
// 从JSON构造函数
|
|
factory Message.fromJson(Map<String, dynamic> json) {
|
|
return Message(
|
|
isUser: json['isUser'] as bool,
|
|
text: json['text'] as String,
|
|
timestamp: DateTime.parse(json['timestamp'] as String),
|
|
isRecognizing: json['isRecognizing'] as bool? ?? false,
|
|
hasImage: json['hasImage'] as bool? ?? false,
|
|
imagePath: json['imagePath'] as String?,
|
|
meta: json['meta'] as Map?,
|
|
);
|
|
}
|
|
|
|
// 转换为JSON
|
|
Map<String, dynamic> toJson() {
|
|
return {
|
|
'isUser': isUser,
|
|
'text': text,
|
|
'timestamp': timestamp.toIso8601String(),
|
|
'isRecognizing': isRecognizing,
|
|
'hasImage': hasImage,
|
|
'imagePath': imagePath,
|
|
'meta': meta,
|
|
};
|
|
}
|
|
|
|
// 创建一个加载中的消息
|
|
factory Message.loading() {
|
|
return Message(
|
|
isUser: false,
|
|
text: '',
|
|
timestamp: DateTime.now(),
|
|
isRecognizing: true,
|
|
);
|
|
}
|
|
|
|
// 复制并修改
|
|
Message copyWith({
|
|
bool? isUser,
|
|
String? text,
|
|
DateTime? timestamp,
|
|
bool? isRecognizing,
|
|
bool? hasImage,
|
|
String? imagePath,
|
|
Map? meta,
|
|
}) {
|
|
return Message(
|
|
isUser: isUser ?? this.isUser,
|
|
text: text ?? this.text,
|
|
timestamp: timestamp ?? this.timestamp,
|
|
isRecognizing: isRecognizing ?? this.isRecognizing,
|
|
hasImage: hasImage ?? this.hasImage,
|
|
imagePath: imagePath ?? this.imagePath,
|
|
meta: meta ?? this.meta,
|
|
);
|
|
}
|
|
}
|
|
|