feat: pinch-to-zoom gestures for PDF reader & post editing for admins and authors
Build Android APK / Build Release APKs (push) Successful in 14m41s
Build Android APK / Build Release APKs (push) Successful in 14m41s
This commit is contained in:
@@ -1,13 +1,18 @@
|
||||
import 'dart:io';
|
||||
import 'package:cached_network_image/cached_network_image.dart';
|
||||
import 'package:firebase_auth/firebase_auth.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:image_picker/image_picker.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import '../models/post_item.dart';
|
||||
import '../services/auth_service.dart';
|
||||
import '../services/firestore_service.dart';
|
||||
import '../services/storage_service.dart';
|
||||
|
||||
class AddIssueScreen extends StatefulWidget {
|
||||
const AddIssueScreen({super.key});
|
||||
final PostItem? issueToEdit;
|
||||
|
||||
const AddIssueScreen({super.key, this.issueToEdit});
|
||||
|
||||
@override
|
||||
State<AddIssueScreen> createState() => _AddIssueScreenState();
|
||||
@@ -22,8 +27,18 @@ class _AddIssueScreenState extends State<AddIssueScreen> {
|
||||
final StorageService _storageService = StorageService();
|
||||
|
||||
final List<File> _selectedImages = [];
|
||||
List<String> _existingImageUrls = [];
|
||||
String _selectedStatus = 'Gemeldet';
|
||||
bool _isSaving = false;
|
||||
|
||||
bool get isEditing => widget.issueToEdit != null;
|
||||
|
||||
final List<String> _statuses = [
|
||||
'Gemeldet',
|
||||
'In Bearbeitung',
|
||||
'Erledigt',
|
||||
];
|
||||
|
||||
final List<String> _streets = [
|
||||
'Hauptstraße',
|
||||
'Seestraße',
|
||||
@@ -56,8 +71,31 @@ class _AddIssueScreenState extends State<AddIssueScreen> {
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_selectedStreet = _streets.first;
|
||||
_selectedCategory = _categories.first;
|
||||
if (widget.issueToEdit != null) {
|
||||
final issue = widget.issueToEdit!;
|
||||
_titleController.text = issue.title;
|
||||
_descriptionController.text = issue.description;
|
||||
if (issue.street != null && _streets.contains(issue.street)) {
|
||||
_selectedStreet = issue.street;
|
||||
} else {
|
||||
_selectedStreet = _streets.first;
|
||||
}
|
||||
if (issue.category != null && _categories.contains(issue.category)) {
|
||||
_selectedCategory = issue.category;
|
||||
} else {
|
||||
_selectedCategory = _categories.first;
|
||||
}
|
||||
_existingImageUrls = List<String>.from(issue.imageUrls);
|
||||
if (_existingImageUrls.isEmpty && issue.imageUrl.isNotEmpty) {
|
||||
_existingImageUrls.add(issue.imageUrl);
|
||||
}
|
||||
if (issue.status != null && _statuses.contains(issue.status)) {
|
||||
_selectedStatus = issue.status!;
|
||||
}
|
||||
} else {
|
||||
_selectedStreet = _streets.first;
|
||||
_selectedCategory = _categories.first;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -68,9 +106,10 @@ class _AddIssueScreenState extends State<AddIssueScreen> {
|
||||
}
|
||||
|
||||
Future<void> _pickImage(ImageSource source) async {
|
||||
if (_selectedImages.length >= 2) {
|
||||
if (_existingImageUrls.length + _selectedImages.length >= 2) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(content: Text('Es können maximal 2 Fotos hinzugefügt werden.')),
|
||||
const SnackBar(
|
||||
content: Text('Es können maximal 2 Fotos hinzugefügt werden.')),
|
||||
);
|
||||
return;
|
||||
}
|
||||
@@ -136,43 +175,89 @@ class _AddIssueScreenState extends State<AddIssueScreen> {
|
||||
});
|
||||
|
||||
try {
|
||||
final currentUserId = FirebaseAuth.instance.currentUser?.uid;
|
||||
if (isEditing) {
|
||||
// Delete removed old images from Storage
|
||||
for (final oldUrl in widget.issueToEdit!.imageUrls) {
|
||||
if (!_existingImageUrls.contains(oldUrl)) {
|
||||
try {
|
||||
await _storageService.deleteFileByUrl(oldUrl);
|
||||
} catch (_) {}
|
||||
}
|
||||
}
|
||||
|
||||
// Upload selected images to Firebase Storage
|
||||
final List<String> uploadedUrls = [];
|
||||
for (final file in _selectedImages) {
|
||||
final url = await _storageService.uploadFile(
|
||||
file: file,
|
||||
collectionName: 'Issues',
|
||||
optimize: true,
|
||||
// Upload newly selected images to Storage
|
||||
final List<String> newlyUploadedUrls = [];
|
||||
for (final file in _selectedImages) {
|
||||
final url = await _storageService.uploadFile(
|
||||
file: file,
|
||||
collectionName: 'Issues',
|
||||
optimize: true,
|
||||
);
|
||||
newlyUploadedUrls.add(url);
|
||||
}
|
||||
|
||||
final allImages = [..._existingImageUrls, ...newlyUploadedUrls];
|
||||
|
||||
final updatedIssue = widget.issueToEdit!.copyWith(
|
||||
title: _titleController.text.trim(),
|
||||
description: _descriptionController.text.trim(),
|
||||
imageUrl: allImages.isNotEmpty ? allImages.first : '',
|
||||
imageUrls: allImages,
|
||||
street: _selectedStreet,
|
||||
category: _selectedCategory,
|
||||
status: _selectedStatus,
|
||||
);
|
||||
uploadedUrls.add(url);
|
||||
|
||||
await _firestoreService.updatePost('Issues', updatedIssue);
|
||||
|
||||
if (!mounted) return;
|
||||
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('Mangel erfolgreich aktualisiert!'),
|
||||
backgroundColor: Colors.green,
|
||||
),
|
||||
);
|
||||
Navigator.pop(context, updatedIssue);
|
||||
} else {
|
||||
final currentUserId = FirebaseAuth.instance.currentUser?.uid;
|
||||
|
||||
// Upload selected images to Firebase Storage
|
||||
final List<String> uploadedUrls = [];
|
||||
for (final file in _selectedImages) {
|
||||
final url = await _storageService.uploadFile(
|
||||
file: file,
|
||||
collectionName: 'Issues',
|
||||
optimize: true,
|
||||
);
|
||||
uploadedUrls.add(url);
|
||||
}
|
||||
|
||||
final newIssue = PostItem(
|
||||
id: '',
|
||||
title: _titleController.text.trim(),
|
||||
description: _descriptionController.text.trim(),
|
||||
imageUrl: uploadedUrls.isNotEmpty ? uploadedUrls.first : '',
|
||||
imageUrls: uploadedUrls,
|
||||
createdDate: DateTime.now(),
|
||||
authorUid: currentUserId,
|
||||
street: _selectedStreet,
|
||||
category: _selectedCategory,
|
||||
status: 'Gemeldet',
|
||||
);
|
||||
|
||||
await _firestoreService.addPost('Issues', newIssue);
|
||||
|
||||
if (!mounted) return;
|
||||
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('Mangel wurde erfolgreich gemeldet!'),
|
||||
backgroundColor: Colors.green,
|
||||
),
|
||||
);
|
||||
Navigator.pop(context, newIssue);
|
||||
}
|
||||
|
||||
final newIssue = PostItem(
|
||||
id: '',
|
||||
title: _titleController.text.trim(),
|
||||
description: _descriptionController.text.trim(),
|
||||
imageUrl: uploadedUrls.isNotEmpty ? uploadedUrls.first : '',
|
||||
imageUrls: uploadedUrls,
|
||||
createdDate: DateTime.now(),
|
||||
authorUid: currentUserId,
|
||||
street: _selectedStreet,
|
||||
category: _selectedCategory,
|
||||
status: 'Gemeldet',
|
||||
);
|
||||
|
||||
await _firestoreService.addPost('Issues', newIssue);
|
||||
|
||||
if (!mounted) return;
|
||||
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('Mangel wurde erfolgreich gemeldet!'),
|
||||
backgroundColor: Colors.green,
|
||||
),
|
||||
);
|
||||
Navigator.pop(context);
|
||||
} catch (e) {
|
||||
if (!mounted) return;
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
@@ -192,9 +277,13 @@ class _AddIssueScreenState extends State<AddIssueScreen> {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final auth = Provider.of<AuthService>(context, listen: false);
|
||||
final isAdmin = auth.isAdmin;
|
||||
final totalPhotos = _existingImageUrls.length + _selectedImages.length;
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Mangel melden'),
|
||||
title: Text(isEditing ? 'Mangel bearbeiten' : 'Mangel melden'),
|
||||
),
|
||||
body: SingleChildScrollView(
|
||||
padding: EdgeInsets.only(
|
||||
@@ -251,6 +340,26 @@ class _AddIssueScreenState extends State<AddIssueScreen> {
|
||||
.toList(),
|
||||
onChanged: (val) => setState(() => _selectedCategory = val),
|
||||
),
|
||||
if (isAdmin && isEditing) ...[
|
||||
const SizedBox(height: 16),
|
||||
DropdownButtonFormField<String>(
|
||||
initialValue: _selectedStatus,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Bearbeitungsstatus (Admin) *',
|
||||
prefixIcon: Icon(Icons.assignment_turned_in_outlined),
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
items: _statuses
|
||||
.map((status) => DropdownMenuItem(
|
||||
value: status,
|
||||
child: Text(status),
|
||||
))
|
||||
.toList(),
|
||||
onChanged: (val) {
|
||||
if (val != null) setState(() => _selectedStatus = val);
|
||||
},
|
||||
),
|
||||
],
|
||||
const SizedBox(height: 16),
|
||||
TextFormField(
|
||||
controller: _descriptionController,
|
||||
@@ -271,67 +380,127 @@ class _AddIssueScreenState extends State<AddIssueScreen> {
|
||||
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 14),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Row(
|
||||
children: [
|
||||
..._selectedImages.asMap().entries.map((entry) {
|
||||
final index = entry.key;
|
||||
final file = entry.value;
|
||||
return Stack(
|
||||
children: [
|
||||
Container(
|
||||
margin: const EdgeInsets.only(right: 12),
|
||||
SingleChildScrollView(
|
||||
scrollDirection: Axis.horizontal,
|
||||
child: Row(
|
||||
children: [
|
||||
// Existing images from Firebase Storage
|
||||
..._existingImageUrls.asMap().entries.map((entry) {
|
||||
final index = entry.key;
|
||||
final url = entry.value;
|
||||
return Stack(
|
||||
children: [
|
||||
Container(
|
||||
margin: const EdgeInsets.only(right: 12),
|
||||
width: 110,
|
||||
height: 80,
|
||||
decoration: BoxDecoration(
|
||||
border: Border.all(color: Colors.grey.shade400),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
clipBehavior: Clip.antiAlias,
|
||||
child: CachedNetworkImage(
|
||||
imageUrl: url,
|
||||
fit: BoxFit.cover,
|
||||
placeholder: (ctx, u) => const Center(
|
||||
child: CircularProgressIndicator(),
|
||||
),
|
||||
errorWidget: (ctx, u, e) => Container(
|
||||
color: Colors.grey.shade200,
|
||||
child: const Icon(Icons.broken_image, size: 28),
|
||||
),
|
||||
),
|
||||
),
|
||||
Positioned(
|
||||
top: -4,
|
||||
right: 4,
|
||||
child: CircleAvatar(
|
||||
radius: 14,
|
||||
backgroundColor: Colors.black54,
|
||||
child: IconButton(
|
||||
padding: EdgeInsets.zero,
|
||||
icon: const Icon(Icons.close,
|
||||
color: Colors.white, size: 16),
|
||||
tooltip: 'Foto entfernen',
|
||||
onPressed: () {
|
||||
setState(() {
|
||||
_existingImageUrls.removeAt(index);
|
||||
});
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}),
|
||||
// Newly picked local image files
|
||||
..._selectedImages.asMap().entries.map((entry) {
|
||||
final index = entry.key;
|
||||
final file = entry.value;
|
||||
return Stack(
|
||||
children: [
|
||||
Container(
|
||||
margin: const EdgeInsets.only(right: 12),
|
||||
width: 110,
|
||||
height: 80,
|
||||
decoration: BoxDecoration(
|
||||
border: Border.all(color: Colors.grey.shade400),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
image: DecorationImage(
|
||||
image: FileImage(file),
|
||||
fit: BoxFit.cover,
|
||||
),
|
||||
),
|
||||
),
|
||||
Positioned(
|
||||
top: -4,
|
||||
right: 4,
|
||||
child: CircleAvatar(
|
||||
radius: 14,
|
||||
backgroundColor: Colors.black54,
|
||||
child: IconButton(
|
||||
padding: EdgeInsets.zero,
|
||||
icon: const Icon(Icons.close,
|
||||
color: Colors.white, size: 16),
|
||||
tooltip: 'Foto entfernen',
|
||||
onPressed: () {
|
||||
setState(() {
|
||||
_selectedImages.removeAt(index);
|
||||
});
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}),
|
||||
if (totalPhotos < 2)
|
||||
InkWell(
|
||||
onTap: _showImageSourceModal,
|
||||
child: Container(
|
||||
width: 110,
|
||||
height: 80,
|
||||
decoration: BoxDecoration(
|
||||
border: Border.all(color: Colors.grey.shade400),
|
||||
color: Colors.grey.shade200,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
image: DecorationImage(
|
||||
image: FileImage(file),
|
||||
fit: BoxFit.cover,
|
||||
),
|
||||
border: Border.all(
|
||||
color: Colors.grey.shade400,
|
||||
style: BorderStyle.solid),
|
||||
),
|
||||
),
|
||||
Positioned(
|
||||
top: -4,
|
||||
right: 4,
|
||||
child: IconButton(
|
||||
icon: const Icon(Icons.cancel, color: Colors.red),
|
||||
onPressed: () {
|
||||
setState(() {
|
||||
_selectedImages.removeAt(index);
|
||||
});
|
||||
},
|
||||
child: const Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Icon(Icons.add_a_photo, color: Colors.grey),
|
||||
SizedBox(height: 4),
|
||||
Text('Foto hinzufügen',
|
||||
style: TextStyle(
|
||||
fontSize: 11, color: Colors.black54)),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}),
|
||||
if (_selectedImages.length < 2)
|
||||
InkWell(
|
||||
onTap: _showImageSourceModal,
|
||||
child: Container(
|
||||
width: 110,
|
||||
height: 80,
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.grey.shade200,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
border: Border.all(
|
||||
color: Colors.grey.shade400,
|
||||
style: BorderStyle.solid),
|
||||
),
|
||||
child: const Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Icon(Icons.add_a_photo, color: Colors.grey),
|
||||
SizedBox(height: 4),
|
||||
Text('Foto hinzufügen',
|
||||
style: TextStyle(
|
||||
fontSize: 11, color: Colors.black54)),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 32),
|
||||
SizedBox(
|
||||
@@ -346,9 +515,13 @@ class _AddIssueScreenState extends State<AddIssueScreen> {
|
||||
child: CircularProgressIndicator(
|
||||
strokeWidth: 2, color: Colors.white),
|
||||
)
|
||||
: const Icon(Icons.check_circle_outline),
|
||||
: Icon(isEditing ? Icons.save : Icons.check_circle_outline),
|
||||
label: Text(
|
||||
_isSaving ? 'Wird gespeichert...' : 'Mangel fertigstellen',
|
||||
_isSaving
|
||||
? 'Wird gespeichert...'
|
||||
: (isEditing
|
||||
? 'Änderungen speichern'
|
||||
: 'Mangel fertigstellen'),
|
||||
style: const TextStyle(fontSize: 16),
|
||||
),
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user