implemented that a list of issues/news can be exported as pdf.

This commit is contained in:
2023-05-14 15:36:29 +02:00
parent 5687670e36
commit 893d095f75
2 changed files with 128 additions and 56 deletions
+113 -41
View File
@@ -132,7 +132,9 @@ class UserDefinedItem extends StatelessWidget {
class GenericList extends StatefulWidget { class GenericList extends StatefulWidget {
final String collectionName; 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 @override
_GenericListState createState() => _GenericListState(); _GenericListState createState() => _GenericListState();
@@ -140,7 +142,7 @@ class GenericList extends StatefulWidget {
class _GenericListState extends State<GenericList> { class _GenericListState extends State<GenericList> {
final FirebaseFirestore firestore = FirebaseFirestore.instance; final FirebaseFirestore firestore = FirebaseFirestore.instance;
Map<String, bool> selectedItems = {}; Map<String, Map<String, dynamic>> selectedItems = {};
@override @override
void initState() { void initState() {
@@ -224,16 +226,14 @@ class _GenericListState extends State<GenericList> {
datum: formattedDate, datum: formattedDate,
description: data['description'] ?? '', description: data['description'] ?? '',
imageUrl: data['imageUrl'] ?? '', imageUrl: data['imageUrl'] ?? '',
isSelected: selectedItems.containsKey(documentId) isSelected: selectedItems.containsKey(documentId),
? selectedItems[documentId]!
: false,
onLongPress: () { onLongPress: () {
// Aktualisieren Sie den ausgewählten Zustand des Elements
setState(() { setState(() {
selectedItems[documentId] = if (selectedItems.containsKey(documentId)) {
!selectedItems.containsKey(documentId) selectedItems.remove(documentId);
? true } else {
: !selectedItems[documentId]!; selectedItems[documentId] = data;
}
}); });
}, },
), ),
@@ -243,25 +243,42 @@ class _GenericListState extends State<GenericList> {
} }
}, },
), ),
if (selectedItems.containsValue(true)) 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( Align(
alignment: Alignment.bottomLeft, alignment: Alignment.bottomLeft,
child: Padding( child: Padding(
padding: const EdgeInsets.all(16.0), padding: const EdgeInsets.all(16.0),
child: FloatingActionButton( child: FloatingActionButton(
onPressed: () {
// Führen Sie hier die gewünschte Aktion aus
},
backgroundColor: Colors.red, backgroundColor: Colors.red,
onPressed: () => _printSelectedItemsAsPdf(widget.selectedIndex),
child: const Icon(Icons.picture_as_pdf), child: const Icon(Icons.picture_as_pdf),
), ),
), ),
), ),
], ],
), ),
],
),
); );
} }
/*
Future<pdfWidgets.ImageProvider> _loadNetworkImage(String url) async { Future<pdfWidgets.ImageProvider> _loadNetworkImage(String url) async {
final response = await http.get(Uri.parse(url)); final response = await http.get(Uri.parse(url));
@@ -274,43 +291,98 @@ class _GenericListState extends State<GenericList> {
return imageProvider; return imageProvider;
} }
Future<void> _printPdf() async {
final pdf = pdfWidgets.Document();
for (var docId in selectedItems.keys) { Future<void> _printSelectedItemsAsPdf(int selectedIndex) async {
final title = selectedItems[docId]!['title']; final pdfWidgets.Document pdf = pdfWidgets.Document();
final datum = selectedItems[docId]!['datum'];
final description = selectedItems[docId]!['description'];
final imageUrl = selectedItems[docId]!['imageUrl'];
final image = await _loadNetworkImage(imageUrl); final List<pdfWidgets.Widget> 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( items.add(
pdfWidgets.MultiPage( pdfWidgets.Container(
build: (context) => [ height: 150, // Begrenzt die Höhe des Containers
pdfWidgets.Center( padding: const pdfWidgets.EdgeInsets.all(5.0), // Reduziert den Abstand
child: pdfWidgets.SizedBox( child: pdfWidgets.Row(
width: double.infinity, children: [
child: pdfWidgets.AspectRatio( pdfWidgets.Container(
aspectRatio: 1, width: 150, // Reduziert die Breite des Bildes
child: pdfWidgets.Image(image), 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( pdfWidgets.Text(
title, formattedDate,
style: pdfWidgets.TextStyle( style: const pdfWidgets.TextStyle(
fontSize: 20, fontSize: 15,
fontWeight: pdfWidgets.FontWeight.bold
)
), ),
pdfWidgets.Text(datum, style: const pdfWidgets.TextStyle(fontSize: 14, )), ),
pdfWidgets.Paragraph(text: description, style: const pdfWidgets.TextStyle(fontSize: 12)),
], ],
), ),
pdfWidgets.Padding(
padding: const pdfWidgets.EdgeInsets.all(5.0), // Reduziert den Abstand
child: pdfWidgets.Text(
description,
style: const pdfWidgets.TextStyle(
fontSize: 15,
),
),
),
],
),
),
),
],
),
),
); );
} }
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();
});
}
} }
+3 -3
View File
@@ -34,9 +34,9 @@ class _MyHomePageState extends State<MyHomePage> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final List<Widget> widgetOptions = <Widget>[ final List<Widget> widgetOptions = <Widget>[
const GenericList(collectionName: 'CitizensForum'), const GenericList(collectionName: 'CitizensForum', selectedIndex: 0),
const GenericList(collectionName: 'News'), const GenericList(collectionName: 'News', selectedIndex: 1),
const GenericList(collectionName: 'Issues'), const GenericList(collectionName: 'Issues', selectedIndex: 2),
Column(mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Column(mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[
ElevatedButton( ElevatedButton(
child: Text(login ? 'Logout' : 'Login'), child: Text(login ? 'Logout' : 'Login'),