From 893d095f7562aebedb9c86f33bbc99dee93c2acd Mon Sep 17 00:00:00 2001 From: Nicolas Gruber Date: Sun, 14 May 2023 15:36:29 +0200 Subject: [PATCH] implemented that a list of issues/news can be exported as pdf. --- lib/UserDefinedList.dart | 178 +++++++++++++++++++++++++++------------ lib/home.dart | 6 +- 2 files changed, 128 insertions(+), 56 deletions(-) diff --git a/lib/UserDefinedList.dart b/lib/UserDefinedList.dart index 6658078..c213309 100644 --- a/lib/UserDefinedList.dart +++ b/lib/UserDefinedList.dart @@ -132,7 +132,9 @@ class UserDefinedItem extends StatelessWidget { class GenericList extends StatefulWidget { final String collectionName; - const GenericList({Key? key, required this.collectionName}) : super(key: key); + final int selectedIndex; + + const GenericList({required this.collectionName, required this.selectedIndex}); @override _GenericListState createState() => _GenericListState(); @@ -140,7 +142,7 @@ class GenericList extends StatefulWidget { class _GenericListState extends State { final FirebaseFirestore firestore = FirebaseFirestore.instance; - Map selectedItems = {}; + Map> selectedItems = {}; @override void initState() { @@ -224,16 +226,14 @@ class _GenericListState extends State { datum: formattedDate, description: data['description'] ?? '', imageUrl: data['imageUrl'] ?? '', - isSelected: selectedItems.containsKey(documentId) - ? selectedItems[documentId]! - : false, + isSelected: selectedItems.containsKey(documentId), onLongPress: () { - // Aktualisieren Sie den ausgewählten Zustand des Elements setState(() { - selectedItems[documentId] = - !selectedItems.containsKey(documentId) - ? true - : !selectedItems[documentId]!; + if (selectedItems.containsKey(documentId)) { + selectedItems.remove(documentId); + } else { + selectedItems[documentId] = data; + } }); }, ), @@ -243,25 +243,42 @@ class _GenericListState extends State { } }, ), - if (selectedItems.containsValue(true)) - Align( - alignment: Alignment.bottomLeft, - child: Padding( - padding: const EdgeInsets.all(16.0), - child: FloatingActionButton( - onPressed: () { - // Führen Sie hier die gewünschte Aktion aus - }, - backgroundColor: Colors.red, - child: const Icon(Icons.picture_as_pdf), + if (selectedItems.isNotEmpty) + Stack( + children: [ + Align( + alignment: Alignment.topRight, + child: Padding( + padding: const EdgeInsets.all(16.0), + child: FloatingActionButton( + backgroundColor: Colors.blue, + onPressed: () { + setState(() { + selectedItems.clear(); + }); + }, + child: const Icon(Icons.refresh), + ), + ), ), - ), + Align( + alignment: Alignment.bottomLeft, + child: Padding( + padding: const EdgeInsets.all(16.0), + child: FloatingActionButton( + backgroundColor: Colors.red, + onPressed: () => _printSelectedItemsAsPdf(widget.selectedIndex), + child: const Icon(Icons.picture_as_pdf), + ), + ), + ), + ], ), ], ), ); } - /* + Future _loadNetworkImage(String url) async { final response = await http.get(Uri.parse(url)); @@ -274,43 +291,98 @@ class _GenericListState extends State { return imageProvider; } - Future _printPdf() async { - final pdf = pdfWidgets.Document(); - for (var docId in selectedItems.keys) { - final title = selectedItems[docId]!['title']; - final datum = selectedItems[docId]!['datum']; - final description = selectedItems[docId]!['description']; - final imageUrl = selectedItems[docId]!['imageUrl']; + Future _printSelectedItemsAsPdf(int selectedIndex) async { + final pdfWidgets.Document pdf = pdfWidgets.Document(); - final image = await _loadNetworkImage(imageUrl); + final List items = []; + for (final entry in selectedItems.entries) { + final String title = entry.value['title'] ?? ''; + final String description = (entry.value['description'] ?? '').length > 200 ? (entry.value['description'] ?? '').substring(0, 200) + '...' : (entry.value['description'] ?? ''); + final String imageUrl = entry.value['imageUrl'] ?? ''; + final DateTime date = entry.value['createdDate'].toDate(); + final String formattedDate = DateFormat('dd.MM.yyyy').format(date); + final pdfWidgets.ImageProvider imageProvider = await _loadNetworkImage(imageUrl); - pdf.addPage( - pdfWidgets.MultiPage( - build: (context) => [ - pdfWidgets.Center( - child: pdfWidgets.SizedBox( - width: double.infinity, - child: pdfWidgets.AspectRatio( - aspectRatio: 1, - child: pdfWidgets.Image(image), + items.add( + pdfWidgets.Container( + height: 150, // Begrenzt die Höhe des Containers + padding: const pdfWidgets.EdgeInsets.all(5.0), // Reduziert den Abstand + child: pdfWidgets.Row( + children: [ + pdfWidgets.Container( + width: 150, // Reduziert die Breite des Bildes + height: 150, // Reduziert die Höhe des Bildes + child: pdfWidgets.Image(imageProvider), + ), + pdfWidgets.Flexible( + child: pdfWidgets.Padding( + padding: const pdfWidgets.EdgeInsets.all(5.0), // Reduziert den Abstand + child: pdfWidgets.Column( + crossAxisAlignment: pdfWidgets.CrossAxisAlignment.start, + children: [ + pdfWidgets.Row( + mainAxisAlignment: pdfWidgets.MainAxisAlignment.spaceBetween, + children: [ + pdfWidgets.Flexible( + child: pdfWidgets.Text( + title, + style: pdfWidgets.TextStyle( + fontSize: 20, fontWeight: pdfWidgets.FontWeight.bold), + ), + ), + pdfWidgets.Text( + formattedDate, + style: const pdfWidgets.TextStyle( + fontSize: 15, + ), + ), + + ], + ), + pdfWidgets.Padding( + padding: const pdfWidgets.EdgeInsets.all(5.0), // Reduziert den Abstand + child: pdfWidgets.Text( + description, + style: const pdfWidgets.TextStyle( + fontSize: 15, + ), + ), + ), + ], + ), ), ), - ), - pdfWidgets.Text( - title, - style: pdfWidgets.TextStyle( - fontSize: 20, - fontWeight: pdfWidgets.FontWeight.bold - ) - ), - pdfWidgets.Text(datum, style: const pdfWidgets.TextStyle(fontSize: 14, )), - pdfWidgets.Paragraph(text: description, style: const pdfWidgets.TextStyle(fontSize: 12)), - ], + ], + ), ), ); } - await Printing.sharePdf(bytes: await pdf.save(), filename: 'selectedItems.pdf'); - }*/ + pdf.addPage( + pdfWidgets.MultiPage( + build: (pdfWidgets.Context context) => items, + ), + ); + + String filename; + switch(selectedIndex) { + case 0: + filename = 'Bürgerforum_items.pdf'; + break; + case 1: + filename = 'Neuigkeiten_items.pdf'; + break; + case 2: + filename = 'Mängel_items.pdf'; + break; + default: + filename = 'selected_items.pdf'; + } + + await Printing.sharePdf(bytes: await pdf.save(), filename: filename); + setState(() { + selectedItems.clear(); + }); + } } diff --git a/lib/home.dart b/lib/home.dart index 552fdcb..be36aa1 100644 --- a/lib/home.dart +++ b/lib/home.dart @@ -34,9 +34,9 @@ class _MyHomePageState extends State { @override Widget build(BuildContext context) { final List widgetOptions = [ - const GenericList(collectionName: 'CitizensForum'), - const GenericList(collectionName: 'News'), - const GenericList(collectionName: 'Issues'), + const GenericList(collectionName: 'CitizensForum', selectedIndex: 0), + const GenericList(collectionName: 'News', selectedIndex: 1), + const GenericList(collectionName: 'Issues', selectedIndex: 2), Column(mainAxisAlignment: MainAxisAlignment.center, children: [ ElevatedButton( child: Text(login ? 'Logout' : 'Login'),