chat support update
This commit is contained in:
145
lib/screens/chat_file_viewer.dart
Normal file
145
lib/screens/chat_file_viewer.dart
Normal file
@@ -0,0 +1,145 @@
|
||||
import 'dart:io';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:photo_view/photo_view.dart';
|
||||
import 'package:video_player/video_player.dart';
|
||||
import 'package:chewie/chewie.dart';
|
||||
import 'package:flutter_pdfview/flutter_pdfview.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'package:dio/dio.dart';
|
||||
|
||||
class ChatFileViewer {
|
||||
/// Entry point used by chat screen
|
||||
static void open(
|
||||
BuildContext context, {
|
||||
required String url,
|
||||
required String fileType,
|
||||
}) {
|
||||
if (fileType.startsWith('image/')) {
|
||||
_openImage(context, url);
|
||||
} else if (fileType.startsWith('video/')) {
|
||||
_openVideo(context, url);
|
||||
} else if (fileType == 'application/pdf') {
|
||||
_openPdf(context, url);
|
||||
} else {
|
||||
_downloadFile(context, url);
|
||||
}
|
||||
}
|
||||
|
||||
// ===========================
|
||||
// IMAGE VIEWER
|
||||
// ===========================
|
||||
static void _openImage(BuildContext context, String url) {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (_) => Scaffold(
|
||||
backgroundColor: Colors.black,
|
||||
appBar: AppBar(backgroundColor: Colors.black),
|
||||
body: PhotoView(
|
||||
imageProvider: NetworkImage(url),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// ===========================
|
||||
// VIDEO VIEWER
|
||||
// ===========================
|
||||
static void _openVideo(BuildContext context, String url) {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (_) => _VideoPlayerPage(url: url),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// ===========================
|
||||
// PDF VIEWER
|
||||
// ===========================
|
||||
static Future<void> _openPdf(
|
||||
BuildContext context, String url) async {
|
||||
final dir = await getTemporaryDirectory();
|
||||
final path = "${dir.path}/chat.pdf";
|
||||
|
||||
await Dio().download(url, path);
|
||||
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (_) => Scaffold(
|
||||
appBar: AppBar(title: const Text("PDF")),
|
||||
body: PDFView(filePath: path),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// ===========================
|
||||
// FILE DOWNLOAD
|
||||
// ===========================
|
||||
static Future<void> _downloadFile(
|
||||
BuildContext context, String url) async {
|
||||
final dir = await getExternalStorageDirectory();
|
||||
final fileName = url.split('/').last;
|
||||
final path = "${dir!.path}/$fileName";
|
||||
|
||||
await Dio().download(url, path);
|
||||
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text("File downloaded: $fileName")),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// ===========================
|
||||
// VIDEO PLAYER WIDGET
|
||||
// ===========================
|
||||
class _VideoPlayerPage extends StatefulWidget {
|
||||
final String url;
|
||||
const _VideoPlayerPage({required this.url});
|
||||
|
||||
@override
|
||||
State<_VideoPlayerPage> createState() => _VideoPlayerPageState();
|
||||
}
|
||||
|
||||
class _VideoPlayerPageState extends State<_VideoPlayerPage> {
|
||||
late VideoPlayerController _videoController;
|
||||
ChewieController? _chewieController;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
|
||||
_videoController = VideoPlayerController.network(widget.url)
|
||||
..initialize().then((_) {
|
||||
setState(() {
|
||||
_chewieController = ChewieController(
|
||||
videoPlayerController: _videoController,
|
||||
autoPlay: true,
|
||||
looping: false,
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_videoController.dispose();
|
||||
_chewieController?.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: const Text("Video")),
|
||||
body: Center(
|
||||
child: _chewieController == null
|
||||
? const CircularProgressIndicator()
|
||||
: Chewie(controller: _chewieController!),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,14 @@ import '../services/chat_service.dart';
|
||||
import '../services/reverb_socket_service.dart';
|
||||
import '../services/dio_client.dart';
|
||||
import '../providers/chat_unread_provider.dart';
|
||||
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';
|
||||
|
||||
|
||||
|
||||
|
||||
class ChatScreen extends StatefulWidget {
|
||||
const ChatScreen({super.key});
|
||||
@@ -41,6 +49,17 @@ class _ChatScreenState extends State<ChatScreen> {
|
||||
_initChat();
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
// ============================
|
||||
// INIT CHAT
|
||||
// ============================
|
||||
@@ -135,6 +154,77 @@ class _ChatScreenState extends State<ChatScreen> {
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
// 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";
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Widget _buildMessages() {
|
||||
return ListView.builder(
|
||||
controller: _scrollCtrl,
|
||||
@@ -144,21 +234,32 @@ class _ChatScreenState extends State<ChatScreen> {
|
||||
final msg = messages[index];
|
||||
final isUser = msg['sender_type'] == 'App\\Models\\User';
|
||||
|
||||
final String? filePath = msg['file_path'];
|
||||
final String? fileType = msg['file_type'];
|
||||
final String? message = msg['message'];
|
||||
|
||||
return Align(
|
||||
alignment:
|
||||
isUser ? Alignment.centerRight : Alignment.centerLeft,
|
||||
alignment: isUser ? Alignment.centerRight : Alignment.centerLeft,
|
||||
child: Container(
|
||||
margin: const EdgeInsets.symmetric(vertical: 4),
|
||||
margin: const EdgeInsets.symmetric(vertical: 6),
|
||||
padding: const EdgeInsets.all(10),
|
||||
decoration: BoxDecoration(
|
||||
color: isUser ? Colors.blue : Colors.grey.shade300,
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
child: Text(
|
||||
msg['message'] ?? '',
|
||||
|
||||
// 🔽 ONLY THIS PART CHANGED
|
||||
child: filePath == null
|
||||
? Text(
|
||||
message ?? '',
|
||||
style: TextStyle(
|
||||
color: isUser ? Colors.white : Colors.black,
|
||||
),
|
||||
)
|
||||
: ChatFilePreview(
|
||||
filePath: filePath,
|
||||
fileType: fileType ?? '',
|
||||
isUser: isUser,
|
||||
),
|
||||
),
|
||||
);
|
||||
@@ -170,11 +271,17 @@ class _ChatScreenState extends State<ChatScreen> {
|
||||
return SafeArea(
|
||||
child: Row(
|
||||
children: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.attach_file),
|
||||
onPressed: _pickAndSendFile,
|
||||
),
|
||||
Expanded(
|
||||
child: TextField(
|
||||
controller: _messageCtrl,
|
||||
decoration:
|
||||
const InputDecoration(hintText: "Type message"),
|
||||
decoration: const InputDecoration(
|
||||
hintText: "Type message",
|
||||
border: InputBorder.none,
|
||||
),
|
||||
),
|
||||
),
|
||||
IconButton(
|
||||
@@ -185,4 +292,5 @@ class _ChatScreenState extends State<ChatScreen> {
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
import '../config/api_config.dart';
|
||||
|
||||
@@ -38,4 +40,25 @@ class ChatService {
|
||||
|
||||
await dio.post('/user/chat/send/$ticketId', data: form);
|
||||
}
|
||||
|
||||
// ---------------------------
|
||||
// SEND FILE (image/video/pdf/excel)
|
||||
// ---------------------------
|
||||
Future<void> sendFile(int ticketId, File file) async {
|
||||
final formData = FormData.fromMap({
|
||||
'file': await MultipartFile.fromFile(
|
||||
file.path,
|
||||
filename: file.path.split('/').last,
|
||||
),
|
||||
});
|
||||
|
||||
await dio.post(
|
||||
"/user/chat/send/$ticketId",
|
||||
|
||||
data: formData,
|
||||
options: Options(
|
||||
headers: {'Content-Type': 'multipart/form-data'},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,9 @@ import '../providers/auth_provider.dart';
|
||||
import 'token_interceptor.dart';
|
||||
|
||||
class DioClient {
|
||||
static Dio? _dio; // Singleton instance
|
||||
static Dio? _dio;
|
||||
|
||||
static const String baseUrl = "http://10.11.236.74:8000";
|
||||
|
||||
static Dio getInstance(BuildContext context) {
|
||||
if (_dio == null) {
|
||||
|
||||
108
lib/widgets/chat_file_preview.dart
Normal file
108
lib/widgets/chat_file_preview.dart
Normal file
@@ -0,0 +1,108 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import '../services/dio_client.dart';
|
||||
import '../screens/chat_file_viewer.dart';
|
||||
|
||||
class ChatFilePreview extends StatelessWidget {
|
||||
final String filePath;
|
||||
final String fileType;
|
||||
final bool isUser;
|
||||
|
||||
const ChatFilePreview({
|
||||
super.key,
|
||||
required this.filePath,
|
||||
required this.fileType,
|
||||
required this.isUser,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final url = "${DioClient.baseUrl}/storage/$filePath";
|
||||
final textColor = isUser ? Colors.white : Colors.black;
|
||||
|
||||
// IMAGE PREVIEW
|
||||
if (fileType.startsWith('image/')) {
|
||||
return GestureDetector(
|
||||
onTap: () => ChatFileViewer.open(
|
||||
context,
|
||||
url: url,
|
||||
fileType: fileType,
|
||||
),
|
||||
child: ClipRRect(
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
child: Image.network(
|
||||
url,
|
||||
width: 180,
|
||||
height: 120,
|
||||
fit: BoxFit.cover,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// VIDEO PREVIEW
|
||||
if (fileType.startsWith('video/')) {
|
||||
return GestureDetector(
|
||||
onTap: () => ChatFileViewer.open(
|
||||
context,
|
||||
url: url,
|
||||
fileType: fileType,
|
||||
),
|
||||
child: Stack(
|
||||
alignment: Alignment.center,
|
||||
children: [
|
||||
Container(
|
||||
width: 180,
|
||||
height: 120,
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.black26,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: const Icon(
|
||||
Icons.videocam,
|
||||
size: 50,
|
||||
color: Colors.white70,
|
||||
),
|
||||
),
|
||||
const Icon(
|
||||
Icons.play_circle_fill,
|
||||
size: 56,
|
||||
color: Colors.white,
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// PDF / FILE PREVIEW
|
||||
return GestureDetector(
|
||||
onTap: () => ChatFileViewer.open(
|
||||
context,
|
||||
url: url,
|
||||
fileType: fileType,
|
||||
),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(_fileIcon(fileType), color: textColor),
|
||||
const SizedBox(width: 8),
|
||||
Text(
|
||||
_fileLabel(fileType),
|
||||
style: TextStyle(color: textColor),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
IconData _fileIcon(String type) {
|
||||
if (type == 'application/pdf') return Icons.picture_as_pdf;
|
||||
if (type.contains('excel')) return Icons.table_chart;
|
||||
return Icons.insert_drive_file;
|
||||
}
|
||||
|
||||
String _fileLabel(String type) {
|
||||
if (type == 'application/pdf') return "PDF document";
|
||||
if (type.contains('excel')) return "Excel file";
|
||||
return "Download file";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user