add pdf print from all stops in actual year
This commit is contained in:
103
lib/pages/print/print_controller.dart
Normal file
103
lib/pages/print/print_controller.dart
Normal file
@@ -0,0 +1,103 @@
|
||||
import 'package:get/get.dart';
|
||||
import 'package:pdf/pdf.dart';
|
||||
import 'package:pdf/widgets.dart' as pw;
|
||||
import 'package:printing/printing.dart';
|
||||
|
||||
import '../../data/models/tank_model.dart';
|
||||
import '../tanklist/tanklist_view.dart';
|
||||
|
||||
class PrintController extends GetxController {
|
||||
final argunments = Get.arguments;
|
||||
final List<dynamic> tankList = Get.arguments['tankList'] ?? [];
|
||||
final String year = Get.arguments['year'] ?? 'NoYear';
|
||||
final String summeLiter = Get.arguments['summeLiter'] ?? '0.0';
|
||||
final String summePreis = Get.arguments['summePreis'] ?? '0.0';
|
||||
|
||||
Future<void> printPdfReport() async {
|
||||
final doc = pw.Document();
|
||||
final font = await PdfGoogleFonts.robotoRegular();
|
||||
final List<AppWriteTankModel> tankungen =
|
||||
tankList.map((e) => AppWriteTankModel.fromMap(e)).toList();
|
||||
|
||||
doc.addPage(
|
||||
pw.Page(
|
||||
pageFormat: PdfPageFormat.a4,
|
||||
build: (pw.Context context) {
|
||||
return pw.Column(
|
||||
crossAxisAlignment: pw.CrossAxisAlignment.start,
|
||||
children: [
|
||||
// PDF-Design hier
|
||||
pw.Text('Tankbericht $year',
|
||||
style: pw.TextStyle(
|
||||
fontSize: 24,
|
||||
fontWeight: pw.FontWeight.bold,
|
||||
color: PdfColors.blue900,
|
||||
font: font)),
|
||||
pw.SizedBox(height: 10),
|
||||
pw.Text('Alle Tankungen im Überblick',
|
||||
style: pw.TextStyle(
|
||||
fontSize: 16, color: PdfColors.grey700, font: font)),
|
||||
pw.Divider(color: PdfColors.grey400),
|
||||
pw.SizedBox(height: 20),
|
||||
|
||||
pw.TableHelper.fromTextArray(
|
||||
headers: [
|
||||
'Datum',
|
||||
'Ort',
|
||||
'Menge (L)',
|
||||
'Preis/L (€)',
|
||||
'Summe (€)'
|
||||
],
|
||||
cellAlignment: pw.Alignment.centerLeft,
|
||||
border: pw.TableBorder.all(color: PdfColors.grey200),
|
||||
headerStyle:
|
||||
pw.TextStyle(fontWeight: pw.FontWeight.bold, font: font),
|
||||
data: tankungen.map((item) {
|
||||
var modDate = item.date.substring(5);
|
||||
return [
|
||||
modDate,
|
||||
item.location,
|
||||
item.liters,
|
||||
item.pricePerLiter,
|
||||
item.szSummePreis ?? '0.0',
|
||||
];
|
||||
}).toList(),
|
||||
),
|
||||
|
||||
pw.Spacer(),
|
||||
pw.Divider(color: PdfColors.grey400),
|
||||
pw.Container(
|
||||
alignment: pw.Alignment.centerRight,
|
||||
child: pw.Column(
|
||||
crossAxisAlignment: pw.CrossAxisAlignment.end,
|
||||
children: [
|
||||
pw.Text('Jahres-Gesamtmenge: $summeLiter L',
|
||||
style: pw.TextStyle(
|
||||
fontWeight: pw.FontWeight.bold,
|
||||
fontSize: 16,
|
||||
font: font)),
|
||||
pw.Text('Jahres-Gesamtsumme: $summePreis €',
|
||||
style: pw.TextStyle(
|
||||
fontWeight: pw.FontWeight.bold,
|
||||
fontSize: 16,
|
||||
color: PdfColors.blue700,
|
||||
font: font)),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
// Druckansicht öffnen
|
||||
await Printing.layoutPdf(
|
||||
onLayout: (PdfPageFormat format) async => doc.save(),
|
||||
);
|
||||
}
|
||||
|
||||
void goToTankListPage() async {
|
||||
await Get.offAndToNamed(TanklistPage.namedRoute);
|
||||
}
|
||||
}
|
||||
40
lib/pages/print/print_view.dart
Normal file
40
lib/pages/print/print_view.dart
Normal file
@@ -0,0 +1,40 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:get/get.dart';
|
||||
import './print_controller.dart';
|
||||
|
||||
class PrintPage extends GetView<PrintController> {
|
||||
static const namedRoute = '/print-pdf-page';
|
||||
const PrintPage({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
var printCtrl = controller;
|
||||
return PopScope(
|
||||
canPop: false,
|
||||
child: SafeArea(
|
||||
child: Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('PDF-Bericht'),
|
||||
centerTitle: true,
|
||||
actions: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.list, color: Colors.white),
|
||||
onPressed: () {
|
||||
// Handle logout logic here
|
||||
printCtrl.goToTankListPage();
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
body: Center(
|
||||
child: ElevatedButton(
|
||||
// Ruft die Controller-Methode auf
|
||||
onPressed: () => printCtrl.printPdfReport(),
|
||||
child: const Text('PDF erstellen & drucken'),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user