Screen: 6
Receipt Screen.dart
Screen Description:
Code:
import ‘package:flutter/material.dart’;
import ‘package:footer/footer.dart’;
import ‘package:footer/footer_view.dart’;
import ‘package:path_provider/path_provider.dart’;
import ‘package:pdf/pdf.dart’;
import ‘package:pdf/widgets.dart’ as pw;
import ‘package:tis_root01/common/CommonFooter.dart’;
import ‘dart:io’;
import ‘common/CommonHeader.dart’;
class ReceiptScreen extends StatelessWidget {
final ReceiptData data;
ReceiptScreen({required this.data});
Future<void> _downloadPDF() async {
final pdf = pw.Document();
pdf.addPage(
pw.Page(
build: (context) {
return pw.Column(
children: [
pw.Container(
padding: pw.EdgeInsets.all(10),
decoration: pw.BoxDecoration(
border: pw.Border(
top: pw.BorderSide(
color: PdfColors.black,
width: 1,
),
left: pw.BorderSide(
color: PdfColors.black,
width: 1,
),
right: pw.BorderSide(
color: PdfColors.black,
width: 1,
),
bottom: pw.BorderSide(
color: PdfColors.black,
width: 1,
),
),
),
child: pw.Row(
mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
children: [
pw.Column(
crossAxisAlignment: pw.CrossAxisAlignment.start,
children: [
pw.Text(‘Company Name’,
style: pw.TextStyle(fontSize: 16)),
pw.Text(‘Company Address’,
style: pw.TextStyle(fontSize: 12)),
],
),
],
),
),
pw.SizedBox(height: 20),
pw.Text(
‘Receipt’,
style:
pw.TextStyle(fontSize: 18, fontWeight: pw.FontWeight.bold),
),
pw.SizedBox(height: 10),
pw.Text(‘Name: ${data.name}’),
pw.Text(‘Date: ${data.date}’),
pw.Text(‘Phone Number: ${data.phoneNo}’),
pw.Text(‘Address: ${data.address}’),
pw.Text(‘Amount: \$${data.amount.toStringAsFixed(2)}’),
pw.Text(‘Membership Type: ${data.membershipType}’),
pw.SizedBox(height: 20),
pw.Text(‘Received with thanks the ‘,
style: pw.TextStyle(fontSize: 16)),
pw.Text(
‘Total Amount of : \$${data.amount.toStringAsFixed(2)}’,
style:
pw.TextStyle(fontSize: 16, fontWeight: pw.FontWeight.bold),
),
],
);
},
),
);
// Get the device’s internal storage directory.
final internalStorageDir = await getApplicationDocumentsDirectory();
print(” device:$internalStorageDir”);
// Define the file path for the PDF file in the internal storage.
final filePath = ‘${internalStorageDir.path}/receipt.pdf’;
print(“Filepath:$filePath”);
// Create a File object to represent the PDF file.
final pdfFile = File(filePath);
// Write the PDF content to the file.
await pdfFile.writeAsBytes(await pdf.save());
// You can print a message or show a notification that the PDF has been saved.
print(“PDF saved to: $filePath”);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: PreferredSize(
preferredSize: const Size.fromHeight(80.0),
child: Container(
decoration: const BoxDecoration(
gradient: LinearGradient(
colors: <Color>[Colors.blue, Color.fromRGBO(233, 30, 99, 1)],
),
),
child: const CommonHeader(),
),
),
body:FooterView(
children: [
Center(
child: Container(
padding: EdgeInsets.all(20),
decoration: BoxDecoration(
border: Border.all(
color: Colors.black,
width: 1.0,
),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(‘Yadavar Peravai’, style: TextStyle(fontSize:20)),
Text(‘\nAddress:27/11, Pycrafts Road, 2-nd Street, \nRoyapettah, Chennai- 600 014’, style: TextStyle(fontSize:10)),
Text(‘Contact No : 044 28613077 ‘,style: TextStyle(fontSize:10)),
Text(‘Email : yadavarperavai@gmail.com ‘,style: TextStyle(fontSize:10)),
],
),
Image.asset(‘assets/yp_logo.png’, width: 100, height: 100),
],
),
SizedBox(height: 20),
Center(
child: Text(
‘RECEIPT’,
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
),
),
SizedBox(height: 30),
Text(‘Name: ${data.name}’),
Text(‘Date: ${data.date}’),
Text(‘Phone Number: ${data.phoneNo}’),
Text(‘Address: ${data.address}’),
//Text(‘Amount: \Rs.${data.amount.toStringAsFixed(2)}’),
Text(‘Membership Type: ${data.membershipType}’),
SizedBox(height: 20),
Center(
child: Text(‘Received with thanks the’,
style: TextStyle(fontSize: 16)),
),
SizedBox(height: 20),
Center(
child: Text(
‘Total Amount of \Rs.${data.amount.toStringAsFixed(2)}’,
style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
),
),
Center(
child: ElevatedButton(
onPressed: () {
_downloadPDF(); // Call the function without passing any arguments
},
child: Text(“DOWNLOAD RECEIPT”),
),
),
],
),
),
),
],
footer: new Footer(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
‘TISROOT,’,
style: TextStyle(
color: Colors.blue,
fontWeight: FontWeight.bold,
fontSize:5,
),
),
SizedBox(height:1), // Add some spacing
Text(
‘a cloud ERP “MAKEININDIA” software of Trust Infosys Incorporation,’,
style: TextStyle(
fontSize: 10,
),
),
Text(
‘Govt. of India accredited STARTUPINDIA venture.’,
style: TextStyle(
fontSize: 10,
),
),
],
),
//default flex is 2
),
flex:1
)
// bottomNavigationBar: CommonFooter(),
);
}
}
class ReceiptData {
final String name;
// DateTime date = DateTime.now();
final String date;
final String phoneNo;
//TextEditingController phoneNo = TextEditingController();
final String address;
final double amount;
final String membershipType;
ReceiptData({
required this.name,
required this.date,
required this.phoneNo,
required this.address,
required this.amount,
required this.membershipType,
});
}
Code description:
`ReceiptScreen` Class
The `ReceiptScreen` class is a Flutter widget that represents a screen for displaying and generating receipts. It uses the `pdf` package to create PDF receipts and allows the user to download them. The screen consists of a header, receipt information, and a footer.
- Constructor
“`dart
ReceiptScreen({required this.data});
“`
– The `ReceiptScreen` constructor takes a required parameter `data`, which is an instance of the `ReceiptData` class. This data is used to populate the receipt information.
- `_downloadPDF` Method
“`dart
Future<void> _downloadPDF() async {
// … (PDF generation and saving logic)
}
“`
- The `_downloadPDF` method is an asynchronous function that generates a PDF receipt and saves it to the device’s internal storage.
`build` Method
“`dart
@override
Widget build(BuildContext context) {
// … (Widget tree)
}
- The `build` method constructs the UI for the `ReceiptScreen`.
App Bar
“`dart
appBar: PreferredSize(
preferredSize: const Size.fromHeight(80.0),
child: Container(
decoration: const BoxDecoration(
gradient: LinearGradient(
colors: <Color>[Colors.blue, Color.fromRGBO(233, 30, 99, 1)],
),
),
child: const CommonHeader(),
),
),
- The `AppBar` at the top of the screen is customized with a gradient background and contains a common header provided by the `CommonHeader` widget.
Body
“`dart
body: FooterView(
// … (Widget tree for receipt information)
footer: Footer(
// … (Widget tree for the footer)
),
),
The body of the screen is wrapped in a `FooterView`, allowing you to scroll the content if it exceeds the screen size. It consists of receipt information and a footer.
Receipt Information
The receipt information is displayed within a container and includes details like the company name, address, recipient name, date, phone number, address, amount, and membership type.There’s also a button labeled “DOWNLOAD RECEIPT” that triggers the `_downloadPDF` method to generate and save the PDF receipt.
Footer
“`dart
footer: Footer(
// … (Footer content)
flex: 1,
),
`
The `Footer` widget represents the screen’s footer. It contains information about “TISROOT” and its description. It’s aligned at the bottom of the screen.
‘ReceiptData` Class
“`dart
class ReceiptData {
// … (Properties for receipt data)
}
- The `ReceiptData` class is a data model that represents the necessary data for generating a receipt. It includes properties for recipient name, date, phone number, address, amount, and membership type.
Overall, the `ReceiptScreen` class provides a user-friendly interface for generating and downloading PDF receipts. It utilizes the `pdf` package to create PDF documents dynamically based on the provided receipt data. The screen structure includes a header, receipt information, and a footer for a complete user experience
Audit :