Add file analysis and answer feedback

This commit is contained in:
Hamza Ayed
2026-10-01 15:13:07 +03:00
parent dc4d5bad0f
commit 3563a104a3
10 changed files with 586 additions and 89 deletions
@@ -5,6 +5,7 @@ import 'package:flutter_highlight/flutter_highlight.dart';
import 'package:flutter_highlight/themes/monokai-sublime.dart';
import 'package:flutter/services.dart';
import 'package:share_plus/share_plus.dart';
import 'package:file_selector/file_selector.dart';
import '../../../audio/presentation/cubit/voice_cubit.dart';
import '../../../audio/presentation/cubit/voice_state.dart';
@@ -14,6 +15,11 @@ import '../cubit/chat_state.dart';
const _ink = Color(0xFF152A2A);
const _green = Color(0xFF167C68);
const _codeFileExtensions = [
'py', 'dart', 'js', 'ts', 'tsx', 'jsx', 'html', 'css', 'json', 'yaml', 'yml',
'toml', 'md', 'txt', 'sh', 'ps1', 'sql', 'java', 'kt', 'go', 'rs', 'c', 'h',
'cpp', 'hpp',
];
class ChatPage extends StatefulWidget {
const ChatPage({super.key});
@@ -25,6 +31,7 @@ class ChatPage extends StatefulWidget {
class _ChatPageState extends State<ChatPage> {
final _input = TextEditingController();
final _scroll = ScrollController();
final List<XFile> _attachments = [];
bool _notificationsEnabled = false;
@override
@@ -46,12 +53,38 @@ class _ChatPageState extends State<ChatPage> {
void _send([String? suggested]) {
final text = (suggested ?? _input.text).trim();
if (text.isEmpty) return;
context.read<ChatCubit>().send(text);
if (text.isEmpty && _attachments.isEmpty) return;
context.read<ChatCubit>().send(
text,
attachments: List<XFile>.unmodifiable(_attachments),
);
setState(_attachments.clear);
_input.clear();
_jumpToBottom();
}
Future<void> _pickCodeFiles() async {
final selected = await openFiles(
acceptedTypeGroups: const [
XTypeGroup(label: 'ملفات برمجية ونصية', extensions: _codeFileExtensions),
],
);
if (!mounted || selected.isEmpty) return;
final remaining = 3 - _attachments.length;
if (remaining <= 0) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('الحد الأقصى 3 ملفات في الرسالة.')),
);
return;
}
setState(() => _attachments.addAll(selected.take(remaining)));
if (selected.length > remaining) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('أُضيفت أول 3 ملفات فقط.')),
);
}
}
void _jumpToBottom() {
WidgetsBinding.instance.addPostFrameCallback((_) {
if (_scroll.hasClients) {
@@ -751,6 +784,10 @@ class _ChatPageState extends State<ChatPage> {
itemBuilder: (context, index) {
if (index == chat.messages.length) return _typing(chat);
final message = chat.messages[index];
final selectedFeedback =
message.selectedVersion < message.feedbackVersions.length
? message.feedbackVersions[message.selectedVersion]
: null;
return Align(
alignment:
message.fromUser ? Alignment.centerLeft : Alignment.centerRight,
@@ -855,6 +892,34 @@ class _ChatPageState extends State<ChatPage> {
child: Wrap(
spacing: 2,
children: [
IconButton(
tooltip: 'إجابة مفيدة',
visualDensity: VisualDensity.compact,
onPressed: () => context
.read<ChatCubit>()
.rateAnswer(index, 1),
color: selectedFeedback == 1 ? _green : null,
icon: Icon(
selectedFeedback == 1
? Icons.thumb_up_alt_rounded
: Icons.thumb_up_alt_outlined,
size: 18,
),
),
IconButton(
tooltip: 'إجابة غير مفيدة',
visualDensity: VisualDensity.compact,
onPressed: () => context
.read<ChatCubit>()
.rateAnswer(index, -1),
color: selectedFeedback == -1 ? Colors.redAccent : null,
icon: Icon(
selectedFeedback == -1
? Icons.thumb_down_alt_rounded
: Icons.thumb_down_alt_outlined,
size: 18,
),
),
IconButton(
tooltip: 'نسخ الإجابة',
visualDensity: VisualDensity.compact,
@@ -934,96 +999,139 @@ class _ChatPageState extends State<ChatPage> {
child: ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 800),
child: Center(
child: Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(18),
border: Border.all(color: const Color(0xFFE1E5DE)),
boxShadow: const [
BoxShadow(
color: Color(0x0A142A23),
blurRadius: 20,
offset: Offset(0, 6),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
if (_attachments.isNotEmpty)
Padding(
padding: const EdgeInsets.fromLTRB(10, 10, 10, 0),
child: Wrap(
spacing: 6,
runSpacing: 4,
children: [
for (final file in _attachments)
InputChip(
avatar: const Icon(
Icons.description_outlined,
size: 16,
),
label: ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 220),
child: Text(
file.name,
overflow: TextOverflow.ellipsis,
),
),
onDeleted: () => setState(
() => _attachments.remove(file),
),
),
],
),
),
],
),
child: Row(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Expanded(
child: TextField(
controller: _input,
minLines: 1,
maxLines: 5,
textDirection: TextDirection.rtl,
textInputAction: TextInputAction.send,
onSubmitted: (_) => _send(),
decoration: InputDecoration(
hintText:
chat.isWorkspaceMode
Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(18),
border: Border.all(color: const Color(0xFFE1E5DE)),
boxShadow: const [
BoxShadow(
color: Color(0x0A142A23),
blurRadius: 20,
offset: Offset(0, 6),
),
],
),
child: Row(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Expanded(
child: TextField(
controller: _input,
minLines: 1,
maxLines: 5,
textDirection: TextDirection.rtl,
textInputAction: TextInputAction.send,
onSubmitted: (_) => _send(),
decoration: InputDecoration(
hintText: _attachments.isNotEmpty
? 'اسأل عن الملفات المرفقة أو اضغط إرسال لتحليلها…'
: chat.isWorkspaceMode
? 'اسأل عن ملف كود أو عن مساحة العمل…'
: chat.isWebSearchMode
? 'ما الموضوع الذي تريد البحث عنه على الإنترنت؟'
: 'اكتب رسالتك هنا…',
hintStyle: TextStyle(color: Color(0xFF9BA59E)),
border: InputBorder.none,
contentPadding: EdgeInsets.symmetric(
horizontal: 18,
vertical: 16,
hintStyle: const TextStyle(
color: Color(0xFF9BA59E),
),
border: InputBorder.none,
contentPadding: const EdgeInsets.symmetric(
horizontal: 18,
vertical: 16,
),
),
),
),
),
),
Padding(
padding: const EdgeInsets.all(4),
child: IconButton(
onPressed:
voice.status == VoiceStatus.transcribing
Padding(
padding: const EdgeInsets.all(4),
child: IconButton(
onPressed: chat.isSending ? null : _pickCodeFiles,
tooltip: 'إضافة ملفات كود للتحليل (حتى 3)',
icon: const Icon(Icons.attach_file_rounded),
color: _green,
),
),
Padding(
padding: const EdgeInsets.all(4),
child: IconButton(
onPressed: voice.status == VoiceStatus.transcribing
? null
: () => _toggleRecording(voice),
tooltip:
voice.status == VoiceStatus.recording
tooltip: voice.status == VoiceStatus.recording
? 'إيقاف التسجيل'
: 'تسجيل صوت',
color:
voice.status == VoiceStatus.recording
color: voice.status == VoiceStatus.recording
? Colors.red
: _green,
icon:
voice.status == VoiceStatus.transcribing
icon: voice.status == VoiceStatus.transcribing
? const SizedBox(
width: 18,
height: 18,
child: CircularProgressIndicator(strokeWidth: 2),
)
width: 18,
height: 18,
child: CircularProgressIndicator(
strokeWidth: 2,
),
)
: Icon(
voice.status == VoiceStatus.recording
? Icons.stop_circle_outlined
: Icons.mic_none_rounded,
),
),
),
Padding(
padding: const EdgeInsets.all(7),
child: IconButton.filled(
onPressed:
chat.isSending
? () => context.read<ChatCubit>().cancelGeneration()
voice.status == VoiceStatus.recording
? Icons.stop_circle_outlined
: Icons.mic_none_rounded,
),
),
),
Padding(
padding: const EdgeInsets.all(7),
child: IconButton.filled(
onPressed: chat.isSending
? () => context
.read<ChatCubit>()
.cancelGeneration()
: () => _send(),
style: IconButton.styleFrom(
backgroundColor: chat.isSending ? Colors.red : _green,
foregroundColor: Colors.white,
style: IconButton.styleFrom(
backgroundColor: chat.isSending ? Colors.red : _green,
foregroundColor: Colors.white,
),
icon: Icon(
chat.isSending
? Icons.stop_rounded
: Icons.arrow_upward_rounded,
),
tooltip: chat.isSending ? 'إيقاف التوليد' : 'إرسال',
),
),
icon: Icon(
chat.isSending
? Icons.stop_rounded
: Icons.arrow_upward_rounded,
),
tooltip: chat.isSending ? 'إيقاف التوليد' : 'إرسال',
),
],
),
],
),
),
],
),
),
),