2025-12-03 11:57:05 +05:30
|
|
|
|
import 'package:flutter/material.dart';
|
2025-12-15 11:10:52 +05:30
|
|
|
|
import 'package:provider/provider.dart';
|
2025-12-03 11:57:05 +05:30
|
|
|
|
|
2025-12-15 11:10:52 +05:30
|
|
|
|
import '../services/chat_service.dart';
|
|
|
|
|
|
import '../services/reverb_socket_service.dart';
|
|
|
|
|
|
import '../services/dio_client.dart';
|
|
|
|
|
|
import '../providers/chat_unread_provider.dart';
|
2025-12-16 10:24:16 +05:30
|
|
|
|
import 'package:url_launcher/url_launcher.dart';
|
|
|
|
|
|
import 'dart:io';
|
|
|
|
|
|
import 'package:file_selector/file_selector.dart';
|
|
|
|
|
|
import 'chat_file_viewer.dart';
|
|
|
|
|
|
import '../widgets/chat_file_preview.dart';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-12-15 11:10:52 +05:30
|
|
|
|
|
|
|
|
|
|
class ChatScreen extends StatefulWidget {
|
2025-12-03 11:57:05 +05:30
|
|
|
|
const ChatScreen({super.key});
|
|
|
|
|
|
|
2025-12-15 11:10:52 +05:30
|
|
|
|
@override
|
|
|
|
|
|
State<ChatScreen> createState() => _ChatScreenState();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class _ChatScreenState extends State<ChatScreen> {
|
|
|
|
|
|
final TextEditingController _messageCtrl = TextEditingController();
|
|
|
|
|
|
final ScrollController _scrollCtrl = ScrollController();
|
|
|
|
|
|
|
|
|
|
|
|
late ChatService _chatService;
|
|
|
|
|
|
final ReverbSocketService _socket = ReverbSocketService();
|
|
|
|
|
|
|
|
|
|
|
|
int? ticketId;
|
|
|
|
|
|
List<Map<String, dynamic>> messages = [];
|
|
|
|
|
|
bool isLoading = true;
|
|
|
|
|
|
|
|
|
|
|
|
// ============================
|
|
|
|
|
|
// INIT STATE
|
|
|
|
|
|
// ============================
|
|
|
|
|
|
@override
|
|
|
|
|
|
void initState() {
|
|
|
|
|
|
super.initState();
|
|
|
|
|
|
|
|
|
|
|
|
_chatService = ChatService(DioClient.getInstance(context));
|
|
|
|
|
|
|
|
|
|
|
|
// 🔔 Mark chat as OPEN (important)
|
|
|
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
|
|
|
|
context.read<ChatUnreadProvider>().setChatOpen(true);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
_initChat();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-16 10:24:16 +05:30
|
|
|
|
Future<void> _pickAndSendFile() async {
|
|
|
|
|
|
final XFile? file = await openFile();
|
|
|
|
|
|
|
|
|
|
|
|
if (file == null || ticketId == null) return;
|
|
|
|
|
|
|
|
|
|
|
|
final dartFile = File(file.path);
|
|
|
|
|
|
|
|
|
|
|
|
await _chatService.sendFile(ticketId!, dartFile);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-12-15 11:10:52 +05:30
|
|
|
|
// ============================
|
|
|
|
|
|
// INIT CHAT
|
|
|
|
|
|
// ============================
|
|
|
|
|
|
Future<void> _initChat() async {
|
|
|
|
|
|
// 1️⃣ Start chat
|
|
|
|
|
|
final ticketRes = await _chatService.startChat();
|
|
|
|
|
|
ticketId = ticketRes['ticket']['id'];
|
|
|
|
|
|
|
|
|
|
|
|
// 2️⃣ Load messages
|
|
|
|
|
|
final msgs = await _chatService.getMessages(ticketId!);
|
|
|
|
|
|
messages = List<Map<String, dynamic>>.from(msgs);
|
|
|
|
|
|
|
|
|
|
|
|
// 3️⃣ Realtime socket
|
|
|
|
|
|
await _socket.connect(
|
|
|
|
|
|
context: context,
|
|
|
|
|
|
ticketId: ticketId!,
|
|
|
|
|
|
onMessage: (msg) {
|
|
|
|
|
|
if (!mounted) return;
|
|
|
|
|
|
setState(() => messages.add(msg));
|
|
|
|
|
|
_scrollToBottom();
|
|
|
|
|
|
},
|
|
|
|
|
|
onAdminMessage: () {
|
|
|
|
|
|
if (!mounted) {
|
|
|
|
|
|
context.read<ChatUnreadProvider>().increment();
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!mounted) return;
|
|
|
|
|
|
setState(() => isLoading = false);
|
|
|
|
|
|
_scrollToBottom();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ============================
|
|
|
|
|
|
// SCROLL
|
|
|
|
|
|
// ============================
|
|
|
|
|
|
void _scrollToBottom() {
|
|
|
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
|
|
|
|
if (_scrollCtrl.hasClients) {
|
|
|
|
|
|
_scrollCtrl.jumpTo(_scrollCtrl.position.maxScrollExtent);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ============================
|
|
|
|
|
|
// SEND MESSAGE
|
|
|
|
|
|
// ============================
|
|
|
|
|
|
Future<void> _sendMessage() async {
|
|
|
|
|
|
final text = _messageCtrl.text.trim();
|
|
|
|
|
|
if (text.isEmpty) return;
|
|
|
|
|
|
|
|
|
|
|
|
_messageCtrl.clear();
|
|
|
|
|
|
|
|
|
|
|
|
await _chatService.sendMessage(
|
|
|
|
|
|
ticketId!,
|
|
|
|
|
|
message: text,
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ============================
|
|
|
|
|
|
// DISPOSE
|
|
|
|
|
|
// ============================
|
|
|
|
|
|
@override
|
|
|
|
|
|
void dispose() {
|
|
|
|
|
|
// 🔕 Mark chat CLOSED
|
|
|
|
|
|
context.read<ChatUnreadProvider>().setChatOpen(false);
|
|
|
|
|
|
|
|
|
|
|
|
_socket.disconnect();
|
|
|
|
|
|
_messageCtrl.dispose();
|
|
|
|
|
|
_scrollCtrl.dispose();
|
|
|
|
|
|
super.dispose();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ============================
|
|
|
|
|
|
// UI
|
|
|
|
|
|
// ============================
|
2025-12-03 11:57:05 +05:30
|
|
|
|
@override
|
|
|
|
|
|
Widget build(BuildContext context) {
|
2025-12-15 11:10:52 +05:30
|
|
|
|
return Scaffold(
|
|
|
|
|
|
appBar: AppBar(title: const Text("Support Chat")),
|
|
|
|
|
|
body: isLoading
|
|
|
|
|
|
? const Center(child: CircularProgressIndicator())
|
|
|
|
|
|
: Column(
|
|
|
|
|
|
children: [
|
|
|
|
|
|
Expanded(child: _buildMessages()),
|
|
|
|
|
|
_buildInput(),
|
|
|
|
|
|
],
|
|
|
|
|
|
),
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-16 10:24:16 +05:30
|
|
|
|
|
|
|
|
|
|
// Future<void> _openUrl(String url) async {
|
|
|
|
|
|
// final uri = Uri.parse(url);
|
|
|
|
|
|
//
|
|
|
|
|
|
// if (await canLaunchUrl(uri)) {
|
|
|
|
|
|
// await launchUrl(
|
|
|
|
|
|
// uri,
|
|
|
|
|
|
// mode: LaunchMode.externalApplication,
|
|
|
|
|
|
// );
|
|
|
|
|
|
// } else {
|
|
|
|
|
|
// debugPrint("❌ Cannot launch URL: $url");
|
|
|
|
|
|
// }
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Widget _buildMessageContent({
|
|
|
|
|
|
String? message,
|
|
|
|
|
|
String? filePath,
|
|
|
|
|
|
String? fileType,
|
|
|
|
|
|
required bool isUser,
|
|
|
|
|
|
}) {
|
|
|
|
|
|
final textColor = isUser ? Colors.white : Colors.black;
|
|
|
|
|
|
|
|
|
|
|
|
if (filePath == null) {
|
|
|
|
|
|
return Text(message ?? '', style: TextStyle(color: textColor));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
final url = "${DioClient.baseUrl}/storage/$filePath";
|
|
|
|
|
|
|
|
|
|
|
|
return GestureDetector(
|
|
|
|
|
|
onTap: () {
|
|
|
|
|
|
ChatFileViewer.open(
|
|
|
|
|
|
context,
|
|
|
|
|
|
url: url,
|
|
|
|
|
|
fileType: fileType ?? '',
|
|
|
|
|
|
);
|
|
|
|
|
|
},
|
|
|
|
|
|
child: Row(
|
|
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
|
|
children: [
|
|
|
|
|
|
Icon(_iconForFile(fileType), color: textColor),
|
|
|
|
|
|
const SizedBox(width: 8),
|
|
|
|
|
|
Text(
|
|
|
|
|
|
_labelForFile(fileType),
|
|
|
|
|
|
style: TextStyle(color: textColor),
|
|
|
|
|
|
),
|
|
|
|
|
|
],
|
|
|
|
|
|
),
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
IconData _iconForFile(String? type) {
|
|
|
|
|
|
if (type == null) return Icons.insert_drive_file;
|
|
|
|
|
|
if (type.startsWith('image/')) return Icons.image;
|
|
|
|
|
|
if (type.startsWith('video/')) return Icons.play_circle_fill;
|
|
|
|
|
|
if (type == 'application/pdf') return Icons.picture_as_pdf;
|
|
|
|
|
|
return Icons.insert_drive_file;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
String _labelForFile(String? type) {
|
|
|
|
|
|
if (type == null) return "File";
|
|
|
|
|
|
if (type.startsWith('image/')) return "Image";
|
|
|
|
|
|
if (type.startsWith('video/')) return "Video";
|
|
|
|
|
|
if (type == 'application/pdf') return "PDF";
|
|
|
|
|
|
return "Download file";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-12-15 11:10:52 +05:30
|
|
|
|
Widget _buildMessages() {
|
|
|
|
|
|
return ListView.builder(
|
|
|
|
|
|
controller: _scrollCtrl,
|
|
|
|
|
|
padding: const EdgeInsets.all(12),
|
|
|
|
|
|
itemCount: messages.length,
|
|
|
|
|
|
itemBuilder: (_, index) {
|
|
|
|
|
|
final msg = messages[index];
|
|
|
|
|
|
final isUser = msg['sender_type'] == 'App\\Models\\User';
|
|
|
|
|
|
|
2025-12-16 10:24:16 +05:30
|
|
|
|
final String? filePath = msg['file_path'];
|
|
|
|
|
|
final String? fileType = msg['file_type'];
|
|
|
|
|
|
final String? message = msg['message'];
|
|
|
|
|
|
|
2025-12-15 11:10:52 +05:30
|
|
|
|
return Align(
|
2025-12-16 10:24:16 +05:30
|
|
|
|
alignment: isUser ? Alignment.centerRight : Alignment.centerLeft,
|
2025-12-15 11:10:52 +05:30
|
|
|
|
child: Container(
|
2025-12-16 10:24:16 +05:30
|
|
|
|
margin: const EdgeInsets.symmetric(vertical: 6),
|
2025-12-15 11:10:52 +05:30
|
|
|
|
padding: const EdgeInsets.all(10),
|
|
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
|
|
color: isUser ? Colors.blue : Colors.grey.shade300,
|
|
|
|
|
|
borderRadius: BorderRadius.circular(12),
|
|
|
|
|
|
),
|
2025-12-16 10:24:16 +05:30
|
|
|
|
|
|
|
|
|
|
// 🔽 ONLY THIS PART CHANGED
|
|
|
|
|
|
child: filePath == null
|
|
|
|
|
|
? Text(
|
|
|
|
|
|
message ?? '',
|
2025-12-15 11:10:52 +05:30
|
|
|
|
style: TextStyle(
|
|
|
|
|
|
color: isUser ? Colors.white : Colors.black,
|
|
|
|
|
|
),
|
2025-12-16 10:24:16 +05:30
|
|
|
|
)
|
|
|
|
|
|
: ChatFilePreview(
|
|
|
|
|
|
filePath: filePath,
|
|
|
|
|
|
fileType: fileType ?? '',
|
|
|
|
|
|
isUser: isUser,
|
2025-12-15 11:10:52 +05:30
|
|
|
|
),
|
|
|
|
|
|
),
|
|
|
|
|
|
);
|
|
|
|
|
|
},
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Widget _buildInput() {
|
|
|
|
|
|
return SafeArea(
|
|
|
|
|
|
child: Row(
|
|
|
|
|
|
children: [
|
2025-12-16 10:24:16 +05:30
|
|
|
|
IconButton(
|
|
|
|
|
|
icon: const Icon(Icons.attach_file),
|
|
|
|
|
|
onPressed: _pickAndSendFile,
|
|
|
|
|
|
),
|
2025-12-15 11:10:52 +05:30
|
|
|
|
Expanded(
|
|
|
|
|
|
child: TextField(
|
|
|
|
|
|
controller: _messageCtrl,
|
2025-12-16 10:24:16 +05:30
|
|
|
|
decoration: const InputDecoration(
|
|
|
|
|
|
hintText: "Type message",
|
|
|
|
|
|
border: InputBorder.none,
|
|
|
|
|
|
),
|
2025-12-15 11:10:52 +05:30
|
|
|
|
),
|
|
|
|
|
|
),
|
|
|
|
|
|
IconButton(
|
|
|
|
|
|
icon: const Icon(Icons.send),
|
|
|
|
|
|
onPressed: _sendMessage,
|
|
|
|
|
|
),
|
|
|
|
|
|
],
|
|
|
|
|
|
),
|
2025-12-03 11:57:05 +05:30
|
|
|
|
);
|
|
|
|
|
|
}
|
2025-12-16 10:24:16 +05:30
|
|
|
|
|
2025-12-03 11:57:05 +05:30
|
|
|
|
}
|