131 lines
3.1 KiB
Dart
131 lines
3.1 KiB
Dart
import 'dart:io';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:pdfrx/pdfrx.dart';
|
|
import 'package:stadtbote/content_views/utils/content_view_widget.dart';
|
|
import 'package:stadtbote/tools/pdf_service.dart';
|
|
import 'package:stadtbote/tools/util.dart';
|
|
|
|
class PdfViewerPage extends ContentViewWidget {
|
|
final String pdfUrl;
|
|
final String title;
|
|
|
|
const PdfViewerPage({
|
|
super.key,
|
|
required this.pdfUrl,
|
|
this.title = 'PDF',
|
|
});
|
|
|
|
@override
|
|
State<PdfViewerPage> createState() => _PdfViewerPageState();
|
|
|
|
@override
|
|
Future<Widget> getHeaderWidget(BuildContext context) async {
|
|
await Future.delayed(const Duration(microseconds: 0));
|
|
return Text(
|
|
title,
|
|
style: const TextStyle(
|
|
fontSize: 18,
|
|
fontWeight: FontWeight.bold,
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
class _PdfViewerPageState extends State<PdfViewerPage> {
|
|
final PdfService _pdfService = PdfService();
|
|
File? _pdfFile;
|
|
bool _isLoading = true;
|
|
String? _errorMessage;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_loadPdf();
|
|
}
|
|
|
|
Future<void> _loadPdf() async {
|
|
if (!_pdfService.isValidPdfUrl(widget.pdfUrl)) {
|
|
setState(() {
|
|
_isLoading = false;
|
|
_errorMessage = 'Ungültige PDF-URL';
|
|
});
|
|
return;
|
|
}
|
|
|
|
try {
|
|
final file = await _pdfService.getPdf(widget.pdfUrl);
|
|
setState(() {
|
|
_pdfFile = file;
|
|
_isLoading = false;
|
|
});
|
|
} catch (e) {
|
|
Util.logException('Error loading PDF: $e');
|
|
setState(() {
|
|
_isLoading = false;
|
|
_errorMessage = 'Fehler beim Laden der PDF: $e';
|
|
});
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
if (_isLoading) {
|
|
return const Center(
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
CircularProgressIndicator(),
|
|
SizedBox(height: 16),
|
|
Text('PDF wird geladen...'),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
if (_errorMessage != null) {
|
|
return Center(
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(16.0),
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
const Icon(
|
|
Icons.error_outline,
|
|
size: 64,
|
|
color: Colors.red,
|
|
),
|
|
const SizedBox(height: 16),
|
|
Text(
|
|
_errorMessage!,
|
|
textAlign: TextAlign.center,
|
|
style: const TextStyle(color: Colors.red),
|
|
),
|
|
const SizedBox(height: 16),
|
|
ElevatedButton(
|
|
onPressed: () {
|
|
setState(() {
|
|
_isLoading = true;
|
|
_errorMessage = null;
|
|
});
|
|
_loadPdf();
|
|
},
|
|
child: const Text('Erneut versuchen'),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
if (_pdfFile == null) {
|
|
return const Center(
|
|
child: Text('Keine PDF-Datei verfügbar'),
|
|
);
|
|
}
|
|
|
|
return PdfViewer.file(
|
|
_pdfFile!.path,
|
|
);
|
|
}
|
|
}
|