Addpage optimierung, button wird ausgeblendet auf der profil page

This commit is contained in:
2023-04-28 00:08:05 +02:00
parent 6834e34b6a
commit 097f2744be
3 changed files with 143 additions and 252 deletions
+128 -91
View File
@@ -9,7 +9,7 @@ import 'package:cloud_firestore/cloud_firestore.dart';
class AddScreen extends StatefulWidget {
final int initialIndex;
AddScreen({required this.initialIndex});
const AddScreen({super.key, required this.initialIndex});
@override
_AddScreenState createState() => _AddScreenState();
@@ -23,14 +23,16 @@ class _AddScreenState extends State<AddScreen> {
bool _uploading = false;
int _selectedIndex = 0;
@override
void initState() {
super.initState();
_selectedIndex = widget.initialIndex;
}
Future<void> _getImage(ImageSource source) async {
final pickedFile = await ImagePicker().getImage(source: source);
final pickedFile = await ImagePicker().pickImage(source: source);
if (pickedFile != null) {
setState(() {
@@ -38,65 +40,53 @@ class _AddScreenState extends State<AddScreen> {
});
}
}
Future<void> addNewElementToFirestore(String title, String description, File imageFile) async {
// Zunächst das Bild in Firebase Storage speichern
// Das neue Element in der Firestore-Sammlung "Issues" speichern
if(_selectedIndex == 0)
{
Reference ref = FirebaseStorage.instance.ref().child("CitizensForum/${DateTime.now().toString()}");
UploadTask uploadTask = ref.putFile(imageFile);
String imageUrl = await (await uploadTask).ref.getDownloadURL();
await FirebaseFirestore.instance.collection("CitizensForum").add({
"title": title,
"description": description,
"imageUrl": imageUrl,
"createdDate": DateTime.now(),
});
}
else if(_selectedIndex == 1)
{
Reference ref = FirebaseStorage.instance.ref().child("News/${DateTime.now().toString()}");
UploadTask uploadTask = ref.putFile(imageFile);
String imageUrl = await (await uploadTask).ref.getDownloadURL();
await FirebaseFirestore.instance.collection("News").add({
"title": title,
"description": description,
"imageUrl": imageUrl,
"createdDate": DateTime.now(),
});
}
else
{
Reference ref = FirebaseStorage.instance.ref().child("Issues/${DateTime.now().toString()}");
UploadTask uploadTask = ref.putFile(imageFile);
String imageUrl = await (await uploadTask).ref.getDownloadURL();
await FirebaseFirestore.instance.collection("Issues").add({
"title": title,
"description": description,
"imageUrl": imageUrl,
"createdDate": DateTime.now(),
});
String collectionName;
if (_selectedIndex == 0) {
collectionName = "CitizensForum";
} else if (_selectedIndex == 1) {
collectionName = "News";
} else {
collectionName = "Issues";
}
String imageUrl = await uploadImageToFirebaseStorage(imageFile, collectionName);
await FirebaseFirestore.instance.collection(collectionName).add({
"title": title,
"description": description,
"imageUrl": imageUrl,
"createdDate": DateTime.now(),
});
}
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 = '';
if (_selectedIndex == 0) {
appBarTitle = 'Neue BürgerForum Information';
} else if (_selectedIndex == 1) {
appBarTitle = 'Neue Neuigkeit';
} else if (_selectedIndex == 2) {
appBarTitle = 'Neuer Mängel';
}
return Scaffold(
appBar: AppBar(
title: Text('Neues Element hinzufügen'),
title: Text(appBarTitle),
actions: [
Padding(
padding: const EdgeInsets.only(right: 8.0),
child: IconButton(
icon: Icon(Icons.refresh),
icon: const Icon(Icons.refresh),
onPressed: () {
_titleController.clear();
_descriptionController.clear();
@@ -109,14 +99,17 @@ class _AddScreenState extends State<AddScreen> {
Padding(
padding: const EdgeInsets.only(right: 8.0),
child: IconButton(
icon: Icon(Icons.check),
icon: const Icon(Icons.check),
onPressed: () async {
if (_formKey.currentState!.validate() && _imageFile != null && !_uploading) {
if (_formKey.currentState!.validate() && _imageFile != null &&
!_uploading) {
setState(() {
_uploading = true;
});
try {
await addNewElementToFirestore(_titleController.text, _descriptionController.text, _imageFile!);
await addNewElementToFirestore(
_titleController.text, _descriptionController.text,
_imageFile!);
Navigator.pop(context);
} catch (e) {
print(e);
@@ -128,7 +121,6 @@ class _AddScreenState extends State<AddScreen> {
}
}
},
),
),
],
@@ -156,7 +148,7 @@ class _AddScreenState extends State<AddScreen> {
return null;
},
),
SizedBox(height: 16.0),
const SizedBox(height: 16.0),
TextFormField(
decoration: InputDecoration(
labelText: 'Beschreibung',
@@ -174,53 +166,98 @@ class _AddScreenState extends State<AddScreen> {
return null;
},
),
SizedBox(height: 16.0),
const SizedBox(height: 16.0),
_imageFile != null
? Image.file(_imageFile!)
: Container(
height: 150.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: [
ListTile(
leading: Icon(Icons.photo_library),
title: Text('Galerie auswählen'),
onTap: () {
Navigator.pop(context);
_getImage(ImageSource.gallery);
}, //
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: () {
Navigator.pop(context);
},
),
],
),
ListTile(
leading: Icon(Icons.camera_alt),
title: Text('Kamera verwenden'),
onTap: () {
Navigator.pop(context);
_getImage(ImageSource.camera);
},
),
Center(
child: Container(
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);
},
),
),
],
),
);
},
child: Text(
'Bild hinzufügen',
style: TextStyle(
color: Theme.of(context).primaryColor,
fontWeight: FontWeight.bold,
),
Center(
child: Container(
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);
},
),
),
),
],
),
),
),
);
},
child: Text(
'Bild hinzufügen',
style: TextStyle(
color: Theme
.of(context)
.primaryColor,
fontWeight: FontWeight.bold,
),
),
),
),
),
],
),
),
+12 -160
View File
@@ -26,12 +26,13 @@ class UserDefinedItem extends StatelessWidget {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => DetailPage(
title: title,
datum: datum,
description: description,
image: image != null ? image! : null,
),
builder: (context) =>
DetailPage(
title: title,
datum: datum,
description: description,
image: image != null ? image! : null,
),
),
);
},
@@ -59,7 +60,8 @@ class UserDefinedItem extends StatelessWidget {
overflow: TextOverflow.ellipsis,
style: const TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold// beliebige Schriftgröße
fontWeight: FontWeight
.bold // beliebige Schriftgröße
),
),
),
@@ -73,7 +75,8 @@ class UserDefinedItem extends StatelessWidget {
),
),
Padding(
padding: const EdgeInsets.all(8.0), // feste Höhe für 2 Zeilen
padding: const EdgeInsets.all(8.0),
// feste Höhe für 2 Zeilen
child: Text(
description,
maxLines: 3,
@@ -264,57 +267,6 @@ class _CitizensForumState extends State<CitizensForum> {
itemBuilder: (BuildContext context, int index) {
final Map<String, dynamic> data = documents[index].data();
final DateTime createdDate = data['createdDate'].toDate();
final formattedDate = DateFormat.yMd().format(createdDate);
return UserDefinedItem(
title: data['title'] ?? '',
datum: formattedDate,
description: data['description'] ?? '',
image: Image.network((data['imageUrl'])),
);
},
);
}
},
);
}
}
/*class NewsList extends StatefulWidget {
const NewsList({Key? key}) : super(key: key);
@override
_NewsListState createState() => _NewsListState();
}
class _NewsListState extends State<NewsList> {
final FirebaseFirestore firestore = FirebaseFirestore.instance;
late Stream<QuerySnapshot> newsStream;
@override
void initState() {
super.initState();
newsStream = firestore.collection('News').snapshots();
}
@override
Widget build(BuildContext context) {
return StreamBuilder<QuerySnapshot>(
stream: newsStream,
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasError) {
return Text('Error: ${snapshot.error}');
}
switch (snapshot.connectionState) {
case ConnectionState.waiting:
return const Text('Loading...');
default:
final List<QueryDocumentSnapshot> documents = snapshot.data!.docs;
return ListView.builder(
itemCount: documents.length,
itemBuilder: (BuildContext context, int index) {
final Map<String, dynamic> data = documents[index].data()! as Map<String, dynamic>;
final DateTime createdDate = data['createdDate'].toDate();
final formattedDate = DateFormat('dd.MM.yyyy').format(createdDate);
return UserDefinedItem(
title: data['title'] ?? '',
@@ -328,104 +280,4 @@ class _NewsListState extends State<NewsList> {
},
);
}
}
class IssueList extends StatefulWidget {
const IssueList({Key? key}) : super(key: key);
@override
_IssueListState createState() => _IssueListState();
}
class _IssueListState extends State<IssueList> {
final FirebaseFirestore firestore = FirebaseFirestore.instance;
late Stream<QuerySnapshot> issueStream;
@override
void initState() {
super.initState();
issueStream = firestore.collection('Issues').snapshots();
}
@override
Widget build(BuildContext context) {
return StreamBuilder<QuerySnapshot>(
stream: issueStream,
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasError) {
return Text('Error: ${snapshot.error}');
}
switch (snapshot.connectionState) {
case ConnectionState.waiting:
return const Text('Loading...');
default:
final List<QueryDocumentSnapshot> documents = snapshot.data!.docs;
return ListView.builder(
itemCount: documents.length,
itemBuilder: (BuildContext context, int index) {
final Map<String, dynamic> data = documents[index].data()! as Map<String, dynamic>;
final DateTime createdDate = data['createdDate'].toDate();
final formattedDate = DateFormat('dd.MM.yyyy').format(createdDate);
return UserDefinedItem(
title: data['title'] ?? '',
datum: formattedDate,
description: data['description'] ?? '',
image: Image.network((data['imageUrl'])),
);
},
);
}
},
);
}
}
class CitizensForum extends StatefulWidget {
const CitizensForum({Key? key}) : super(key: key);
@override
_CitizensForumState createState() => _CitizensForumState();
}
class _CitizensForumState extends State<CitizensForum> {
final FirebaseFirestore firestore = FirebaseFirestore.instance;
late Stream<QuerySnapshot<Map<String, dynamic>>> citizenStream;
@override
void initState() {
super.initState();
citizenStream = firestore.collection('CitizensForum').snapshots();
}
@override
Widget build(BuildContext context) {
return StreamBuilder<QuerySnapshot<Map<String, dynamic>>>(
stream: citizenStream,
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot<Map<String, dynamic>>> snapshot) {
if (snapshot.hasError) {
return Text('Error: ${snapshot.error}');
}
switch (snapshot.connectionState) {
case ConnectionState.waiting:
return const Text('Loading...');
default:
final List<QueryDocumentSnapshot<Map<String, dynamic>>> documents = snapshot.data!.docs;
return ListView.builder(
itemCount: documents.length,
itemBuilder: (BuildContext context, int index) {
final Map<String, dynamic> data = documents[index].data();
final DateTime createdDate = data['createdDate'].toDate();
final formattedDate = DateFormat.yMd().format(createdDate);
return UserDefinedItem(
title: data['title'] ?? '',
datum: formattedDate,
description: data['description'] ?? '',
image: Image.network((data['imageUrl'])),
);
},
);
}
},
);
}
}*/
}
+3 -1
View File
@@ -57,7 +57,9 @@ class _MyHomePageState extends State<MyHomePage> {
body: Center(
child: widgetOptions.elementAt(_selectedIndex),
),
floatingActionButton: FloatingActionButton(
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,