changed name from ui to author_id and reduced Resolution from images on pdf list to reduce pdf size

This commit is contained in:
2023-05-19 17:06:12 +02:00
parent c7fe414bc6
commit 9d41a5f5f1
3 changed files with 146 additions and 130 deletions
+124 -108
View File
@@ -7,14 +7,13 @@ import 'package:image_picker/image_picker.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:image/image.dart' as img;
class AddScreen extends StatefulWidget {
final int initialIndex;
const AddScreen({Key? key, required this.initialIndex}) : super(key: key);
@override
_AddScreenState createState() => _AddScreenState();
}
class _AddScreenState extends State<AddScreen> {
@@ -25,7 +24,6 @@ class _AddScreenState extends State<AddScreen> {
bool _uploading = false;
int _selectedIndex = 0;
@override
void initState() {
super.initState();
@@ -43,14 +41,13 @@ class _AddScreenState extends State<AddScreen> {
if (originalImage != null) {
int originSize = min(originalImage.width, originalImage.height);
final croppedImage = img.copyCrop(
originalImage,
x: (originalImage.width - originSize)~/2,
y: (originalImage.height - originSize)~/2,
final croppedImage = img.copyCrop(originalImage,
x: (originalImage.width - originSize) ~/ 2,
y: (originalImage.height - originSize) ~/ 2,
width: originSize,
height: originSize
);
final newPath = imageFile.path.substring(0, imageFile.path.lastIndexOf('/'));
height: originSize);
final newPath =
imageFile.path.substring(0, imageFile.path.lastIndexOf('/'));
final newFile = File('$newPath/cropped.jpg');
await newFile.writeAsBytes(img.encodePng(croppedImage));
return newFile;
@@ -72,17 +69,16 @@ class _AddScreenState extends State<AddScreen> {
if (originalImage != null) {
if (originalImage.width == originalImage.height) {
// Wenn das Bild bereits im 1:1-Format ist, überspringen Sie das Zuschneiden
if(mounted) {
if (mounted) {
setState(() {
_imageFile = tempFile;
print("1:1");
});
}
} else {
// Wenn das Bild nicht im 1:1-Format ist, schneiden Sie es zu
File croppedFile = await _cropImage(tempFile);
if(mounted) {
if (mounted) {
setState(() {
_imageFile = croppedFile;
});
@@ -94,15 +90,16 @@ class _AddScreenState extends State<AddScreen> {
}
} else {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Die Dateigröße darf maximal 10 MB betragen.')),
const SnackBar(
content: Text('Die Dateigröße darf maximal 10 MB betragen.')),
);
}
}
}
Future<void> addNewElementToFirestore(String title, String description, File imageFile, {required Function(bool) onComplete}) async {
Future<void> addNewElementToFirestore(
String title, String description, File imageFile,
{required Function(bool) onComplete}) async {
String collectionName;
if (_selectedIndex == 0) {
collectionName = "CitizensForum";
@@ -112,8 +109,9 @@ class _AddScreenState extends State<AddScreen> {
collectionName = "Issues";
}
String imageUrl = await uploadImageToFirebaseStorage(imageFile, collectionName);
String? userId = FirebaseAuth.instance.currentUser?.uid; // get the user ID
String imageUrl =
await uploadImageToFirebaseStorage(imageFile, collectionName);
String? userId = FirebaseAuth.instance.currentUser?.uid; // get the user ID
try {
await FirebaseFirestore.instance.collection(collectionName).add({
@@ -121,27 +119,29 @@ class _AddScreenState extends State<AddScreen> {
"description": description,
"imageUrl": imageUrl,
"createdDate": DateTime.now(),
"userId": userId, // add the user ID to the document
"author_uid": userId, // add the user ID to the document
});
onComplete(true);
} catch(e) {
} catch (e) {
onComplete(false);
}
}
Future<String> uploadImageToFirebaseStorage(File imageFile, String collectionName) async {
Reference ref = FirebaseStorage.instance.ref().child("$collectionName/${DateTime.now().toString()}");
Future<String> uploadImageToFirebaseStorage(
File imageFile, String collectionName) async {
Reference ref = FirebaseStorage.instance
.ref()
.child("$collectionName/${DateTime.now().toString()}");
UploadTask uploadTask = ref.putFile(imageFile);
return await (await uploadTask).ref.getDownloadURL();
}
@override
Widget build(BuildContext context) {
String appBarTitle = '';
double keyboardHeight = MediaQuery.of(context).viewInsets.bottom;
if (_selectedIndex == 0) {
appBarTitle = 'Neue BürgerForum Information';
} else if (_selectedIndex == 1) {
@@ -171,19 +171,27 @@ class _AddScreenState extends State<AddScreen> {
child: IconButton(
icon: const Icon(Icons.check),
onPressed: () async {
if (_formKey.currentState!.validate() && _imageFile != null && !_uploading) {
if (_formKey.currentState!.validate() &&
_imageFile != null &&
!_uploading) {
setState(() {
_uploading = true;
});
Navigator.pop(context); // Fenster direkt schließen, nachdem der Upload-Button gedrückt wurde.
await addNewElementToFirestore(_titleController.text, _descriptionController.text, _imageFile!, onComplete: (bool success) {
if(mounted) {
Navigator.pop(
context); // Fenster direkt schließen, nachdem der Upload-Button gedrückt wurde.
await addNewElementToFirestore(
_titleController.text,
_descriptionController.text,
_imageFile!, onComplete: (bool success) {
if (mounted) {
setState(() {
_uploading = false;
});
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(success ? 'Upload erfolgreich.' : 'Upload fehlgeschlagen.'),
content: Text(success
? 'Upload erfolgreich.'
: 'Upload fehlgeschlagen.'),
),
);
}
@@ -237,96 +245,104 @@ class _AddScreenState extends State<AddScreen> {
),
const SizedBox(height: 16.0),
_imageFile != null
? Image.file(_imageFile!)
: Container(
height: 200.0,
decoration: BoxDecoration(
color: Colors.grey[200],
borderRadius: BorderRadius.circular(16.0),
? Expanded(
child: LayoutBuilder(
builder: (context, constraints) {
double imageHeight = max(0, constraints.maxHeight - keyboardHeight);
return Container(
height: imageHeight,
child: Image.file(_imageFile!),
);
},
),
child: Center(
child: TextButton(
onPressed: () {
showModalBottomSheet(
context: context,
builder: (context) =>
Column(
mainAxisSize: MainAxisSize.min,
children: [
Container(
height: 60,
padding: const EdgeInsets.symmetric(
horizontal: 16),
child: Row(
mainAxisAlignment: MainAxisAlignment
.spaceBetween,
children: [
const Text(
'Bild hinzufügen',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
)
: Container(
height: 200.0,
decoration: BoxDecoration(
color: Colors.grey[200],
borderRadius: BorderRadius.circular(16.0),
),
child: Center(
child: TextButton(
onPressed: () {
showModalBottomSheet(
context: context,
builder: (context) => Column(
mainAxisSize: MainAxisSize.min,
children: [
Container(
height: 60,
padding: const EdgeInsets.symmetric(
horizontal: 16),
child: Row(
mainAxisAlignment:
MainAxisAlignment.spaceBetween,
children: [
const Text(
'Bild hinzufügen',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
),
),
),
IconButton(
icon: const Icon(Icons.close),
onPressed: () {
IconButton(
icon: const Icon(Icons.close),
onPressed: () {
Navigator.pop(context);
},
),
],
),
),
Center(
child: SizedBox(
height: 80,
child: ListTile(
leading: const Icon(Icons.photo_library,
size: 40),
title: const Text(
'Galerie auswählen',
style: TextStyle(fontSize: 20),
),
onTap: () {
Navigator.pop(context);
_getImage(ImageSource.gallery);
},
),
],
),
),
Center(
child: SizedBox(
height: 80,
child: ListTile(
leading: const Icon(
Icons.photo_library, size: 40),
title: const Text(
'Galerie auswählen',
style: TextStyle(fontSize: 20),
),
onTap: () {
Navigator.pop(context);
_getImage(ImageSource.gallery);
},
),
),
),
Center(
child: SizedBox(
height: 80,
child: ListTile(
leading: const Icon(
Icons.camera_alt, size: 40),
title: const Text(
'Kamera verwenden',
style: TextStyle(fontSize: 20),
Center(
child: SizedBox(
height: 80,
child: ListTile(
leading: const Icon(Icons.camera_alt,
size: 40),
title: const Text(
'Kamera verwenden',
style: TextStyle(fontSize: 20),
),
onTap: () {
Navigator.pop(context);
_getImage(ImageSource.camera);
},
),
onTap: () {
Navigator.pop(context);
_getImage(ImageSource.camera);
},
),
),
),
],
],
),
);
},
child: Text(
'Bild hinzufügen',
style: TextStyle(
color: Theme.of(context).primaryColor,
fontWeight: FontWeight.bold,
),
);
},
child: Text(
'Bild hinzufügen',
style: TextStyle(
color: Theme
.of(context)
.primaryColor,
fontWeight: FontWeight.bold,
),
),
),
),
),
),
),
],
),
),
+2 -2
View File
@@ -214,7 +214,7 @@ class _GenericListState extends State<GenericList> {
DateFormat('dd.MM.yyyy').format(createdDate);
final currentUser = FirebaseAuth.instance.currentUser;
if (currentUser != null && data['userId'] == currentUser.uid) {
if (currentUser != null && data['author_uid'] == currentUser.uid) {
return Dismissible(
key: UniqueKey(),
direction: DismissDirection.endToStart,
@@ -368,7 +368,7 @@ class _GenericListState extends State<GenericList> {
final Uint8List bytes = response.bodyBytes;
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: 290); // Beispielwert für die Breite
final Uint8List resizedBytes = Uint8List.fromList(img.encodeJpg(resizedImage));
final imageProvider = pdfWidgets.MemoryImage(resizedBytes);