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,4 +1,5 @@
|
||||
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';
|
||||
@@ -9,11 +10,13 @@ import '../services/storage_service.dart';
|
||||
class AddPostScreen extends StatefulWidget {
|
||||
final String collectionName;
|
||||
final String screenTitle;
|
||||
final PostItem? postToEdit;
|
||||
|
||||
const AddPostScreen({
|
||||
super.key,
|
||||
required this.collectionName,
|
||||
required this.screenTitle,
|
||||
this.postToEdit,
|
||||
});
|
||||
|
||||
@override
|
||||
@@ -29,8 +32,25 @@ class _AddPostScreenState extends State<AddPostScreen> {
|
||||
final StorageService _storageService = StorageService();
|
||||
|
||||
File? _selectedImage;
|
||||
String? _existingImageUrl;
|
||||
bool _isSaving = false;
|
||||
|
||||
bool get isEditing => widget.postToEdit != null;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
if (widget.postToEdit != null) {
|
||||
_titleController.text = widget.postToEdit!.title;
|
||||
_descriptionController.text = widget.postToEdit!.description;
|
||||
if (widget.postToEdit!.imageUrls.isNotEmpty) {
|
||||
_existingImageUrl = widget.postToEdit!.imageUrls.first;
|
||||
} else if (widget.postToEdit!.imageUrl.isNotEmpty) {
|
||||
_existingImageUrl = widget.postToEdit!.imageUrl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_titleController.dispose();
|
||||
@@ -100,38 +120,85 @@ class _AddPostScreenState extends State<AddPostScreen> {
|
||||
});
|
||||
|
||||
try {
|
||||
final currentUserId = FirebaseAuth.instance.currentUser?.uid;
|
||||
String imageUrl = '';
|
||||
if (isEditing) {
|
||||
String imageUrl = _existingImageUrl ?? '';
|
||||
|
||||
if (_selectedImage != null) {
|
||||
imageUrl = await _storageService.uploadFile(
|
||||
file: _selectedImage!,
|
||||
collectionName: widget.collectionName,
|
||||
optimize: true,
|
||||
if (_selectedImage != null) {
|
||||
// If a new image was chosen, remove the old one from storage if it existed
|
||||
if (widget.postToEdit!.imageUrls.isNotEmpty) {
|
||||
for (final oldUrl in widget.postToEdit!.imageUrls) {
|
||||
try {
|
||||
await _storageService.deleteFileByUrl(oldUrl);
|
||||
} catch (_) {}
|
||||
}
|
||||
}
|
||||
imageUrl = await _storageService.uploadFile(
|
||||
file: _selectedImage!,
|
||||
collectionName: widget.collectionName,
|
||||
optimize: true,
|
||||
);
|
||||
} else if (_existingImageUrl == null &&
|
||||
widget.postToEdit!.imageUrls.isNotEmpty) {
|
||||
// Existing image was deleted by the user
|
||||
for (final oldUrl in widget.postToEdit!.imageUrls) {
|
||||
try {
|
||||
await _storageService.deleteFileByUrl(oldUrl);
|
||||
} catch (_) {}
|
||||
}
|
||||
}
|
||||
|
||||
final updatedPost = widget.postToEdit!.copyWith(
|
||||
title: _titleController.text.trim(),
|
||||
description: _descriptionController.text.trim(),
|
||||
imageUrl: imageUrl,
|
||||
imageUrls: imageUrl.isNotEmpty ? [imageUrl] : [],
|
||||
);
|
||||
|
||||
await _firestoreService.updatePost(widget.collectionName, updatedPost);
|
||||
|
||||
if (!mounted) return;
|
||||
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('Eintrag erfolgreich aktualisiert!'),
|
||||
backgroundColor: Colors.green,
|
||||
),
|
||||
);
|
||||
Navigator.pop(context, updatedPost);
|
||||
} else {
|
||||
final currentUserId = FirebaseAuth.instance.currentUser?.uid;
|
||||
String imageUrl = '';
|
||||
|
||||
if (_selectedImage != null) {
|
||||
imageUrl = await _storageService.uploadFile(
|
||||
file: _selectedImage!,
|
||||
collectionName: widget.collectionName,
|
||||
optimize: true,
|
||||
);
|
||||
}
|
||||
|
||||
final newPost = PostItem(
|
||||
id: '',
|
||||
title: _titleController.text.trim(),
|
||||
description: _descriptionController.text.trim(),
|
||||
imageUrl: imageUrl,
|
||||
imageUrls: imageUrl.isNotEmpty ? [imageUrl] : [],
|
||||
createdDate: DateTime.now(),
|
||||
authorUid: currentUserId,
|
||||
);
|
||||
|
||||
await _firestoreService.addPost(widget.collectionName, newPost);
|
||||
|
||||
if (!mounted) return;
|
||||
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('Eintrag erfolgreich veröffentlicht!'),
|
||||
backgroundColor: Colors.green,
|
||||
),
|
||||
);
|
||||
Navigator.pop(context, newPost);
|
||||
}
|
||||
|
||||
final newPost = PostItem(
|
||||
id: '',
|
||||
title: _titleController.text.trim(),
|
||||
description: _descriptionController.text.trim(),
|
||||
imageUrl: imageUrl,
|
||||
imageUrls: imageUrl.isNotEmpty ? [imageUrl] : [],
|
||||
createdDate: DateTime.now(),
|
||||
authorUid: currentUserId,
|
||||
);
|
||||
|
||||
await _firestoreService.addPost(widget.collectionName, newPost);
|
||||
|
||||
if (!mounted) return;
|
||||
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('Eintrag erfolgreich veröffentlicht!'),
|
||||
backgroundColor: Colors.green,
|
||||
),
|
||||
);
|
||||
Navigator.pop(context);
|
||||
} catch (e) {
|
||||
if (!mounted) return;
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
@@ -153,7 +220,7 @@ class _AddPostScreenState extends State<AddPostScreen> {
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text(widget.screenTitle),
|
||||
title: Text(isEditing ? 'Beitrag bearbeiten' : widget.screenTitle),
|
||||
),
|
||||
body: SingleChildScrollView(
|
||||
padding: EdgeInsets.only(
|
||||
@@ -218,12 +285,68 @@ class _AddPostScreenState extends State<AddPostScreen> {
|
||||
backgroundColor: Colors.black54,
|
||||
child: IconButton(
|
||||
icon: const Icon(Icons.close, color: Colors.white),
|
||||
onPressed: () => setState(() => _selectedImage = null),
|
||||
onPressed: () =>
|
||||
setState(() => _selectedImage = null),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
)
|
||||
else if (_existingImageUrl != null &&
|
||||
_existingImageUrl!.isNotEmpty)
|
||||
Stack(
|
||||
children: [
|
||||
Container(
|
||||
height: 180,
|
||||
width: double.infinity,
|
||||
decoration: BoxDecoration(
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
border: Border.all(color: Colors.grey.shade400),
|
||||
),
|
||||
clipBehavior: Clip.antiAlias,
|
||||
child: CachedNetworkImage(
|
||||
imageUrl: _existingImageUrl!,
|
||||
fit: BoxFit.cover,
|
||||
placeholder: (ctx, url) => const Center(
|
||||
child: CircularProgressIndicator(),
|
||||
),
|
||||
errorWidget: (ctx, url, err) => Container(
|
||||
color: Colors.grey.shade200,
|
||||
child: const Icon(Icons.broken_image, size: 40),
|
||||
),
|
||||
),
|
||||
),
|
||||
Positioned(
|
||||
top: 8,
|
||||
right: 8,
|
||||
child: CircleAvatar(
|
||||
backgroundColor: Colors.black54,
|
||||
child: IconButton(
|
||||
icon: const Icon(Icons.close, color: Colors.white),
|
||||
tooltip: 'Bild entfernen',
|
||||
onPressed: () =>
|
||||
setState(() => _existingImageUrl = null),
|
||||
),
|
||||
),
|
||||
),
|
||||
Positioned(
|
||||
bottom: 8,
|
||||
right: 8,
|
||||
child: ElevatedButton.icon(
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: Colors.black54,
|
||||
foregroundColor: Colors.white,
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 10, vertical: 6),
|
||||
),
|
||||
onPressed: _showImageSourceModal,
|
||||
icon: const Icon(Icons.camera_alt, size: 16),
|
||||
label: const Text('Ersetzen',
|
||||
style: TextStyle(fontSize: 12)),
|
||||
),
|
||||
),
|
||||
],
|
||||
)
|
||||
else
|
||||
InkWell(
|
||||
onTap: _showImageSourceModal,
|
||||
@@ -238,9 +361,11 @@ class _AddPostScreenState extends State<AddPostScreen> {
|
||||
child: const Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Icon(Icons.add_photo_alternate, size: 36, color: Colors.grey),
|
||||
Icon(Icons.add_photo_alternate,
|
||||
size: 36, color: Colors.grey),
|
||||
SizedBox(height: 6),
|
||||
Text('Foto hinzufügen', style: TextStyle(color: Colors.black54)),
|
||||
Text('Foto hinzufügen',
|
||||
style: TextStyle(color: Colors.black54)),
|
||||
],
|
||||
),
|
||||
),
|
||||
@@ -258,9 +383,13 @@ class _AddPostScreenState extends State<AddPostScreen> {
|
||||
child: CircularProgressIndicator(
|
||||
strokeWidth: 2, color: Colors.white),
|
||||
)
|
||||
: const Icon(Icons.send),
|
||||
: Icon(isEditing ? Icons.save : Icons.send),
|
||||
label: Text(
|
||||
_isSaving ? 'Wird gespeichert...' : 'Veröffentlichen',
|
||||
_isSaving
|
||||
? 'Wird gespeichert...'
|
||||
: (isEditing
|
||||
? 'Änderungen speichern'
|
||||
: 'Veröffentlichen'),
|
||||
style: const TextStyle(fontSize: 16),
|
||||
),
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user