added account balance in statement. Fixed bank logo in statement pdf.
removed unwanted log generations
This commit is contained in:
@@ -11,9 +11,6 @@ import 'transaction_details_screen.dart';
|
||||
import 'package:pdf/widgets.dart' as pw;
|
||||
import 'package:permission_handler/permission_handler.dart';
|
||||
import 'package:device_info_plus/device_info_plus.dart';
|
||||
import 'package:flutter/foundation.dart' show kIsWeb;
|
||||
import 'dart:html' as html; // Import for web-specific code
|
||||
import 'dart:typed_data';
|
||||
|
||||
class AccountStatementScreen extends StatefulWidget {
|
||||
final String accountNo;
|
||||
@@ -319,115 +316,114 @@ class _AccountStatementScreen extends State<AccountStatementScreen> {
|
||||
],
|
||||
),
|
||||
),
|
||||
floatingActionButton: FloatingActionButton(
|
||||
onPressed: () {
|
||||
_exportToPdf();
|
||||
},
|
||||
child: const Icon(Icons.download),
|
||||
),
|
||||
floatingActionButton: FloatingActionButton(
|
||||
onPressed: () {
|
||||
_exportToPdf();
|
||||
},
|
||||
child: const Icon(Icons.download),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _exportToPdf() async {
|
||||
// Step 1: Check if there are any transactions to export.
|
||||
if (_transactions.isEmpty) {
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('No transactions to export.'),
|
||||
),
|
||||
);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Step 2: Handle storage permissions for Android.
|
||||
/*if (Platform.isAndroid) {
|
||||
final androidInfo = await DeviceInfoPlugin().androidInfo;
|
||||
if (androidInfo.version.sdkInt < 33) { // Target Android 12 & below
|
||||
final status = await Permission.storage.status;
|
||||
if (status.isDenied) {
|
||||
final result = await Permission.storage.request();
|
||||
if (result.isDenied) {
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('Storage permission is required to save PDF'),
|
||||
),
|
||||
);
|
||||
}
|
||||
return;
|
||||
}
|
||||
// Step 1: Check if there are any transactions to export.
|
||||
if (_transactions.isEmpty) {
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('No transactions to export.'),
|
||||
),
|
||||
);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Step 3: Load assets for the PDF.
|
||||
// IMPORTANT: The original path 'assets/logos/logo.svg' is incorrect.
|
||||
// I've corrected it to 'assets/images/icon.svg' based on your project structure.
|
||||
String logoSvg = await rootBundle.loadString('assets/images/icon.svg');
|
||||
var rubik = await rootBundle.load("assets/fonts/Rubik-Regular.ttf");
|
||||
|
||||
try {
|
||||
var logo = await rootBundle.load('assets/images/logo.png');
|
||||
var rubik = await rootBundle.load("assets/fonts/Rubik-Regular.ttf");
|
||||
final pdf = pw.Document();
|
||||
pdf.addPage(
|
||||
pw.MultiPage(
|
||||
|
||||
pdf.addPage(pw.MultiPage(
|
||||
margin: const pw.EdgeInsets.all(20),
|
||||
build: (pw.Context context) {
|
||||
return [
|
||||
pw.Row(
|
||||
mainAxisAlignment: pw.MainAxisAlignment.start,
|
||||
crossAxisAlignment: pw.CrossAxisAlignment.center,
|
||||
children: [
|
||||
pw.SvgImage(svg: logoSvg, width: 50, height: 50),
|
||||
pw.SizedBox(width: 20),
|
||||
pw.Text(
|
||||
"Account Statement - KCCB",
|
||||
style: pw.TextStyle(
|
||||
fontSize: 24,
|
||||
fontWeight: pw.FontWeight.bold,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
mainAxisAlignment: pw.MainAxisAlignment.start,
|
||||
crossAxisAlignment: pw.CrossAxisAlignment.center,
|
||||
children: [
|
||||
pw.Image(pw.MemoryImage(logo.buffer.asUint8List()),
|
||||
width: 50, height: 50),
|
||||
pw.SizedBox(width: 20),
|
||||
pw.Text('Account Statement - KCCB',
|
||||
style: pw.TextStyle(
|
||||
fontSize: 24, fontWeight: pw.FontWeight.bold)),
|
||||
]),
|
||||
pw.SizedBox(height: 20),
|
||||
pw.Row(
|
||||
mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
pw.Text(
|
||||
'Account Number: ${widget.accountNo}',
|
||||
style: pw.TextStyle(font: pw.Font.ttf(rubik), fontSize: 15),
|
||||
),
|
||||
pw.Text(
|
||||
'Account Type: ${widget.accountType}',
|
||||
style: pw.TextStyle(font: pw.Font.ttf(rubik), fontSize: 15),
|
||||
),
|
||||
],
|
||||
),
|
||||
mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
pw.Text('Account Number: ${widget.accountNo}',
|
||||
style:
|
||||
pw.TextStyle(font: pw.Font.ttf(rubik), fontSize: 15)),
|
||||
pw.Text('Account Type: ${widget.accountType}',
|
||||
style: pw.TextStyle(
|
||||
fontSize: 15,
|
||||
font: pw.Font.ttf(rubik),
|
||||
)),
|
||||
]),
|
||||
pw.SizedBox(height: 20),
|
||||
// Step 4: Build the table from the _transactions list.
|
||||
pw.Table.fromTextArray(
|
||||
border: pw.TableBorder.all(),
|
||||
headerStyle: pw.TextStyle(fontWeight: pw.FontWeight.bold),
|
||||
headerDecoration: const pw.BoxDecoration(
|
||||
color: PdfColors.grey300,
|
||||
pw.Table(border: pw.TableBorder.all(), columnWidths: {
|
||||
0: const pw.FractionColumnWidth(0.2),
|
||||
1: const pw.FractionColumnWidth(0.45),
|
||||
2: const pw.FractionColumnWidth(0.15),
|
||||
3: const pw.FractionColumnWidth(0.20),
|
||||
}, children: [
|
||||
pw.TableRow(
|
||||
children: [
|
||||
pw.Padding(
|
||||
padding: const pw.EdgeInsets.all(4),
|
||||
child: pw.Text('Date')),
|
||||
pw.Padding(
|
||||
padding: const pw.EdgeInsets.all(4),
|
||||
child: pw.Text('Description', softWrap: true)),
|
||||
pw.Padding(
|
||||
padding: const pw.EdgeInsets.all(4),
|
||||
child: pw.Text('Amount')),
|
||||
pw.Padding(
|
||||
padding: const pw.EdgeInsets.all(4),
|
||||
child: pw.Text('Balance')),
|
||||
],
|
||||
),
|
||||
cellHeight: 30,
|
||||
cellAlignments: {
|
||||
0: pw.Alignment.centerLeft,
|
||||
1: pw.Alignment.centerLeft,
|
||||
2: pw.Alignment.centerRight,
|
||||
3: pw.Alignment.center,
|
||||
},
|
||||
headers: ['Date', 'Description', 'Amount', 'Type'],
|
||||
data: _transactions.map((tx) => [
|
||||
tx.date ?? 'N/A',
|
||||
tx.name ?? 'N/A',
|
||||
'₹${tx.amount}',
|
||||
tx.type ?? 'N/A',
|
||||
]).toList(),
|
||||
),
|
||||
..._transactions.map<pw.TableRow>((tx) {
|
||||
return pw.TableRow(children: [
|
||||
pw.Padding(
|
||||
padding: const pw.EdgeInsets.all(10),
|
||||
child: pw.Text(tx.date ?? '',
|
||||
style: pw.TextStyle(
|
||||
fontSize: 12,
|
||||
font: pw.Font.ttf(rubik),
|
||||
))),
|
||||
pw.Padding(
|
||||
padding: const pw.EdgeInsets.all(10),
|
||||
child: pw.Text(tx.name ?? '',
|
||||
style: pw.TextStyle(
|
||||
fontSize: 12, font: pw.Font.ttf(rubik)))),
|
||||
pw.Padding(
|
||||
padding: const pw.EdgeInsets.all(10),
|
||||
child: pw.Text("₹${tx.amount} ${tx.type}",
|
||||
style: pw.TextStyle(
|
||||
fontSize: 12,
|
||||
font: pw.Font.ttf(rubik),
|
||||
))),
|
||||
pw.Padding(
|
||||
padding: const pw.EdgeInsets.all(10),
|
||||
child: pw.Text("₹${tx.balance} ${tx.balanceType}",
|
||||
style: pw.TextStyle(
|
||||
fontSize: 12,
|
||||
font: pw.Font.ttf(rubik),
|
||||
))),
|
||||
]);
|
||||
}),
|
||||
])
|
||||
];
|
||||
},
|
||||
footer: (pw.Context context) {
|
||||
@@ -435,213 +431,64 @@ class _AccountStatementScreen extends State<AccountStatementScreen> {
|
||||
alignment: pw.Alignment.centerRight,
|
||||
margin: const pw.EdgeInsets.only(top: 10),
|
||||
child: pw.Text(
|
||||
'Kangra Central Co-Operative bank Pvt Ltd. ©. All rights reserved.',
|
||||
'Kangra Central Co-Operative Bank Pvt Ltd. ©. All rights reserved.',
|
||||
style: pw.TextStyle(
|
||||
font: pw.Font.ttf(rubik),
|
||||
fontSize: 8,
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}));
|
||||
|
||||
// Step 5: Save the PDF to the device.
|
||||
Directory? directory = await getDownloadsDirectory();
|
||||
if (directory == null) {
|
||||
throw Exception('Could not access downloads directory');
|
||||
//Logic For all platforms
|
||||
try {
|
||||
final Uint8List pdfBytes = await pdf.save();
|
||||
final String timestamp = DateTime.now().millisecondsSinceEpoch.toString();
|
||||
final String fileName = 'account_statement_$timestamp.pdf';
|
||||
|
||||
// For Android
|
||||
if (Platform.isAndroid) {
|
||||
final androidInfo = await DeviceInfoPlugin().androidInfo;
|
||||
if (androidInfo.version.sdkInt < 29) {
|
||||
final status = await Permission.storage.status;
|
||||
if (status.isDenied) {
|
||||
final result = await Permission.storage.request();
|
||||
if (result.isDenied) {
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('Storage permission is required to save PDF'),
|
||||
),
|
||||
);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
final directory = Directory('/storage/emulated/0/Download');
|
||||
final file = File('${directory.path}/$fileName');
|
||||
await file.writeAsBytes(pdfBytes);
|
||||
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('PDF saved to: ${file.path}'),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
// Add for IOS
|
||||
} catch (e) {
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('Error saving PDF: $e'),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
final String timestamp = DateTime.now().millisecondsSinceEpoch.toString();
|
||||
final file = File('${directory.path}/account_statement_$timestamp.pdf');
|
||||
|
||||
await file.writeAsBytes(await pdf.save());
|
||||
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('PDF saved to: ${file.path}'),
|
||||
duration: const Duration(seconds: 3),
|
||||
),
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('Error saving PDF: $e'),
|
||||
),
|
||||
);
|
||||
}
|
||||
}*/
|
||||
String logoSvg = await rootBundle.loadString('assets/images/kccb_logo.svg'); // Corrected asset path
|
||||
var rubik = await rootBundle.load("assets/fonts/Rubik-Regular.ttf");
|
||||
final pdf = pw.Document();
|
||||
|
||||
pdf.addPage(pw.MultiPage(
|
||||
margin: const pw.EdgeInsets.all(20),
|
||||
build: (pw.Context context) {
|
||||
return [
|
||||
pw.Row(
|
||||
mainAxisAlignment: pw.MainAxisAlignment.start,
|
||||
crossAxisAlignment: pw.CrossAxisAlignment.center,
|
||||
children: [
|
||||
pw.SvgImage(svg: logoSvg, width: 50, height: 50),
|
||||
pw.SizedBox(width: 20),
|
||||
pw.Text('Account Statement - KCCB',
|
||||
style: pw.TextStyle(
|
||||
fontSize: 24, fontWeight: pw.FontWeight.bold)),
|
||||
]),
|
||||
pw.SizedBox(height: 20),
|
||||
pw.Row(
|
||||
mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
pw.Text('Account Number: ${widget.accountNo}',
|
||||
style: pw.TextStyle(
|
||||
font: pw.Font.ttf(rubik), fontSize: 15)),
|
||||
pw.Text('Account Type: ${widget.accountType}',
|
||||
style: pw.TextStyle(
|
||||
fontSize: 15,
|
||||
font: pw.Font.ttf(rubik),
|
||||
)),
|
||||
]),
|
||||
pw.SizedBox(height: 20),
|
||||
pw.Table(border: pw.TableBorder.all(), columnWidths: {
|
||||
0: const pw.FractionColumnWidth(0.2),
|
||||
1: const pw.FractionColumnWidth(0.5),
|
||||
2: const pw.FractionColumnWidth(0.15),
|
||||
3: const pw.FractionColumnWidth(0.15),
|
||||
}, children: [
|
||||
pw.TableRow(
|
||||
children: [
|
||||
pw.Padding(
|
||||
padding: const pw.EdgeInsets.all(4),
|
||||
child: pw.Text('Date')),
|
||||
pw.Padding(
|
||||
padding: const pw.EdgeInsets.all(4),
|
||||
child: pw.Text('Description', softWrap: true)),
|
||||
pw.Padding(
|
||||
padding: const pw.EdgeInsets.all(4),
|
||||
child: pw.Text('Amount')),
|
||||
pw.Padding(
|
||||
padding: const pw.EdgeInsets.all(4),
|
||||
child: pw.Text('Balance')),
|
||||
],
|
||||
),
|
||||
..._transactions.map<pw.TableRow>((tx) {
|
||||
return pw.TableRow(children: [
|
||||
pw.Padding(
|
||||
padding: const pw.EdgeInsets.all(10),
|
||||
child: pw.Text(tx.date ?? '',
|
||||
style: pw.TextStyle(
|
||||
fontSize: 12,
|
||||
font: pw.Font.ttf(rubik),
|
||||
))),
|
||||
pw.Padding(
|
||||
padding: const pw.EdgeInsets.all(10),
|
||||
child: pw.Text(tx.name ?? '',
|
||||
style: pw.TextStyle(
|
||||
fontSize: 12, font: pw.Font.ttf(rubik)))),
|
||||
pw.Padding(
|
||||
padding: const pw.EdgeInsets.all(10),
|
||||
child: pw.Text("₹${tx.amount}",
|
||||
style: pw.TextStyle(
|
||||
fontSize: 12,
|
||||
font: pw.Font.ttf(rubik),
|
||||
))),
|
||||
]);
|
||||
}).toList(),
|
||||
])
|
||||
];
|
||||
},
|
||||
footer: (pw.Context context) {
|
||||
return pw.Container(
|
||||
alignment: pw.Alignment.centerRight,
|
||||
margin: const pw.EdgeInsets.only(top: 10),
|
||||
child: pw.Text(
|
||||
'Kangra Central Co-Operative Bank Pvt Ltd. ©. All rights reserved.',
|
||||
style: pw.TextStyle(
|
||||
font: pw.Font.ttf(rubik),
|
||||
fontSize: 8,
|
||||
),
|
||||
),
|
||||
);
|
||||
}));
|
||||
|
||||
//Logic For all platforms
|
||||
try {
|
||||
final Uint8List pdfBytes = await pdf.save();
|
||||
final String timestamp = DateTime.now().millisecondsSinceEpoch.toString();
|
||||
final String fileName = 'account_statement_$timestamp.pdf';
|
||||
|
||||
// For Web
|
||||
if (kIsWeb) {
|
||||
final blob = html.Blob([pdfBytes], 'application/pdf');
|
||||
final url = html.Url.createObjectUrlFromBlob(blob);
|
||||
html.Url.revokeObjectUrl(url);
|
||||
print('Generated PDF Blob URL for web: $url');
|
||||
|
||||
final anchor = html.document.createElement('a') as html.AnchorElement
|
||||
..href = url
|
||||
..style.display = 'none'
|
||||
..download = fileName;
|
||||
|
||||
html.document.body!.children.add(anchor);
|
||||
anchor.click();
|
||||
html.document.body!.children.remove(anchor);
|
||||
|
||||
html.Url.revokeObjectUrl(url);
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('PDF download started: $fileName'),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
// For Android
|
||||
else if (Platform.isAndroid) {
|
||||
final androidInfo = await DeviceInfoPlugin().androidInfo;
|
||||
if (androidInfo.version.sdkInt < 29) {
|
||||
final status = await Permission.storage.status;
|
||||
if (status.isDenied) {
|
||||
final result = await Permission.storage.request();
|
||||
if (result.isDenied) {
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('Storage permission is required to save PDF'),
|
||||
),
|
||||
);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
final directory = Directory('/storage/emulated/0/Download');
|
||||
final file = File('${directory.path}/$fileName');
|
||||
await file.writeAsBytes(pdfBytes);
|
||||
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('PDF saved to: ${file.path}'),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
// Add for IOS
|
||||
|
||||
} catch (e) {
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('Error saving PDF: $e'),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Widget buildDateBox(String label, DateTime? date) {
|
||||
return Container(
|
||||
|
Reference in New Issue
Block a user