first aut
This commit is contained in:
+30
-23
@@ -1,5 +1,6 @@
|
||||
import 'dart:io';
|
||||
import 'dart:math';
|
||||
import 'package:firebase_auth/firebase_auth.dart';
|
||||
import 'package:firebase_storage/firebase_storage.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:image_picker/image_picker.dart';
|
||||
@@ -10,7 +11,7 @@ import 'package:image/image.dart' as img;
|
||||
class AddScreen extends StatefulWidget {
|
||||
final int initialIndex;
|
||||
|
||||
const AddScreen({super.key, required this.initialIndex});
|
||||
const AddScreen({Key? key, required this.initialIndex}) : super(key: key);
|
||||
@override
|
||||
_AddScreenState createState() => _AddScreenState();
|
||||
|
||||
@@ -71,18 +72,22 @@ 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
|
||||
setState(() {
|
||||
_imageFile = tempFile;
|
||||
print("1:1");
|
||||
});
|
||||
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);
|
||||
setState(() {
|
||||
_imageFile = croppedFile;
|
||||
});
|
||||
print("decode");
|
||||
if(mounted) {
|
||||
setState(() {
|
||||
_imageFile = croppedFile;
|
||||
});
|
||||
print("decode");
|
||||
}
|
||||
}
|
||||
} else {
|
||||
throw Exception('Unable to decode image file.');
|
||||
@@ -108,23 +113,23 @@ class _AddScreenState extends State<AddScreen> {
|
||||
}
|
||||
|
||||
String imageUrl = await uploadImageToFirebaseStorage(imageFile, collectionName);
|
||||
try
|
||||
{
|
||||
String? userId = FirebaseAuth.instance.currentUser?.uid; // get the user ID
|
||||
|
||||
try {
|
||||
await FirebaseFirestore.instance.collection(collectionName).add({
|
||||
"title": title,
|
||||
"description": description,
|
||||
"imageUrl": imageUrl,
|
||||
"createdDate": DateTime.now(),
|
||||
"userId": 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()}");
|
||||
UploadTask uploadTask = ref.putFile(imageFile);
|
||||
@@ -172,14 +177,16 @@ class _AddScreenState extends State<AddScreen> {
|
||||
});
|
||||
Navigator.pop(context); // Fenster direkt schließen, nachdem der Upload-Button gedrückt wurde.
|
||||
await addNewElementToFirestore(_titleController.text, _descriptionController.text, _imageFile!, onComplete: (bool success) {
|
||||
setState(() {
|
||||
_uploading = false;
|
||||
});
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(success ? 'Upload erfolgreich.' : 'Upload fehlgeschlagen.'),
|
||||
),
|
||||
);
|
||||
if(mounted) {
|
||||
setState(() {
|
||||
_uploading = false;
|
||||
});
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(success ? 'Upload erfolgreich.' : 'Upload fehlgeschlagen.'),
|
||||
),
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
+76
-26
@@ -1,9 +1,11 @@
|
||||
import 'package:cached_network_image/cached_network_image.dart';
|
||||
import 'package:cloud_firestore/cloud_firestore.dart';
|
||||
import 'package:firebase_auth/firebase_auth.dart';
|
||||
import 'package:firebase_storage/firebase_storage.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:printing/printing.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'AddPage.dart';
|
||||
import 'DetailPage.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
@@ -11,6 +13,8 @@ import 'package:pdf/widgets.dart' as pdfWidgets;
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:image/image.dart' as img;
|
||||
|
||||
import 'anonym_aut.dart';
|
||||
|
||||
class UserDefinedItem extends StatelessWidget {
|
||||
final String title;
|
||||
final String datum;
|
||||
@@ -208,23 +212,59 @@ class _GenericListState extends State<GenericList> {
|
||||
final DateTime createdDate = data['createdDate'].toDate();
|
||||
final formattedDate =
|
||||
DateFormat('dd.MM.yyyy').format(createdDate);
|
||||
return Dismissible(
|
||||
key: UniqueKey(),
|
||||
direction: DismissDirection.endToStart,
|
||||
onDismissed: (direction) {
|
||||
_deleteDocument(documentId, data['imageUrl']);
|
||||
},
|
||||
background: Container(
|
||||
color: Colors.red,
|
||||
child: const Align(
|
||||
alignment: Alignment.centerRight,
|
||||
child: Padding(
|
||||
padding: EdgeInsets.only(right: 16),
|
||||
child: Icon(Icons.delete, color: Colors.white),
|
||||
final currentUser = FirebaseAuth.instance.currentUser;
|
||||
|
||||
if (currentUser != null && data['userId'] == currentUser.uid) {
|
||||
return Dismissible(
|
||||
key: UniqueKey(),
|
||||
direction: DismissDirection.endToStart,
|
||||
onDismissed: (direction) {
|
||||
_deleteDocument(documentId, data['imageUrl']);
|
||||
},
|
||||
background: Container(
|
||||
color: Colors.red,
|
||||
child: const Align(
|
||||
alignment: Alignment.centerRight,
|
||||
child: Padding(
|
||||
padding: EdgeInsets.only(right: 16),
|
||||
child: Icon(Icons.delete, color: Colors.white),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
child: UserDefinedItem(
|
||||
child: UserDefinedItem(
|
||||
title: data['title'] ?? '',
|
||||
datum: formattedDate,
|
||||
description: data['description'] ?? '',
|
||||
imageUrl: data['imageUrl'] ?? '',
|
||||
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: () {
|
||||
setState(() {
|
||||
if (selectedItems.containsKey(documentId)) {
|
||||
selectedItems.remove(documentId);
|
||||
} else {
|
||||
selectedItems[documentId] = data;
|
||||
}
|
||||
});
|
||||
},
|
||||
),
|
||||
);
|
||||
} else {
|
||||
return UserDefinedItem(
|
||||
title: data['title'] ?? '',
|
||||
datum: formattedDate,
|
||||
description: data['description'] ?? '',
|
||||
@@ -254,8 +294,9 @@ class _GenericListState extends State<GenericList> {
|
||||
}
|
||||
});
|
||||
},
|
||||
),
|
||||
);
|
||||
);
|
||||
}
|
||||
|
||||
},
|
||||
);
|
||||
}
|
||||
@@ -295,17 +336,26 @@ class _GenericListState extends State<GenericList> {
|
||||
],
|
||||
|
||||
),
|
||||
floatingActionButton: FloatingActionButton(
|
||||
onPressed: () {
|
||||
clearSelectedItems();
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(builder: (context) => AddScreen(initialIndex: widget.selectedIndex)),
|
||||
);
|
||||
floatingActionButton: Consumer<AuthState>(
|
||||
builder: (context, authState, child) {
|
||||
if (authState.isSignedIn) {
|
||||
return FloatingActionButton(
|
||||
onPressed: () {
|
||||
clearSelectedItems();
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(builder: (context) => AddScreen(initialIndex: widget.selectedIndex)),
|
||||
);
|
||||
},
|
||||
tooltip: "Neues Element hinzufügen",
|
||||
child: const Icon(Icons.add),
|
||||
);
|
||||
} else {
|
||||
return Container(); // Return an empty container when the user is not signed in
|
||||
}
|
||||
},
|
||||
tooltip: "Neues Element hinzufügen",
|
||||
child: const Icon(Icons.add),
|
||||
),
|
||||
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:firebase_auth/firebase_auth.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
class AuthState extends ChangeNotifier {
|
||||
FirebaseAuth auth = FirebaseAuth.instance;
|
||||
bool _isSignedIn = false;
|
||||
|
||||
bool get isSignedIn => _isSignedIn;
|
||||
|
||||
Future<void> signInAnonymously() async {
|
||||
try {
|
||||
await auth.signInAnonymously();
|
||||
_isSignedIn = true;
|
||||
notifyListeners();
|
||||
} catch (e) {
|
||||
print(e);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> signOut() async {
|
||||
await auth.signOut();
|
||||
_isSignedIn = false;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
class AnonymAuthScreen extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
body: Center(
|
||||
child: Consumer<AuthState>(
|
||||
builder: (context, authState, child) {
|
||||
return ElevatedButton(
|
||||
onPressed: authState.isSignedIn
|
||||
? authState.signOut
|
||||
: authState.signInAnonymously,
|
||||
child: Text(authState.isSignedIn ? 'Abmelden' : 'Anonym anmelden'),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
+2
-1
@@ -1,6 +1,7 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:oggauergemeindetrommler/LoginPage.dart';
|
||||
import 'UserDefinedList.dart';
|
||||
import 'anonym_aut.dart';
|
||||
|
||||
class MyHomePage extends StatefulWidget {
|
||||
const MyHomePage({Key? key, required this.title});
|
||||
@@ -23,7 +24,7 @@ class _MyHomePageState extends State<MyHomePage> {
|
||||
const GenericList(collectionName: 'CitizensForum', selectedIndex: 0,),
|
||||
const GenericList(collectionName: 'News', selectedIndex: 1,),
|
||||
const GenericList(collectionName: 'Issues', selectedIndex: 2,),
|
||||
LoginPage()
|
||||
AnonymAuthScreen()
|
||||
];
|
||||
|
||||
return Scaffold(
|
||||
|
||||
+9
-2
@@ -1,4 +1,6 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'anonym_aut.dart';
|
||||
import 'home.dart';
|
||||
import 'package:firebase_core/firebase_core.dart';
|
||||
import 'firebase_options.dart';
|
||||
@@ -8,10 +10,15 @@ void main() async {
|
||||
await Firebase.initializeApp(
|
||||
options: DefaultFirebaseOptions.currentPlatform,
|
||||
);
|
||||
runApp(const MyApp());
|
||||
|
||||
runApp(
|
||||
ChangeNotifierProvider(
|
||||
create: (context) => AuthState(),
|
||||
child: const MyApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
const MyApp({super.key});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user