didvan-app/lib/views/pdf_viewer/pdf_viewer_page.dart

142 lines
4.2 KiB
Dart

import 'package:didvan/views/widgets/didvan/text.dart';
import 'package:flutter/material.dart';
import 'package:syncfusion_flutter_pdfviewer/pdfviewer.dart';
import 'package:flutter/foundation.dart';
import 'package:didvan/services/network/request.dart';
class PdfViewerPage extends StatefulWidget {
final String pdfUrl;
final String title;
const PdfViewerPage({
super.key,
required this.pdfUrl,
required this.title,
});
@override
State<PdfViewerPage> createState() => _PdfViewerPageState();
}
class _PdfViewerPageState extends State<PdfViewerPage> {
final PdfViewerController _pdfViewerController = PdfViewerController();
bool _isLoading = true;
String? _errorMessage;
late String _fullPdfUrl;
@override
void initState() {
super.initState();
_fullPdfUrl = widget.pdfUrl.contains('?')
? 'https://api.didvan.app${widget.pdfUrl}'
: 'https://api.didvan.app${widget.pdfUrl}';
if (kDebugMode) {
print('Original PDF URL: ${widget.pdfUrl}');
print('Full PDF URL : $_fullPdfUrl');
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: DidvanText(
widget.title,
style: const TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
),
),
centerTitle: true,
elevation: 0,
backgroundColor: Theme.of(context).colorScheme.surface,
),
body: Stack(
children: [
SfPdfViewer.network(
_fullPdfUrl,
controller: _pdfViewerController,
enableDoubleTapZooming: true,
enableTextSelection: true,
canShowScrollHead: true,
canShowScrollStatus: true,
pageLayoutMode: PdfPageLayoutMode.continuous,
headers: {
'Authorization': 'Bearer ${RequestService.token}',
},
onDocumentLoaded: (PdfDocumentLoadedDetails details) {
setState(() {
_isLoading = false;
});
if (kDebugMode) {
print('PDF loaded successfully with ${details.document.pages.count} pages');
}
},
onDocumentLoadFailed: (PdfDocumentLoadFailedDetails details) {
setState(() {
_isLoading = false;
_errorMessage = details.error;
});
if (kDebugMode) {
print('PDF load failed: ${details.error}');
print('Description: ${details.description}');
}
},
),
if (_isLoading)
const Center(
child: CircularProgressIndicator(),
),
if (_errorMessage != null)
Center(
child: Padding(
padding: const EdgeInsets.all(20.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Icon(
Icons.error_outline,
size: 64,
color: Colors.red,
),
const SizedBox(height: 16),
const DidvanText(
'خطا در بارگذاری PDF',
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 8),
DidvanText(
_errorMessage!,
textAlign: TextAlign.center,
style: const TextStyle(fontSize: 14),
),
const SizedBox(height: 16),
ElevatedButton(
onPressed: () {
setState(() {
_isLoading = true;
_errorMessage = null;
});
},
child: const DidvanText('تلاش مجدد'),
),
],
),
),
),
],
),
);
}
@override
void dispose() {
_pdfViewerController.dispose();
super.dispose();
}
}