call clear funktion everywhere where it is useful, moved add button from home to user defined list (helpful for using the clearSelectedItems function)

This commit is contained in:
2023-05-15 11:24:20 +02:00
parent 3a50d7f1bc
commit 7062aef03b
2 changed files with 45 additions and 39 deletions
+45 -26
View File
@@ -4,6 +4,7 @@ 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 'package:printing/printing.dart';
import 'AddPage.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:pdf/widgets.dart' as pdfWidgets;
@@ -17,6 +18,7 @@ class UserDefinedItem extends StatelessWidget {
final String imageUrl; final String imageUrl;
final Function onLongPress; final Function onLongPress;
final bool isSelected; final bool isSelected;
final VoidCallback onTap;
const UserDefinedItem({ const UserDefinedItem({
Key? key, Key? key,
@@ -26,26 +28,14 @@ class UserDefinedItem extends StatelessWidget {
required this.imageUrl, required this.imageUrl,
required this.onLongPress, required this.onLongPress,
required this.isSelected, required this.isSelected,
required this.onTap,
}) : super(key: key); }) : super(key: key);
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return InkWell( return InkWell(
onTap: () { onTap:onTap,
// Open a new screen when the user taps on the list item
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => DetailPage(
title: title,
datum: datum,
description: description,
imageUrl: imageUrl,
),
),
);
},
onLongPress: onLongPress as void Function()?, onLongPress: onLongPress as void Function()?,
child: Card( child: Card(
child: Row( child: Row(
@@ -161,6 +151,11 @@ class _GenericListState extends State<GenericList> {
selectedItems.clear(); selectedItems.clear();
} }
} }
void clearSelectedItems() {
setState(() {
selectedItems.clear();
});
}
Stream<QuerySnapshot> getDocumentStream() { Stream<QuerySnapshot> getDocumentStream() {
return firestore return firestore
@@ -235,6 +230,21 @@ class _GenericListState extends State<GenericList> {
description: data['description'] ?? '', description: data['description'] ?? '',
imageUrl: data['imageUrl'] ?? '', imageUrl: data['imageUrl'] ?? '',
isSelected: selectedItems.containsKey(documentId), isSelected: selectedItems.containsKey(documentId),
onTap: () {
clearSelectedItems();
// Open a new screen when the user taps on the list item
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => DetailPage(
title: data['title'],
datum: formattedDate,
description: data['description'],
imageUrl: data['imageUrl'],
),
),
);
},
onLongPress: () { onLongPress: () {
setState(() { setState(() {
if (selectedItems.containsKey(documentId)) { if (selectedItems.containsKey(documentId)) {
@@ -261,10 +271,9 @@ class _GenericListState extends State<GenericList> {
child: FloatingActionButton( child: FloatingActionButton(
backgroundColor: Colors.blue, backgroundColor: Colors.blue,
onPressed: () { onPressed: () {
setState(() { clearSelectedItems();
selectedItems.clear();
});
}, },
tooltip: "Auswahl zurücksetzen",
child: const Icon(Icons.refresh), child: const Icon(Icons.refresh),
), ),
), ),
@@ -276,6 +285,7 @@ class _GenericListState extends State<GenericList> {
child: FloatingActionButton( child: FloatingActionButton(
backgroundColor: Colors.red, backgroundColor: Colors.red,
onPressed: () => _printSelectedItemsAsPdf(widget.selectedIndex), onPressed: () => _printSelectedItemsAsPdf(widget.selectedIndex),
tooltip: "Ausgewählte Elemente als pdf speichern",
child: const Icon(Icons.picture_as_pdf), child: const Icon(Icons.picture_as_pdf),
), ),
), ),
@@ -283,6 +293,18 @@ class _GenericListState extends State<GenericList> {
], ],
), ),
], ],
),
floatingActionButton: FloatingActionButton(
onPressed: () {
clearSelectedItems();
Navigator.push(
context,
MaterialPageRoute(builder: (context) => AddScreen(initialIndex: widget.selectedIndex)),
);
},
tooltip: "Neues Element hinzufügen",
child: const Icon(Icons.add),
), ),
); );
} }
@@ -293,14 +315,13 @@ class _GenericListState extends State<GenericList> {
if (response.statusCode != 200) { if (response.statusCode != 200) {
throw Exception('Failed to load network image.'); throw Exception('Failed to load network image.');
} }
final Uint8List bytes = response.bodyBytes; final Uint8List bytes = response.bodyBytes;
final img.Image? image = img.decodeImage(bytes); final img.Image? image = img.decodeImage(bytes);
final img.Image resizedImage = img.copyResize(image!, width: 500); // Beispielwert für die Breite final img.Image resizedImage = img.copyResize(image!, width: 500); // Beispielwert für die Breite
final Uint8List resizedBytes = Uint8List.fromList(img.encodeJpg(resizedImage)); final Uint8List resizedBytes = Uint8List.fromList(img.encodeJpg(resizedImage));
final imageProvider = pdfWidgets.MemoryImage(resizedBytes); final imageProvider = pdfWidgets.MemoryImage(resizedBytes);
return imageProvider; return imageProvider;
} }
@@ -310,7 +331,7 @@ class _GenericListState extends State<GenericList> {
final List<pdfWidgets.Widget> items = []; final List<pdfWidgets.Widget> items = [];
for (final entry in selectedItems.entries) { for (final entry in selectedItems.entries) {
final String title = entry.value['title'] ?? ''; 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 description = (entry.value['description'] ?? '').length > 165 ? (entry.value['description'] ?? '').substring(0, 165) + '...' : (entry.value['description'] ?? '');
final String imageUrl = entry.value['imageUrl'] ?? ''; final String imageUrl = entry.value['imageUrl'] ?? '';
final DateTime date = entry.value['createdDate'].toDate(); final DateTime date = entry.value['createdDate'].toDate();
final String formattedDate = DateFormat('dd.MM.yyyy').format(date); final String formattedDate = DateFormat('dd.MM.yyyy').format(date);
@@ -318,13 +339,13 @@ class _GenericListState extends State<GenericList> {
items.add( items.add(
pdfWidgets.Container( pdfWidgets.Container(
height: 150, // Begrenzt die Höhe des Containers height: 145, // Begrenzt die Höhe des Containers
padding: const pdfWidgets.EdgeInsets.all(5.0), // Reduziert den Abstand padding: const pdfWidgets.EdgeInsets.all(5.0), // Reduziert den Abstand
child: pdfWidgets.Row( child: pdfWidgets.Row(
children: [ children: [
pdfWidgets.Container( pdfWidgets.Container(
width: 150, // Reduziert die Breite des Bildes width: 145, // Reduziert die Breite des Bildes
height: 150, // Reduziert die Höhe des Bildes height: 145, // Reduziert die Höhe des Bildes
child: pdfWidgets.Image(imageProvider), child: pdfWidgets.Image(imageProvider),
), ),
pdfWidgets.Flexible( pdfWidgets.Flexible(
@@ -393,8 +414,6 @@ class _GenericListState extends State<GenericList> {
} }
await Printing.sharePdf(bytes: await pdf.save(), filename: filename); await Printing.sharePdf(bytes: await pdf.save(), filename: filename);
setState(() { clearSelectedItems();
selectedItems.clear();
});
} }
} }
-13
View File
@@ -58,19 +58,6 @@ class _MyHomePageState extends State<MyHomePage> {
body: Center( body: Center(
child: widgetOptions.elementAt(_selectedIndex), child: widgetOptions.elementAt(_selectedIndex),
), ),
floatingActionButton: _selectedIndex == 3 // Wenn der ausgewählte Index 3 ist, also auf dem Profil-Widget
? null // Zeigt keinen Button an
: FloatingActionButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => AddScreen(initialIndex: _selectedIndex)),
);
},
tooltip: "Add",
child: const Icon(Icons.add),
),
bottomNavigationBar: BottomNavigationBar( bottomNavigationBar: BottomNavigationBar(
items: const <BottomNavigationBarItem>[ items: const <BottomNavigationBarItem>[
BottomNavigationBarItem( BottomNavigationBarItem(