class Message { final String role; // 'user' or 'assistant' final String content; final DateTime timestamp; final bool isLoading; Message({ required this.role, required this.content, required this.timestamp, this.isLoading = false, }); // 从JSON构造函数 factory Message.fromJson(Map json) { return Message( role: json['role'] as String, content: json['content'] as String, timestamp: DateTime.parse(json['timestamp'] as String), isLoading: json['isLoading'] as bool? ?? false, ); } // 转换为JSON Map toJson() { return { 'role': role, 'content': content, 'timestamp': timestamp.toIso8601String(), 'isLoading': isLoading, }; } // 创建一个加载中的消息 factory Message.loading() { return Message( role: 'assistant', content: '', timestamp: DateTime.now(), isLoading: true, ); } // 复制并修改 Message copyWith({ String? role, String? content, DateTime? timestamp, bool? isLoading, }) { return Message( role: role ?? this.role, content: content ?? this.content, timestamp: timestamp ?? this.timestamp, isLoading: isLoading ?? this.isLoading, ); } }