| .. | .. |
|---|
| 1 | +import 'dart:io'; |
|---|
| 1 | 2 | import 'dart:typed_data'; |
|---|
| 2 | 3 | |
|---|
| 3 | 4 | import 'package:flutter/material.dart'; |
|---|
| 5 | +import 'package:path_provider/path_provider.dart'; |
|---|
| 4 | 6 | import 'package:pdfrx/pdfrx.dart'; |
|---|
| 7 | +import 'package:share_plus/share_plus.dart'; |
|---|
| 5 | 8 | |
|---|
| 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. |
|---|
| 7 | 10 | class PdfViewerScreen extends StatelessWidget { |
|---|
| 8 | 11 | final Uint8List pdfBytes; |
|---|
| 9 | 12 | final String title; |
|---|
| .. | .. |
|---|
| 14 | 17 | this.title = 'PDF', |
|---|
| 15 | 18 | }); |
|---|
| 16 | 19 | |
|---|
| 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 | + |
|---|
| 17 | 39 | @override |
|---|
| 18 | 40 | Widget build(BuildContext context) { |
|---|
| 19 | 41 | return Scaffold( |
|---|
| .. | .. |
|---|
| 21 | 43 | title: Text(title, style: const TextStyle(fontSize: 16)), |
|---|
| 22 | 44 | backgroundColor: Colors.black87, |
|---|
| 23 | 45 | foregroundColor: Colors.white, |
|---|
| 46 | + actions: [ |
|---|
| 47 | + IconButton( |
|---|
| 48 | + icon: const Icon(Icons.ios_share), |
|---|
| 49 | + tooltip: 'Share / Save', |
|---|
| 50 | + onPressed: () => _share(context), |
|---|
| 51 | + ), |
|---|
| 52 | + ], |
|---|
| 24 | 53 | ), |
|---|
| 25 | 54 | backgroundColor: Colors.grey[900], |
|---|
| 26 | 55 | body: PdfViewer.data(pdfBytes, sourceName: title), |
|---|