implemented item selection + button to print a list of issues/news etc(Button has no functionality yet).
This commit is contained in:
+190
-75
@@ -3,14 +3,19 @@ import 'package:cloud_firestore/cloud_firestore.dart';
|
|||||||
import 'package:firebase_storage/firebase_storage.dart';
|
import 'package:firebase_storage/firebase_storage.dart';
|
||||||
import 'package:flutter/foundation.dart';
|
import 'package:flutter/foundation.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:printing/printing.dart';
|
||||||
import 'DetailPage.dart';
|
import 'DetailPage.dart';
|
||||||
import 'package:intl/intl.dart';
|
import 'package:intl/intl.dart';
|
||||||
|
import 'package:pdf/widgets.dart' as pdfWidgets;
|
||||||
|
import 'package:http/http.dart' as http;
|
||||||
|
|
||||||
class UserDefinedItem extends StatelessWidget {
|
class UserDefinedItem extends StatelessWidget {
|
||||||
final String title;
|
final String title;
|
||||||
final String datum;
|
final String datum;
|
||||||
final String description;
|
final String description;
|
||||||
final String imageUrl;
|
final String imageUrl;
|
||||||
|
final Function onLongPress;
|
||||||
|
final bool isSelected;
|
||||||
|
|
||||||
const UserDefinedItem({
|
const UserDefinedItem({
|
||||||
Key? key,
|
Key? key,
|
||||||
@@ -18,43 +23,62 @@ class UserDefinedItem extends StatelessWidget {
|
|||||||
required this.description,
|
required this.description,
|
||||||
required this.datum,
|
required this.datum,
|
||||||
required this.imageUrl,
|
required this.imageUrl,
|
||||||
|
required this.onLongPress,
|
||||||
|
required this.isSelected,
|
||||||
}) : super(key: key);
|
}) : super(key: key);
|
||||||
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Card(
|
return InkWell(
|
||||||
child: InkWell(
|
onTap: () {
|
||||||
onTap: () {
|
// Open a new screen when the user taps on the list item
|
||||||
// Open a new screen when the user taps on the list item
|
Navigator.push(
|
||||||
Navigator.push(
|
context,
|
||||||
context,
|
MaterialPageRoute(
|
||||||
MaterialPageRoute(
|
builder: (context) => DetailPage(
|
||||||
builder: (context) => DetailPage(
|
title: title,
|
||||||
title: title,
|
datum: datum,
|
||||||
datum: datum,
|
description: description,
|
||||||
description: description,
|
imageUrl: imageUrl,
|
||||||
imageUrl: imageUrl,
|
|
||||||
),
|
|
||||||
),
|
),
|
||||||
);
|
),
|
||||||
},
|
);
|
||||||
|
},
|
||||||
|
onLongPress: onLongPress as void Function()?,
|
||||||
|
child: Card(
|
||||||
child: Row(
|
child: Row(
|
||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
children: [
|
children: [
|
||||||
SizedBox(
|
Stack(
|
||||||
width: 120,
|
children: [
|
||||||
height: 120,
|
SizedBox(
|
||||||
child: CachedNetworkImage(
|
width: 120,
|
||||||
imageUrl: imageUrl,
|
height: 120,
|
||||||
placeholder: (context, url) => const Center(
|
child: CachedNetworkImage(
|
||||||
child: SizedBox(
|
imageUrl: imageUrl,
|
||||||
width: 50,
|
placeholder: (context, url) => const Center(
|
||||||
height: 50,
|
child: SizedBox(
|
||||||
child: CircularProgressIndicator(),
|
width: 50,
|
||||||
|
height: 50,
|
||||||
|
child: CircularProgressIndicator(),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
errorWidget: (context, url, error) => const Icon(Icons.error),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
errorWidget: (context, url, error) => const Icon(Icons.error),
|
if (isSelected) // Wenn das Element ausgewählt ist, zeigen Sie die Checkbox an
|
||||||
),
|
Positioned(
|
||||||
|
top: 0,
|
||||||
|
left: 0,
|
||||||
|
child: Checkbox(
|
||||||
|
value: isSelected,
|
||||||
|
onChanged: (bool? value) {
|
||||||
|
onLongPress(); // Wenn die Checkbox angeklickt wird, rufen Sie die onLongPress-Funktion auf
|
||||||
|
},
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
),
|
),
|
||||||
Flexible(
|
Flexible(
|
||||||
flex: 1,
|
flex: 1,
|
||||||
@@ -104,6 +128,8 @@ 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);
|
const GenericList({Key? key, required this.collectionName}) : super(key: key);
|
||||||
@@ -114,6 +140,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 = {};
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
@@ -122,7 +149,7 @@ class _GenericListState extends State<GenericList> {
|
|||||||
firestore.enablePersistence();
|
firestore.enablePersistence();
|
||||||
// Enable network
|
// Enable network
|
||||||
firestore.settings =
|
firestore.settings =
|
||||||
const Settings(cacheSizeBytes: Settings.CACHE_SIZE_UNLIMITED);
|
const Settings(cacheSizeBytes: Settings.CACHE_SIZE_UNLIMITED);
|
||||||
}
|
}
|
||||||
|
|
||||||
Stream<QuerySnapshot> getDocumentStream() {
|
Stream<QuerySnapshot> getDocumentStream() {
|
||||||
@@ -139,7 +166,9 @@ class _GenericListState extends State<GenericList> {
|
|||||||
await FirebaseStorage.instance.refFromURL(imageUrl).delete();
|
await FirebaseStorage.instance.refFromURL(imageUrl).delete();
|
||||||
}
|
}
|
||||||
// Löschen Sie das Dokument von der Firestore-Kollektion
|
// Löschen Sie das Dokument von der Firestore-Kollektion
|
||||||
await firestore.collection(widget.collectionName).doc(documentId).delete();
|
await firestore.collection(widget.collectionName)
|
||||||
|
.doc(documentId)
|
||||||
|
.delete();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (kDebugMode) {
|
if (kDebugMode) {
|
||||||
print('Error deleting document: $e');
|
print('Error deleting document: $e');
|
||||||
@@ -149,53 +178,139 @@ class _GenericListState extends State<GenericList> {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return StreamBuilder<QuerySnapshot>(
|
return Scaffold(
|
||||||
stream: getDocumentStream(),
|
body: Stack(
|
||||||
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
|
children: [
|
||||||
if (snapshot.hasError) {
|
StreamBuilder<QuerySnapshot>(
|
||||||
return Text('Error: ${snapshot.error}');
|
stream: getDocumentStream(),
|
||||||
}
|
builder: (BuildContext context,
|
||||||
switch (snapshot.connectionState) {
|
AsyncSnapshot<QuerySnapshot> snapshot) {
|
||||||
case ConnectionState.waiting:
|
if (snapshot.hasError) {
|
||||||
return const Text('Loading...');
|
return Text('Error: ${snapshot.error}');
|
||||||
default:
|
}
|
||||||
final List<QueryDocumentSnapshot> documents = snapshot.data!.docs;
|
switch (snapshot.connectionState) {
|
||||||
return ListView.builder(
|
case ConnectionState.waiting:
|
||||||
itemCount: documents.length,
|
return const Text('Loading...');
|
||||||
itemBuilder: (BuildContext context, int index) {
|
default:
|
||||||
final Map<String, dynamic> data =
|
final List<QueryDocumentSnapshot> documents = snapshot.data!
|
||||||
documents[index].data()! as Map<String, dynamic>;
|
.docs;
|
||||||
final String documentId = documents[index].id;
|
return ListView.builder(
|
||||||
final DateTime createdDate = data['createdDate'].toDate();
|
itemCount: documents.length,
|
||||||
final formattedDate =
|
itemBuilder: (BuildContext context, int index) {
|
||||||
DateFormat('dd.MM.yyyy').format(createdDate);
|
final Map<String, dynamic> data =
|
||||||
return Dismissible(
|
documents[index].data()! as Map<String, dynamic>;
|
||||||
key: UniqueKey(),
|
final String documentId = documents[index].id;
|
||||||
direction: DismissDirection.endToStart,
|
final DateTime createdDate = data['createdDate'].toDate();
|
||||||
onDismissed: (direction) {
|
final formattedDate =
|
||||||
_deleteDocument(documentId, data['imageUrl']);
|
DateFormat('dd.MM.yyyy').format(createdDate);
|
||||||
|
return Dismissible(
|
||||||
|
key: UniqueKey(),
|
||||||
|
direction: DismissDirection.endToStart,
|
||||||
|
onDismissed: (direction) {
|
||||||
|
_deleteDocument(documentId, data['imageUrl']);
|
||||||
|
},
|
||||||
|
background: Container(
|
||||||
|
color: Colors.red,
|
||||||
|
child: const Align(
|
||||||
|
alignment: Alignment.centerRight,
|
||||||
|
child: Padding(
|
||||||
|
padding: EdgeInsets.only(right: 16),
|
||||||
|
child: Icon(Icons.delete, color: Colors.white),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
child: UserDefinedItem(
|
||||||
|
title: data['title'] ?? '',
|
||||||
|
datum: formattedDate,
|
||||||
|
description: data['description'] ?? '',
|
||||||
|
imageUrl: data['imageUrl'] ?? '',
|
||||||
|
isSelected: selectedItems.containsKey(documentId)
|
||||||
|
? selectedItems[documentId]!
|
||||||
|
: false,
|
||||||
|
onLongPress: () {
|
||||||
|
// Aktualisieren Sie den ausgewählten Zustand des Elements
|
||||||
|
setState(() {
|
||||||
|
selectedItems[documentId] =
|
||||||
|
!selectedItems.containsKey(documentId)
|
||||||
|
? true
|
||||||
|
: !selectedItems[documentId]!;
|
||||||
|
});
|
||||||
|
},
|
||||||
|
),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
),
|
||||||
|
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
|
||||||
},
|
},
|
||||||
background: Container(
|
backgroundColor: Colors.red,
|
||||||
color: Colors.red,
|
child: const Icon(Icons.picture_as_pdf),
|
||||||
child: const Align(
|
),
|
||||||
alignment: Alignment.centerRight,
|
),
|
||||||
child: Padding(
|
),
|
||||||
padding: EdgeInsets.only(right: 16),
|
],
|
||||||
child: Icon(Icons.delete, color: Colors.white),
|
),
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
child: UserDefinedItem(
|
|
||||||
title: data['title'] ?? '',
|
|
||||||
datum: formattedDate,
|
|
||||||
description: data['description'] ?? '',
|
|
||||||
imageUrl: data['imageUrl'] ?? '',
|
|
||||||
),
|
|
||||||
);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
|
Future<pdfWidgets.ImageProvider> _loadNetworkImage(String url) async {
|
||||||
|
final response = await http.get(Uri.parse(url));
|
||||||
|
|
||||||
|
if (response.statusCode != 200) {
|
||||||
|
throw Exception('Failed to load network image.');
|
||||||
|
}
|
||||||
|
|
||||||
|
final Uint8List bytes = response.bodyBytes;
|
||||||
|
final imageProvider = pdfWidgets.MemoryImage(bytes);
|
||||||
|
|
||||||
|
return imageProvider;
|
||||||
|
}
|
||||||
|
Future<void> _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'];
|
||||||
|
|
||||||
|
final image = 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),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
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');
|
||||||
|
}*/
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ 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>[
|
||||||
|
|||||||
Reference in New Issue
Block a user