2024-05-15 23:24:26 +02:00
|
|
|
import 'package:cloud_firestore/cloud_firestore.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
|
|
|
import '../constants.dart';
|
|
|
|
import '../services/auth/auth_service.dart';
|
|
|
|
|
|
|
|
class FeedbackDialog extends StatefulWidget {
|
|
|
|
const FeedbackDialog({super.key});
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<FeedbackDialog> createState() => _FeedbackDialogState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _FeedbackDialogState extends State<FeedbackDialog> {
|
|
|
|
final TextEditingController _feedbackController = TextEditingController();
|
|
|
|
final GlobalKey<FormState> _formKey = GlobalKey();
|
|
|
|
|
|
|
|
// get instance of firestore and auth
|
|
|
|
final FirebaseFirestore _firestore = FirebaseFirestore.instance;
|
|
|
|
final AuthService _authService = AuthService();
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
_feedbackController.dispose();
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return AlertDialog(
|
|
|
|
content: Form(
|
|
|
|
key: _formKey,
|
|
|
|
child: TextFormField(
|
|
|
|
controller: _feedbackController,
|
|
|
|
keyboardType: TextInputType.multiline,
|
|
|
|
decoration: const InputDecoration(
|
|
|
|
hintText: 'Please enter your feedback here',
|
|
|
|
filled: true,
|
|
|
|
),
|
|
|
|
maxLines: 5,
|
|
|
|
maxLength: 4096,
|
|
|
|
textInputAction: TextInputAction.done,
|
|
|
|
validator: (String? text) {
|
|
|
|
if (text == null || text.isEmpty) {
|
|
|
|
return 'Please enter something';
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
actions: [
|
|
|
|
TextButton(
|
|
|
|
child: const Text('Cancel'),
|
|
|
|
onPressed: () => Navigator.pop(context),
|
|
|
|
),
|
|
|
|
TextButton(
|
|
|
|
style: TextButton.styleFrom(
|
|
|
|
backgroundColor: Theme.of(context).colorScheme.secondary,
|
|
|
|
),
|
|
|
|
child: const Text(
|
|
|
|
'Send feedback',
|
|
|
|
style: TextStyle(
|
|
|
|
color: Colors.blue,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
onPressed: () async {
|
|
|
|
// Only if the input form is valid (the user has entered text)
|
|
|
|
if (_formKey.currentState!.validate()) {
|
|
|
|
String message;
|
|
|
|
bool error = false;
|
|
|
|
|
|
|
|
try {
|
|
|
|
// Get a reference to the feedbacks collection
|
|
|
|
final collection =
|
|
|
|
_firestore.collection(Constants.dbCollectionFeedbacks);
|
|
|
|
|
|
|
|
// Write the server's timestamp and the user's feedback
|
2024-05-31 23:55:33 +02:00
|
|
|
await collection.add({
|
2024-05-15 23:24:26 +02:00
|
|
|
'timestamp': FieldValue.serverTimestamp(),
|
|
|
|
'feedback': _feedbackController.text,
|
|
|
|
'user': _authService.getCurrentUser()!.uid,
|
2024-05-31 23:55:33 +02:00
|
|
|
'email': _authService.getCurrentUser()!.email,
|
2024-05-15 23:24:26 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
message = 'Feedback sent successfully. Thank you!';
|
|
|
|
} catch (e) {
|
|
|
|
message = 'Error when sending feedback: ${e.toString()}';
|
|
|
|
error = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Show a snackBar with the result
|
|
|
|
if (context.mounted) {
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
|
|
|
content: Text(message),
|
|
|
|
backgroundColor: error ? Colors.red : Colors.green,
|
|
|
|
));
|
|
|
|
Navigator.pop(context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|