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!),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user