Fix PDF loading: fetch PDF bytes directly and use SfPdfViewer.memory()
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
import 'dart:typed_data';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:go_router/go_router.dart';
|
import 'package:go_router/go_router.dart';
|
||||||
import 'package:web/web.dart' as web;
|
import 'package:web/web.dart' as web;
|
||||||
@@ -23,6 +24,7 @@ class _PublicFileViewerState extends State<PublicFileViewer> {
|
|||||||
String? _error;
|
String? _error;
|
||||||
Map<String, dynamic>? _fileData;
|
Map<String, dynamic>? _fileData;
|
||||||
VideoPlayerController? _videoController;
|
VideoPlayerController? _videoController;
|
||||||
|
List<int>? _pdfBytes;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
@@ -46,6 +48,11 @@ class _PublicFileViewerState extends State<PublicFileViewer> {
|
|||||||
_isLoading = false;
|
_isLoading = false;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Load PDF bytes if it's a PDF file
|
||||||
|
if (_isPdfFile()) {
|
||||||
|
await _loadPdfBytes();
|
||||||
|
}
|
||||||
|
|
||||||
// Initialize video controller if it's a video file
|
// Initialize video controller if it's a video file
|
||||||
if (_isVideoFile()) {
|
if (_isVideoFile()) {
|
||||||
_initializeVideoPlayer();
|
_initializeVideoPlayer();
|
||||||
@@ -58,6 +65,28 @@ class _PublicFileViewerState extends State<PublicFileViewer> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<void> _loadPdfBytes() async {
|
||||||
|
if (_fileData?['viewUrl'] != null) {
|
||||||
|
try {
|
||||||
|
final apiClient = getIt<ApiClient>();
|
||||||
|
// Extract the path from viewUrl and call it directly
|
||||||
|
final viewUrl = _fileData!['viewUrl'] as String;
|
||||||
|
final uri = Uri.parse(viewUrl);
|
||||||
|
final path = uri.path + (uri.query.isNotEmpty ? '?${uri.query}' : '');
|
||||||
|
|
||||||
|
final bytes = await apiClient.getBytes(path);
|
||||||
|
setState(() {
|
||||||
|
_pdfBytes = bytes;
|
||||||
|
});
|
||||||
|
} catch (e) {
|
||||||
|
// If loading fails, we'll show an error or fallback
|
||||||
|
setState(() {
|
||||||
|
_error = 'Failed to load PDF content.';
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Future<void> _initializeVideoPlayer() async {
|
Future<void> _initializeVideoPlayer() async {
|
||||||
final url = _fileData?['viewUrl'] ?? _fileData?['downloadUrl'];
|
final url = _fileData?['viewUrl'] ?? _fileData?['downloadUrl'];
|
||||||
if (url != null) {
|
if (url != null) {
|
||||||
@@ -115,15 +144,35 @@ class _PublicFileViewerState extends State<PublicFileViewer> {
|
|||||||
if (viewUrl == null) return const SizedBox();
|
if (viewUrl == null) return const SizedBox();
|
||||||
|
|
||||||
if (_isPdfFile()) {
|
if (_isPdfFile()) {
|
||||||
return Expanded(
|
if (_pdfBytes != null) {
|
||||||
child: SfPdfViewer.network(
|
return Expanded(
|
||||||
viewUrl,
|
child: SfPdfViewer.memory(
|
||||||
canShowScrollHead: false,
|
Uint8List.fromList(_pdfBytes!),
|
||||||
canShowScrollStatus: false,
|
canShowScrollHead: false,
|
||||||
enableDoubleTapZooming: true,
|
canShowScrollStatus: false,
|
||||||
enableTextSelection: false,
|
enableDoubleTapZooming: true,
|
||||||
),
|
enableTextSelection: false,
|
||||||
);
|
),
|
||||||
|
);
|
||||||
|
} else if (_error != null) {
|
||||||
|
return Expanded(
|
||||||
|
child: Center(
|
||||||
|
child: Text(
|
||||||
|
_error!,
|
||||||
|
style: TextStyle(color: AppTheme.primaryText),
|
||||||
|
textAlign: TextAlign.center,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
return const Expanded(
|
||||||
|
child: Center(
|
||||||
|
child: CircularProgressIndicator(
|
||||||
|
valueColor: AlwaysStoppedAnimation<Color>(AppTheme.accentColor),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
} else if (_isVideoFile() && _videoController != null) {
|
} else if (_isVideoFile() && _videoController != null) {
|
||||||
return Expanded(
|
return Expanded(
|
||||||
child: AspectRatio(
|
child: AspectRatio(
|
||||||
|
|||||||
@@ -97,6 +97,18 @@ class ApiClient {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<List<int>> getBytes(String path) async {
|
||||||
|
try {
|
||||||
|
final response = await _dio.get(
|
||||||
|
path,
|
||||||
|
options: Options(responseType: ResponseType.bytes),
|
||||||
|
);
|
||||||
|
return response.data;
|
||||||
|
} on DioException catch (e) {
|
||||||
|
throw _handleError(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Future<Map<String, dynamic>> postRaw(String path, {dynamic data}) async {
|
Future<Map<String, dynamic>> postRaw(String path, {dynamic data}) async {
|
||||||
try {
|
try {
|
||||||
final response = await _dio.post(path, data: data);
|
final response = await _dio.post(path, data: data);
|
||||||
|
|||||||
Reference in New Issue
Block a user