handlePdfExport function

Future<void> handlePdfExport(
  1. BuildContext context,
  2. Uint8List pdfBytes
)

Handle PDF export with platform-specific save dialog.

Implementation

Future<void> handlePdfExport(BuildContext context, Uint8List pdfBytes) async {
  final filename =
      'weather_report_${DateFormat('yyyyMMdd_HHmmss').format(DateTime.now())}';

  if (kIsWeb) {
    // For Web: Download PDF file directly.
    downloadPdfWeb(pdfBytes, '$filename.pdf');

    if (context.mounted) {
      SnackBarHelper.showSuccess(
        context,
        'PDF downloaded successfully',
        duration: const Duration(seconds: 2),
      );
    }
  } else {
    // For mobile/desktop: Let user choose save location.
    final outputPath = await FilePicker.platform.saveFile(
      dialogTitle: 'Save PDF Report',
      fileName: '$filename.pdf',
      type: FileType.custom,
      allowedExtensions: ['pdf'],
    );

    if (outputPath != null) {
      // Save the file to the chosen location.
      final file = File(outputPath);
      await file.writeAsBytes(pdfBytes);

      if (context.mounted) {
        SnackBarHelper.showSuccess(
          context,
          'PDF saved to: $outputPath',
          duration: const Duration(seconds: 3),
        );
      }
    } else {
      // User cancelled the save dialog.
      if (context.mounted) {
        SnackBarHelper.showInfo(
          context,
          'PDF export cancelled',
          duration: const Duration(seconds: 2),
        );
      }
    }
  }
}