Matthias Nott
3 days ago a526ea4ce4a6da31222f73ca12c8dd9017fb2410
lib/widgets/pdf_viewer.dart
....@@ -1,9 +1,12 @@
1
+import 'dart:io';
12 import 'dart:typed_data';
23
34 import 'package:flutter/material.dart';
5
+import 'package:path_provider/path_provider.dart';
46 import 'package:pdfrx/pdfrx.dart';
7
+import 'package:share_plus/share_plus.dart';
58
6
-/// Full-screen PDF viewer with pinch-to-zoom and page navigation.
9
+/// Full-screen PDF viewer with pinch-to-zoom, page navigation, and share/save.
710 class PdfViewerScreen extends StatelessWidget {
811 final Uint8List pdfBytes;
912 final String title;
....@@ -14,6 +17,25 @@
1417 this.title = 'PDF',
1518 });
1619
20
+ Future<void> _share(BuildContext context) async {
21
+ try {
22
+ final dir = await getTemporaryDirectory();
23
+ final safeName = title.replaceAll(RegExp(r'[^\w\s.-]'), '').trim();
24
+ final fileName = safeName.endsWith('.pdf') ? safeName : '$safeName.pdf';
25
+ final file = File('${dir.path}/$fileName');
26
+ await file.writeAsBytes(pdfBytes);
27
+ await SharePlus.instance.share(
28
+ ShareParams(files: [XFile(file.path, mimeType: 'application/pdf')]),
29
+ );
30
+ } catch (e) {
31
+ if (context.mounted) {
32
+ ScaffoldMessenger.of(context).showSnackBar(
33
+ SnackBar(content: Text('Share failed: $e')),
34
+ );
35
+ }
36
+ }
37
+ }
38
+
1739 @override
1840 Widget build(BuildContext context) {
1941 return Scaffold(
....@@ -21,6 +43,13 @@
2143 title: Text(title, style: const TextStyle(fontSize: 16)),
2244 backgroundColor: Colors.black87,
2345 foregroundColor: Colors.white,
46
+ actions: [
47
+ IconButton(
48
+ icon: const Icon(Icons.ios_share),
49
+ tooltip: 'Share / Save',
50
+ onPressed: () => _share(context),
51
+ ),
52
+ ],
2453 ),
2554 backgroundColor: Colors.grey[900],
2655 body: PdfViewer.data(pdfBytes, sourceName: title),