import 'dart:io'; import 'package:flutter/material.dart'; import 'package:path_provider/path_provider.dart'; import 'package:pdf/widgets.dart' as pw; import 'package:share_plus/share_plus.dart'; import '../services/dio_client.dart'; import '../services/invoice_service.dart'; import '../widgets/invoice_detail_view.dart'; class InvoiceDetailScreen extends StatefulWidget { final int invoiceId; const InvoiceDetailScreen({super.key, required this.invoiceId}); @override State createState() => _InvoiceDetailScreenState(); } class _InvoiceDetailScreenState extends State { bool loading = true; Map invoice = {}; @override void initState() { super.initState(); load(); } // ------------------------------------------------------- // ⭐ LOAD INVOICE FROM API // ------------------------------------------------------- Future load() async { final service = InvoiceService(DioClient.getInstance(context)); try { final res = await service.getInvoiceDetails(widget.invoiceId); if (res['success'] == true) { invoice = res['invoice'] ?? {}; } else { invoice = {}; } } catch (e) { invoice = {}; } finally { if (mounted) setState(() => loading = false); } } // ------------------------------------------------------- // ⭐ GENERATE + SAVE PDF TO DOWNLOADS FOLDER // (No Permission Needed) // ------------------------------------------------------- Future generatePDF() async { final pdf = pw.Document(); pdf.addPage( pw.Page( build: (context) => pw.Column( crossAxisAlignment: pw.CrossAxisAlignment.start, children: [ pw.Text( "INVOICE DETAILS", style: pw.TextStyle(fontSize: 26, fontWeight: pw.FontWeight.bold), ), pw.SizedBox(height: 20), pw.Text("Invoice ID: ${invoice['id'] ?? '-'}"), pw.Text("Amount: ₹${invoice['amount'] ?? '-'}"), pw.Text("Status: ${invoice['status'] ?? '-'}"), pw.Text("Date: ${invoice['date'] ?? '-'}"), pw.Text("Customer: ${invoice['customer_name'] ?? '-'}"), ], ), ), ); // ⭐ SAFEST WAY (Android 10–14) final downloadsDir = await getDownloadsDirectory(); final filePath = "${downloadsDir!.path}/invoice_${invoice['id']}.pdf"; final file = File(filePath); await file.writeAsBytes(await pdf.save()); if (mounted) { ScaffoldMessenger.of(context).showSnackBar( SnackBar(content: Text("PDF saved to Downloads:\n$filePath")), ); } return file; } // ------------------------------------------------------- // ⭐ SHARE THE SAVED PDF FILE // ------------------------------------------------------- Future sharePDF() async { final file = await generatePDF(); await Share.shareXFiles( [XFile(file.path)], text: "Invoice Details", ); } @override Widget build(BuildContext context) { final width = MediaQuery.of(context).size.width; final scale = (width / 430).clamp(0.88, 1.08); return Scaffold( appBar: AppBar( title: Text( "Invoice Details", style: TextStyle( fontSize: 18 * scale, fontWeight: FontWeight.w600, ), ), // ⭐ PDF + SHARE BUTTONS actions: [ IconButton( icon: const Icon(Icons.picture_as_pdf), onPressed: generatePDF, ), IconButton( icon: const Icon(Icons.share), onPressed: sharePDF, ), ], ), body: loading ? const Center(child: CircularProgressIndicator()) : invoice.isEmpty ? Center( child: Text( "No Invoice Data Found", style: TextStyle( fontSize: 18 * scale, fontWeight: FontWeight.w600, color: Colors.black87, ), ), ) : Padding( padding: EdgeInsets.all(12 * scale), child: InvoiceDetailView(invoice: invoice), ), ); } }