chat support download updated
This commit is contained in:
118
lib/widgets/chat_file_actions.dart
Normal file
118
lib/widgets/chat_file_actions.dart
Normal file
@@ -0,0 +1,118 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import '../screens/chat_file_viewer.dart';
|
||||
|
||||
class ChatFileActions {
|
||||
static void show(
|
||||
BuildContext context, {
|
||||
required String url,
|
||||
required String fileType,
|
||||
}) {
|
||||
showModalBottomSheet(
|
||||
context: context,
|
||||
shape: const RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.vertical(top: Radius.circular(16)),
|
||||
),
|
||||
builder: (_) {
|
||||
return SafeArea(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
_actionTile(
|
||||
icon: Icons.open_in_new,
|
||||
title: "Open",
|
||||
onTap: () {
|
||||
Navigator.pop(context);
|
||||
ChatFileViewer.open(
|
||||
context,
|
||||
url: url,
|
||||
fileType: fileType,
|
||||
);
|
||||
},
|
||||
),
|
||||
_actionTile(
|
||||
icon: Icons.download,
|
||||
title: "Download",
|
||||
onTap: () async {
|
||||
Navigator.pop(context);
|
||||
await _downloadFile(context, url, fileType);
|
||||
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
],
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
static Widget _actionTile({
|
||||
required IconData icon,
|
||||
required String title,
|
||||
required VoidCallback onTap,
|
||||
}) {
|
||||
return ListTile(
|
||||
leading: Icon(icon),
|
||||
title: Text(title),
|
||||
onTap: onTap,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
static Future<void> _scanFile(String path) async {
|
||||
const platform = MethodChannel('media_scanner');
|
||||
|
||||
try {
|
||||
await platform.invokeMethod('scanFile', {'path': path});
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
|
||||
// ===========================
|
||||
// DOWNLOAD LOGIC
|
||||
// ===========================
|
||||
static Future<void> _downloadFile(
|
||||
BuildContext context,
|
||||
String url,
|
||||
String fileType,
|
||||
) async {
|
||||
try {
|
||||
final fileName = url.split('/').last;
|
||||
|
||||
Directory baseDir;
|
||||
|
||||
if (fileType.startsWith('image/')) {
|
||||
baseDir = Directory('/storage/emulated/0/Pictures/KentChat');
|
||||
} else if (fileType.startsWith('video/')) {
|
||||
baseDir = Directory('/storage/emulated/0/Movies/KentChat');
|
||||
} else {
|
||||
baseDir = Directory('/storage/emulated/0/Download/KentChat');
|
||||
}
|
||||
|
||||
if (!await baseDir.exists()) {
|
||||
await baseDir.create(recursive: true);
|
||||
}
|
||||
|
||||
final filePath = "${baseDir.path}/$fileName";
|
||||
|
||||
await Dio().download(url, filePath);
|
||||
|
||||
// 🔔 IMPORTANT: Tell Android to scan the file
|
||||
await _scanFile(filePath);
|
||||
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text("Saved to ${baseDir.path}")),
|
||||
);
|
||||
} catch (e) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(content: Text("Download failed")),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user