Files
kent_logistics_app/lib/widgets/chat_file_preview.dart

128 lines
2.9 KiB
Dart
Raw Normal View History

2025-12-18 11:03:25 +05:30
import 'dart:io';
2025-12-16 10:24:16 +05:30
import 'package:flutter/material.dart';
import '../services/dio_client.dart';
import '../screens/chat_file_viewer.dart';
2025-12-18 11:03:25 +05:30
import '../widgets/chat_file_actions.dart';
2025-12-16 10:24:16 +05:30
class ChatFilePreview extends StatelessWidget {
final String filePath;
final String fileType;
final bool isUser;
2025-12-18 11:03:25 +05:30
final bool isLocal;
2025-12-16 10:24:16 +05:30
const ChatFilePreview({
super.key,
required this.filePath,
required this.fileType,
required this.isUser,
2025-12-18 11:03:25 +05:30
this.isLocal = false,
2025-12-16 10:24:16 +05:30
});
2025-12-18 11:03:25 +05:30
2025-12-16 10:24:16 +05:30
@override
Widget build(BuildContext context) {
final url = "${DioClient.baseUrl}/storage/$filePath";
final textColor = isUser ? Colors.white : Colors.black;
2025-12-18 11:03:25 +05:30
// LOCAL IMAGE (uploading preview)
if (isLocal && fileType.startsWith('image/')) {
return Image.file(
File(filePath),
width: 180,
height: 120,
fit: BoxFit.cover,
2025-12-16 10:24:16 +05:30
);
}
2025-12-18 11:03:25 +05:30
return GestureDetector(
onTap: () {
// ✅ TAP = OPEN
ChatFileViewer.open(
2025-12-16 10:24:16 +05:30
context,
url: url,
fileType: fileType,
2025-12-18 11:03:25 +05:30
);
},
onLongPress: () {
// ✅ LONG PRESS = OPEN / DOWNLOAD
ChatFileActions.show(
context,
url: url,
fileType: fileType,
);
},
child: _buildPreviewUI(textColor, url),
);
}
Widget _buildPreviewUI(Color textColor, String url) {
// IMAGE
if (fileType.startsWith('image/')) {
return ClipRRect(
borderRadius: BorderRadius.circular(8),
child: Image.network(
url,
width: 180,
height: 120,
fit: BoxFit.cover,
2025-12-16 10:24:16 +05:30
),
);
}
2025-12-18 11:03:25 +05:30
// VIDEO
if (fileType.startsWith('video/')) {
return Stack(
alignment: Alignment.center,
2025-12-16 10:24:16 +05:30
children: [
2025-12-18 11:03:25 +05:30
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,
2025-12-16 10:24:16 +05:30
),
],
2025-12-18 11:03:25 +05:30
);
}
// PDF / OTHER FILES
return Row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(_fileIcon(fileType), color: textColor),
const SizedBox(width: 8),
Text(
_fileLabel(fileType),
style: TextStyle(color: textColor),
),
],
2025-12-16 10:24:16 +05:30
);
}
2025-12-18 11:03:25 +05:30
2025-12-16 10:24:16 +05:30
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";
}
}