MIF_E31222656/lib/utils/pdf_test.dart

195 lines
5.7 KiB
Dart

import 'package:flutter/material.dart';
import 'dart:io';
import 'pdf_generator.dart';
class PdfTest {
static Future<void> testPdfGeneration(BuildContext context) async {
try {
// Show loading indicator
showDialog(
context: context,
barrierDismissible: false,
builder: (context) => const Center(
child: CircularProgressIndicator(),
),
);
// Create test data
final testData = {
'productivity': 5.5,
'cost': 5000000.0,
'income': 8000000.0,
'profit': 3000000.0,
'profit_margin': 60.0,
'status': 'Baik',
'seed_cost': 1000000.0,
'fertilizer_cost': 1500000.0,
'pesticide_cost': 800000.0,
'labor_cost': 1200000.0,
'irrigation_cost': 500000.0,
'crop_name': 'Padi Test',
};
// Generate PDF
final pdfGenerator = HarvestPdfGenerator();
final pdfFile = await pdfGenerator.generatePdf(
title: 'Test PDF',
harvestData: testData,
scheduleData: null,
dailyLogs: null,
);
// Close loading dialog
if (!context.mounted) return;
Navigator.pop(context);
// Show success dialog
showDialog(
context: context,
builder: (context) => AlertDialog(
title: const Text('PDF Berhasil Dibuat'),
content: SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text('PDF disimpan di:'),
const SizedBox(height: 8),
Text(
pdfFile.path,
style: const TextStyle(fontWeight: FontWeight.bold),
),
const SizedBox(height: 16),
const Text(
'Catatan: PDF disimpan di direktori internal aplikasi yang tidak memerlukan izin khusus.',
style: TextStyle(fontSize: 12, fontStyle: FontStyle.italic),
),
],
),
),
actions: [
TextButton(
onPressed: () {
Navigator.pop(context);
},
child: const Text('Tutup'),
),
TextButton(
onPressed: () {
Navigator.pop(context);
_sharePdf(context, pdfFile);
},
child: const Text('Bagikan'),
),
TextButton(
onPressed: () {
Navigator.pop(context);
_openPdf(context, pdfFile);
},
child: const Text('Buka'),
),
],
),
);
} catch (e, stackTrace) {
// Close loading dialog if open
if (!context.mounted) return;
Navigator.pop(context);
// Show detailed error
showDialog(
context: context,
builder: (context) => AlertDialog(
title: const Text('Error'),
content: SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text('Gagal membuat PDF:'),
const SizedBox(height: 8),
Text(e.toString(), style: const TextStyle(fontWeight: FontWeight.bold)),
const SizedBox(height: 16),
const Text('Stack Trace:', style: TextStyle(fontWeight: FontWeight.bold)),
Text(stackTrace.toString(), style: const TextStyle(fontSize: 12)),
],
),
),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: const Text('OK'),
),
],
),
);
}
}
static Future<void> _openPdf(BuildContext context, File file) async {
try {
final pdfGenerator = HarvestPdfGenerator();
await pdfGenerator.openPdf(file);
} catch (e) {
// If opening fails, show a dialog with options
if (!context.mounted) return;
showDialog(
context: context,
builder: (context) => AlertDialog(
title: const Text('Gagal Membuka PDF'),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
const Text(
'Tidak dapat membuka file PDF secara langsung. '
'Silakan bagikan file untuk dibuka dengan aplikasi lain.'
),
const SizedBox(height: 8),
Text(
'Error: ${e.toString()}',
style: const TextStyle(fontSize: 12, color: Colors.red),
),
],
),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: const Text('Tutup'),
),
TextButton(
onPressed: () {
Navigator.pop(context);
_sharePdf(context, file);
},
child: const Text('Bagikan'),
),
],
),
);
}
}
static Future<void> _sharePdf(BuildContext context, File file) async {
try {
final pdfGenerator = HarvestPdfGenerator();
await pdfGenerator.sharePdf(file);
} catch (e) {
if (!context.mounted) return;
showDialog(
context: context,
builder: (context) => AlertDialog(
title: const Text('Error'),
content: Text('Gagal membagikan PDF: ${e.toString()}'),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: const Text('OK'),
),
],
),
);
}
}
}