168 lines
6.1 KiB
Dart
168 lines
6.1 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter_markdown/flutter_markdown.dart';
|
|
import 'package:google_fonts/google_fonts.dart';
|
|
import 'package:trainhub_flutter/core/constants/ui_constants.dart';
|
|
import 'package:trainhub_flutter/core/theme/app_colors.dart';
|
|
import 'package:trainhub_flutter/domain/entities/chat_message.dart';
|
|
|
|
class MessageBubble extends StatelessWidget {
|
|
const MessageBubble({
|
|
super.key,
|
|
required this.message,
|
|
required this.formattedTime,
|
|
this.onCreatePlan,
|
|
});
|
|
|
|
final ChatMessageEntity message;
|
|
final String formattedTime;
|
|
|
|
/// When set (assistant messages), shows a "create plan" action that turns
|
|
/// the reply into a real training plan.
|
|
final VoidCallback? onCreatePlan;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final isUser = message.isUser;
|
|
final maxWidth = MediaQuery.of(context).size.width * 0.55;
|
|
return Padding(
|
|
padding: const EdgeInsets.only(bottom: UIConstants.spacing12),
|
|
child: Row(
|
|
mainAxisAlignment: isUser
|
|
? MainAxisAlignment.end
|
|
: MainAxisAlignment.start,
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
if (!isUser) ...[
|
|
_buildAvatar(
|
|
Icons.auto_awesome_rounded,
|
|
AppColors.surfaceContainerHigh,
|
|
),
|
|
const SizedBox(width: UIConstants.spacing8),
|
|
],
|
|
Flexible(
|
|
child: Column(
|
|
crossAxisAlignment: isUser
|
|
? CrossAxisAlignment.end
|
|
: CrossAxisAlignment.start,
|
|
children: [
|
|
Container(
|
|
constraints: BoxConstraints(maxWidth: maxWidth),
|
|
padding: const EdgeInsets.symmetric(
|
|
horizontal: UIConstants.spacing16,
|
|
vertical: UIConstants.spacing12,
|
|
),
|
|
decoration: BoxDecoration(
|
|
color: isUser
|
|
? AppColors.zinc700
|
|
: AppColors.surfaceContainer,
|
|
borderRadius: BorderRadius.only(
|
|
topLeft: const Radius.circular(16),
|
|
topRight: const Radius.circular(16),
|
|
bottomLeft: isUser
|
|
? const Radius.circular(16)
|
|
: const Radius.circular(4),
|
|
bottomRight: isUser
|
|
? const Radius.circular(4)
|
|
: const Radius.circular(16),
|
|
),
|
|
border: isUser
|
|
? null
|
|
: Border.all(color: AppColors.border, width: 1),
|
|
),
|
|
child: isUser
|
|
? SelectableText(
|
|
message.content,
|
|
style: const TextStyle(
|
|
color: AppColors.textPrimary,
|
|
fontSize: 14,
|
|
height: 1.5,
|
|
),
|
|
)
|
|
: MarkdownBody(
|
|
data: message.content,
|
|
styleSheet: MarkdownStyleSheet(
|
|
p: const TextStyle(
|
|
color: AppColors.textPrimary,
|
|
fontSize: 14,
|
|
height: 1.5,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
const SizedBox(height: 4),
|
|
Padding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 4),
|
|
child: Row(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
Text(
|
|
formattedTime,
|
|
style: const TextStyle(
|
|
color: AppColors.textMuted,
|
|
fontSize: 11,
|
|
),
|
|
),
|
|
if (!isUser && onCreatePlan != null) ...[
|
|
const SizedBox(width: UIConstants.spacing12),
|
|
InkWell(
|
|
onTap: onCreatePlan,
|
|
borderRadius: BorderRadius.circular(4),
|
|
child: Padding(
|
|
padding: const EdgeInsets.symmetric(
|
|
horizontal: 4,
|
|
vertical: 2,
|
|
),
|
|
child: Row(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
const Icon(
|
|
Icons.playlist_add_rounded,
|
|
size: 13,
|
|
color: AppColors.accent,
|
|
),
|
|
const SizedBox(width: 4),
|
|
Text(
|
|
'CREATE PLAN',
|
|
style: GoogleFonts.jetBrainsMono(
|
|
fontSize: 9,
|
|
fontWeight: FontWeight.w600,
|
|
letterSpacing: 0.8,
|
|
color: AppColors.accent,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
],
|
|
],
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
if (isUser) ...[
|
|
const SizedBox(width: UIConstants.spacing8),
|
|
_buildAvatar(
|
|
Icons.person_rounded,
|
|
AppColors.accent.withValues(alpha: 0.15),
|
|
),
|
|
],
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildAvatar(IconData icon, Color backgroundColor) {
|
|
return Container(
|
|
width: 28,
|
|
height: 28,
|
|
decoration: BoxDecoration(
|
|
color: backgroundColor,
|
|
borderRadius: BorderRadius.circular(8),
|
|
),
|
|
child: Icon(icon, size: 14, color: AppColors.accent),
|
|
);
|
|
}
|
|
}
|