From a88121d46533824d9ea4d3e98c6c3149b0f1e527 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leon=20B=C3=B6sche?= Date: Sun, 25 Jan 2026 15:23:01 +0100 Subject: [PATCH] Fix PDF loading in public file viewer - Change PDF viewer to use SfPdfViewer.network instead of loading bytes - Remove _loadPdfBytes method and _pdfBytes variable - Use direct network loading for better performance and reliability - Add onDocumentLoadFailed callback for error handling - Remove unused dart:typed_data import --- .../lib/pages/public_file_viewer.dart | 90 +++++-------------- 1 file changed, 23 insertions(+), 67 deletions(-) diff --git a/b0esche_cloud/lib/pages/public_file_viewer.dart b/b0esche_cloud/lib/pages/public_file_viewer.dart index 41b3e04..c945d34 100644 --- a/b0esche_cloud/lib/pages/public_file_viewer.dart +++ b/b0esche_cloud/lib/pages/public_file_viewer.dart @@ -4,7 +4,6 @@ import 'package:web/web.dart' as web; import 'dart:ui_web' as ui_web; import 'package:syncfusion_flutter_core/theme.dart'; import 'package:syncfusion_flutter_pdfviewer/pdfviewer.dart'; -import 'dart:typed_data'; import 'package:video_player/video_player.dart'; import 'package:flutter/foundation.dart' show kIsWeb; import '../theme/app_theme.dart'; @@ -27,7 +26,6 @@ class _PublicFileViewerState extends State { String? _error; Map? _fileData; VideoPlayerController? _videoController; - List? _pdfBytes; String? _videoViewType; @override @@ -52,11 +50,6 @@ class _PublicFileViewerState extends State { _isLoading = false; }); - // Load PDF bytes if it's a PDF file - if (_isPdfFile()) { - await _loadPdfBytes(); - } - // Initialize video player if it's a video file if (_isVideoFile()) { await _initializeVideoPlayer(); @@ -69,28 +62,6 @@ class _PublicFileViewerState extends State { } } - Future _loadPdfBytes() async { - if (_fileData?['viewUrl'] != null) { - try { - final apiClient = getIt(); - // 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 _initializeVideoPlayer() async { if (!kIsWeb) { // For mobile, use VideoPlayerController @@ -185,49 +156,34 @@ class _PublicFileViewerState extends State { if (viewUrl == null) return const SizedBox(); if (_isPdfFile()) { - if (_pdfBytes != null) { - return Expanded( - child: SfTheme( - data: SfThemeData( - pdfViewerThemeData: SfPdfViewerThemeData( + return Expanded( + child: SfTheme( + data: SfThemeData( + pdfViewerThemeData: SfPdfViewerThemeData( + backgroundColor: AppTheme.primaryBackground, + progressBarColor: AppTheme.accentColor, + scrollStatusStyle: PdfScrollStatusStyle( backgroundColor: AppTheme.primaryBackground, - progressBarColor: AppTheme.accentColor, - scrollStatusStyle: PdfScrollStatusStyle( - backgroundColor: AppTheme.primaryBackground, - ), - scrollHeadStyle: PdfScrollHeadStyle( - backgroundColor: AppTheme.accentColor, - ), + ), + scrollHeadStyle: PdfScrollHeadStyle( + backgroundColor: AppTheme.accentColor, ), ), - child: SfPdfViewer.memory( - Uint8List.fromList(_pdfBytes!), - canShowScrollHead: false, - canShowScrollStatus: false, - enableDoubleTapZooming: true, - enableTextSelection: false, - ), ), - ); - } else if (_error != null) { - return Expanded( - child: Center( - child: Text( - _error!, - style: TextStyle(color: AppTheme.primaryText), - textAlign: TextAlign.center, - ), + child: SfPdfViewer.network( + viewUrl, + canShowScrollHead: false, + canShowScrollStatus: false, + enableDoubleTapZooming: true, + enableTextSelection: false, + onDocumentLoadFailed: (details) { + setState(() { + _error = 'Failed to load PDF content.'; + }); + }, ), - ); - } else { - return const Expanded( - child: Center( - child: CircularProgressIndicator( - valueColor: AlwaysStoppedAnimation(AppTheme.accentColor), - ), - ), - ); - } + ), + ); } else if (_isVideoFile()) { if (kIsWeb && _videoViewType != null) { // Use HTML video element for web