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, page navigation, and share/save. class PdfViewerScreen extends StatelessWidget { final Uint8List pdfBytes; final String title; const PdfViewerScreen({ super.key, required this.pdfBytes, this.title = 'PDF', }); Future _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( appBar: AppBar( 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), ); } }