From a526ea4ce4a6da31222f73ca12c8dd9017fb2410 Mon Sep 17 00:00:00 2001
From: Matthias Nott <mnott@mnsoft.org>
Date: Sat, 11 Apr 2026 09:41:41 +0200
Subject: [PATCH] feat: add share/save button to PDF viewer

---
 lib/widgets/pdf_viewer.dart |   31 ++++++++++++++++++++++++++++++-
 1 files changed, 30 insertions(+), 1 deletions(-)

diff --git a/lib/widgets/pdf_viewer.dart b/lib/widgets/pdf_viewer.dart
index ecc3a1c..f861335 100644
--- a/lib/widgets/pdf_viewer.dart
+++ b/lib/widgets/pdf_viewer.dart
@@ -1,9 +1,12 @@
+import 'dart:io';
 import 'dart:typed_data';
 
 import 'package:flutter/material.dart';
+import 'package:path_provider/path_provider.dart';
 import 'package:pdfrx/pdfrx.dart';
+import 'package:share_plus/share_plus.dart';
 
-/// Full-screen PDF viewer with pinch-to-zoom and page navigation.
+/// Full-screen PDF viewer with pinch-to-zoom, page navigation, and share/save.
 class PdfViewerScreen extends StatelessWidget {
   final Uint8List pdfBytes;
   final String title;
@@ -14,6 +17,25 @@
     this.title = 'PDF',
   });
 
+  Future<void> _share(BuildContext context) async {
+    try {
+      final dir = await getTemporaryDirectory();
+      final safeName = title.replaceAll(RegExp(r'[^\w\s.-]'), '').trim();
+      final fileName = safeName.endsWith('.pdf') ? safeName : '$safeName.pdf';
+      final file = File('${dir.path}/$fileName');
+      await file.writeAsBytes(pdfBytes);
+      await SharePlus.instance.share(
+        ShareParams(files: [XFile(file.path, mimeType: 'application/pdf')]),
+      );
+    } catch (e) {
+      if (context.mounted) {
+        ScaffoldMessenger.of(context).showSnackBar(
+          SnackBar(content: Text('Share failed: $e')),
+        );
+      }
+    }
+  }
+
   @override
   Widget build(BuildContext context) {
     return Scaffold(
@@ -21,6 +43,13 @@
         title: Text(title, style: const TextStyle(fontSize: 16)),
         backgroundColor: Colors.black87,
         foregroundColor: Colors.white,
+        actions: [
+          IconButton(
+            icon: const Icon(Icons.ios_share),
+            tooltip: 'Share / Save',
+            onPressed: () => _share(context),
+          ),
+        ],
       ),
       backgroundColor: Colors.grey[900],
       body: PdfViewer.data(pdfBytes, sourceName: title),

--
Gitblit v1.3.1