From 5036122bce911caacc7119f790332283891d4b3b Mon Sep 17 00:00:00 2001 From: venbaittech Date: Mon, 4 Nov 2024 17:41:37 +0530 Subject: [PATCH] feedback funtionality --- .../routes/drawer_routes/feedback_route.dart | 186 +++++++++++++----- 1 file changed, 138 insertions(+), 48 deletions(-) diff --git a/lib/presentation/routes/drawer_routes/feedback_route.dart b/lib/presentation/routes/drawer_routes/feedback_route.dart index f60bd38b..110c7007 100644 --- a/lib/presentation/routes/drawer_routes/feedback_route.dart +++ b/lib/presentation/routes/drawer_routes/feedback_route.dart @@ -76,23 +76,13 @@ // } // } -import 'package:excel/excel.dart'; import 'package:flutter/material.dart'; import 'package:flutter_rating_bar/flutter_rating_bar.dart'; +import 'package:pocketbase/pocketbase.dart'; +import 'package:uae_stat/domain/use_cases/language.dart'; import 'package:uae_stat/presentation/components/my_drawer.dart'; -// class FeedbackPage extends StatelessWidget { -// const FeedbackPage({super.key}); - -// @override -// Widget build(BuildContext context) { -// return const Scaffold( -// body: Center( -// child: Text('Feedback'), -// ), -// ); -// } -// } +import '../../components/lang_toggle.dart'; class FeedbackForm extends StatefulWidget { const FeedbackForm({super.key}); @@ -102,17 +92,45 @@ class FeedbackForm extends StatefulWidget { } class _FeedbackFormState extends State { + final _pb = + PocketBase('http://127.0.0.1:8090'); // Initialize PocketBase client + + final TextEditingController _feedbackController = TextEditingController(); + double _easeOfUseRating = 0; double _qualityRating = 0; double _designRating = 0; double _redundancyRating = 0; + int? _selectedEmojiIndex; + + // Map of emoji labels and values for sending to PocketBase + final List> _emojiOptions = [ + { + "icon": Icons.sentiment_very_dissatisfied, + "label": "Terrible", + "value": 1 + }, + {"icon": Icons.sentiment_dissatisfied, "label": "Bad", "value": 2}, + {"icon": Icons.sentiment_neutral, "label": "Okay", "value": 3}, + {"icon": Icons.sentiment_satisfied, "label": "Good", "value": 4}, + {"icon": Icons.sentiment_very_satisfied, "label": "Amazing", "value": 5}, + ]; @override Widget build(BuildContext context) { return Scaffold( drawer: const MyDrawer(), appBar: AppBar( - title: const Text('Feedback Form'), + backgroundColor: const Color(0xFFf8f9ff), + title: Text( + context.translate('Feedback Form', 'نموذج الملاحظات'), + ), + actions: const [ + Align( + alignment: AlignmentDirectional.topEnd, + child: LangToggle(), + ), + ], ), body: Padding( padding: const EdgeInsets.all(16.0), @@ -120,60 +138,71 @@ class _FeedbackFormState extends State { child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ - const Text( - 'Do you have a suggestion or had any problem? Let us know.', - style: TextStyle(fontSize: 18), + Text( + context.translate( + 'Do you have a suggestion or had any problem? Let us know.', + 'هل لديك اقتراح أو واجهت أي مشكلة؟ اسمحوا لنا أن نعرف.'), + style: const TextStyle(fontSize: 18), ), const SizedBox(height: 20), - const Text( - 'How was your experience with us today?', - style: TextStyle(fontSize: 16), + Text( + context.translate('How was your experience with us today?', + 'كيف كانت تجربتك معنا اليوم؟'), + style: const TextStyle(fontSize: 16), ), const SizedBox(height: 10), Row( mainAxisAlignment: MainAxisAlignment.spaceEvenly, - children: [ - _buildEmojiButton( - Icons.sentiment_very_dissatisfied, - 'Terrible', - ), - _buildEmojiButton(Icons.sentiment_dissatisfied, 'Bad'), - _buildEmojiButton(Icons.sentiment_neutral, 'Okay'), - _buildEmojiButton(Icons.sentiment_satisfied, 'Good'), - _buildEmojiButton(Icons.sentiment_very_satisfied, 'Amazing'), - ], + children: List.generate(_emojiOptions.length, (index) { + return _buildEmojiButton( + icon: _emojiOptions[index]["icon"], + label: _emojiOptions[index]["label"], + index: index, + ); + }), ), const SizedBox(height: 30), - const Text( - 'How good did we do in these aspects?', - style: TextStyle(fontSize: 16), + Text( + context.translate('How good did we do in these aspects?', + 'ما مدى جودة ما قمنا به في هذه الجوانب؟'), + style: const TextStyle(fontSize: 16), ), const SizedBox(height: 20), - _buildRatingRow('Ease of use', _easeOfUseRating, (rating) { + _buildRatingRow( + context.translate('Ease of use', 'سهولة الاستخدام'), + _easeOfUseRating, (rating) { setState(() { _easeOfUseRating = rating; }); }), - _buildRatingRow('Quality', _qualityRating, (rating) { + _buildRatingRow( + context.translate('Quality', 'جودة'), _qualityRating, + (rating) { setState(() { _qualityRating = rating; }); }), - _buildRatingRow('Design', _designRating, (rating) { + _buildRatingRow( + context.translate('Design', 'تصميم'), _designRating, + (rating) { setState(() { _designRating = rating; }); }), - _buildRatingRow('Redundant', _redundancyRating, (rating) { + _buildRatingRow( + context.translate('Redundant', 'متكرر'), _redundancyRating, + (rating) { setState(() { _redundancyRating = rating; }); }), const SizedBox(height: 20), - const TextField( + TextField( + controller: _feedbackController, decoration: InputDecoration( - hintText: 'Tell us how we can improve', - border: OutlineInputBorder( + hintText: context.translate( + 'Tell us how we can improve', 'أخبرنا كيف يمكننا تحسين'), + border: const OutlineInputBorder( borderSide: BorderSide(color: Colors.blueAccent)), ), maxLines: 5, @@ -181,10 +210,10 @@ class _FeedbackFormState extends State { const SizedBox(height: 20), Center( child: ElevatedButton( - onPressed: () { - // Handle the submit action - }, - child: const Text('Submit'), + onPressed: _submitFeedback, + child: Text( + context.translate('Submit', 'يُقدِّم'), + ), ), ), ], @@ -194,19 +223,30 @@ class _FeedbackFormState extends State { ); } - Widget _buildEmojiButton(IconData icon, String label) { + Widget _buildEmojiButton({ + required IconData icon, + required String label, + required int index, + }) { + bool isSelected = _selectedEmojiIndex == index; + return Column( children: [ IconButton( icon: Icon(icon, size: 40), - color: Colors.amber, + color: isSelected ? Colors.green : Colors.amber, onPressed: () { - // Handle the emoji rating + setState(() { + _selectedEmojiIndex = index; + }); }, ), Text( label, - style: TextStyle(fontWeight: FontWeight.bold), + style: TextStyle( + fontWeight: FontWeight.bold, + color: isSelected ? Colors.green : Colors.black, + ), ), ], ); @@ -237,4 +277,54 @@ class _FeedbackFormState extends State { ], ); } + + Future _submitFeedback() async { + if (_selectedEmojiIndex == null) { + ScaffoldMessenger.of(context).showSnackBar( + const SnackBar(content: Text("Please select your experience rating.")), + ); + return; + } + + final feedbackData = { + "ease_of_use": _easeOfUseRating, + "quality": _qualityRating, + "design": _designRating, + "redundancy": _redundancyRating, + "emoji_rating": _emojiOptions[_selectedEmojiIndex!] + ["label"], // Emoji rating + "feedback": _feedbackController.text, + }; + // print(feedbackData); + // return; + try { + final adminAuth = await _pb.admins + .authWithPassword('surendar.m@venbainfotech.com', 'Venba@123456'); + final adminToken = adminAuth.token; + await _pb + .collection('feedback') + .create(body: feedbackData, headers: {'Authorization': adminToken}); + ScaffoldMessenger.of(context).showSnackBar( + const SnackBar(content: Text("Feedback submitted successfully!")), + ); + setState(() { + _easeOfUseRating = 0; + _qualityRating = 0; + _designRating = 0; + _redundancyRating = 0; + _selectedEmojiIndex = null; + _feedbackController.clear(); + }); + } catch (e) { + ScaffoldMessenger.of(context).showSnackBar( + SnackBar(content: Text("Failed to submit feedback: $e")), + ); + } + } + + @override + void dispose() { + _feedbackController.dispose(); + super.dispose(); + } }