1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
| import 'package:flutter/material.dart'; import '../models/message.dart';
class MessageBubble extends StatelessWidget { final ChatMessage message; final bool showAvatar; final bool showTime;
const MessageBubble({ super.key, required this.message, this.showAvatar = true, this.showTime = true, });
@override Widget build(BuildContext context) { return Padding( padding: EdgeInsets.only( left: message.isMe ? 60 : 12, right: message.isMe ? 12 : 60, top: 4, bottom: 4, ), child: Column( crossAxisAlignment: message.isMe ? CrossAxisAlignment.end : CrossAxisAlignment.start, children: [ if (showTime) _buildTimeLabel(), Row( mainAxisAlignment: message.isMe ? MainAxisAlignment.end : MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.end, children: [ if (!message.isMe && showAvatar) _buildAvatar(), const SizedBox(width: 8), Flexible(child: _buildBubble(context)), const SizedBox(width: 8), if (message.isMe) _buildStatusIcon(), ], ), ], ), ); }
Widget _buildTimeLabel() { return Padding( padding: const EdgeInsets.symmetric(vertical: 8), child: Center( child: Text( _formatTime(message.timestamp), style: TextStyle( fontSize: 12, color: Colors.grey[400], ), ), ), ); }
Widget _buildAvatar() { return CircleAvatar( radius: 16, backgroundColor: message.isMe ? Colors.blue[100] : Colors.grey[200], child: Icon( message.isMe ? Icons.person : Icons.smart_toy, size: 18, color: message.isMe ? Colors.blue : Colors.grey[600], ), ); }
Widget _buildBubble(BuildContext context) { return Container( padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 10), decoration: BoxDecoration( color: message.isMe ? Theme.of(context).colorScheme.primary : Colors.grey[100], borderRadius: BorderRadius.only( topLeft: const Radius.circular(16), topRight: const Radius.circular(16), bottomLeft: Radius.circular(message.isMe ? 16 : 4), bottomRight: Radius.circular(message.isMe ? 4 : 16), ), ), child: _buildContent(context), ); }
Widget _buildContent(BuildContext context) { switch (message.type) { case MessageType.text: return Text( message.content, style: TextStyle( fontSize: 15, color: message.isMe ? Colors.white : Colors.black87, height: 1.4, ), ); case MessageType.image: return _buildImageContent(); case MessageType.voice: return _buildVoiceContent(); case MessageType.system: return _buildSystemContent(); case MessageType.typing: return _buildTypingIndicator(); } }
Widget _buildImageContent() { return ClipRRect( borderRadius: BorderRadius.circular(8), child: Image.network( message.imageUrl!, width: 200, height: 200, fit: BoxFit.cover, loadingBuilder: (context, child, progress) { if (progress == null) return child; return Container( width: 200, height: 200, color: Colors.grey[200], child: const Center(child: CircularProgressIndicator()), ); }, errorBuilder: (context, error, stackTrace) { return Container( width: 200, height: 200, color: Colors.grey[200], child: const Icon(Icons.broken_image, color: Colors.grey), ); }, ), ); }
Widget _buildVoiceContent() { return Container( width: 120, height: 36, child: Row( children: [ Icon( Icons.mic, size: 18, color: message.isMe ? Colors.white : Colors.black87, ), const SizedBox(width: 8), Expanded( child: LinearProgressIndicator( value: 0.5, backgroundColor: message.isMe ? Colors.white.withOpacity(0.3) : Colors.grey[300], color: message.isMe ? Colors.white : Colors.blue, ), ), const SizedBox(width: 8), Text( "${message.voiceDuration ?? 0}s", style: TextStyle( fontSize: 12, color: message.isMe ? Colors.white : Colors.black87, ), ), ], ), ); }
Widget _buildSystemContent() { return Center( child: Container( padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6), decoration: BoxDecoration( color: Colors.grey[200], borderRadius: BorderRadius.circular(12), ), child: Text( message.content, style: TextStyle(fontSize: 12, color: Colors.grey[600]), ), ), ); }
Widget _buildTypingIndicator() { return Container( padding: const EdgeInsets.all(12), decoration: BoxDecoration( color: Colors.grey[100], borderRadius: BorderRadius.circular(16), ), child: Row( mainAxisSize: MainAxisSize.min, children: [ _buildDot(0), const SizedBox(width: 4), _buildDot(1), const SizedBox(width: 4), _buildDot(2), ], ), ); }
Widget _buildDot(int index) { return AnimatedOpacity( opacity: 1.0, duration: Duration(milliseconds: 400), child: Container( width: 8, height: 8, decoration: BoxDecoration( color: Colors.grey[400], shape: BoxShape.circle, ), ), ); }
Widget _buildStatusIcon() { switch (message.status) { case MessageStatus.sending: return const SizedBox( width: 16, height: 16, child: CircularProgressIndicator(strokeWidth: 2), ); case MessageStatus.sent: return Icon(Icons.check, size: 16, color: Colors.grey[400]); case MessageStatus.delivered: return Icon(Icons.done_all, size: 16, color: Colors.grey[400]); case MessageStatus.read: return Icon(Icons.done_all, size: 16, color: Colors.blue[400]); case MessageStatus.failed: return Icon(Icons.error, size: 16, color: Colors.red[400]); } }
String _formatTime(DateTime time) { final now = DateTime.now(); final diff = now.difference(time);
if (diff.inMinutes < 1) return '刚刚'; if (diff.inHours < 1) return '${diff.inMinutes} 分钟前'; if (diff.inDays < 1) { return '${time.hour.toString().padLeft(2, '0')}:${time.minute.toString().padLeft(2, '0')}'; } return '${time.month}/${time.day} ${time.hour.toString().padLeft(2, '0')}:${time.minute.toString().padLeft(2, '0')}'; } }
|