0

I have some errors when trying to send data over to Firebase. I already put the necessary codes but i cant seem to make it work. This codes is for a feedback page. It will prompt a alertdialog when users already type their feedback in a textfield. In the alertdialog, their feedback input will be shown along with their rating of the app. Then there's two option - cancel (which goes back to the feedback page without sending data to firebase) and Yes (it sends the data to firebase and will show a snackbar that it is senet successfully)

I have already imported the files too.

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_core/firebase_core.dart';
import 'firebase_options.dart';

Is there anything wrong with the way i code?

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(
    options: DefaultFirebaseOptions.currentPlatform,
  );
  runApp(ReportScreen());
}

class ReportScreen extends StatefulWidget {
  const ReportScreen({Key? key}) : super(key: key);
  @override
  State<StatefulWidget> createState() {
    return Report();
  }
}

class Report extends State<ReportScreen> {
  bool abc = false;
  final improvementsController = TextEditingController();
  String improvementComment = '';
  final GlobalKey<FormState> _formkey = GlobalKey();
  double _rating = 0;

  Future<void> _popup(BuildContext context) {
    return showDialog<void>(
        context: context,
        builder: (BuildContext context) {
          return AlertDialog(
            key: _formkey,
            title: const Text('Submit Feedback?'),
            content: Text('FeedBack: $improvementComment \nRating: $_rating '),
            actions: <Widget>[
              TextButton(
                  onPressed: () => Navigator.pop(context),
                  child: Text(
                    'Cancel',
                    style: GoogleFonts.montserrat(
                        textStyle: const TextStyle(
                            fontWeight: FontWeight.bold, fontSize: 12)),
                  )),
              TextButton(
                  onPressed: () async {
                    String message;
                    final collection =
                        FirebaseFirestore.instance.collection('feedback');

                    await collection.doc().set({
                      'timestamp': FieldValue.serverTimestamp(),
                      'feedback': improvementsController.text,
                    });
                    message = "Feedback submitted successfully";
                    ScaffoldMessenger.of(context).showSnackBar(SnackBar(
                      content: Text(message),
                      duration: const Duration(seconds: 3),
                    ));
                    Navigator.pop(context);
                  },
                  child: Text(
                    'Yes',
                    style: GoogleFonts.montserrat(
                        textStyle: const TextStyle(
                            fontWeight: FontWeight.bold, fontSize: 12)),
                  ))
            ],
          );
        });
  }

  @override
  void dispose() {
    improvementsController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Builder(builder: (context) {
      return Stack(
        children: [
          const Backgroundimage(),
          Scaffold(
            backgroundColor: Colors.transparent,
            appBar: AppBar(
              elevation: 0.0,
              centerTitle: true,
              backgroundColor: Colors.transparent,
              title: Text(
                'Give Feedback s10194266d',
                style: GoogleFonts.kalam(
                    textStyle: const TextStyle(
                        fontWeight: FontWeight.bold,
                        color: Colors.white,
                        fontSize: 24)),
              ),
            ),
            body: Center(
              child: Column(
                children: [
                  Padding(
                    padding: const EdgeInsets.fromLTRB(0, 30, 0, 0),
                    child: Text(
                      'Are you satisfied with the app?',
                      style: GoogleFonts.montserrat(
                        textStyle: const TextStyle(
                            fontSize: 18,
                            color: Colors.white,
                            fontWeight: FontWeight.bold),
                      ),
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.fromLTRB(0, 20, 0, 20),
                    child: RatingBar.builder(
                      initialRating: 3,
                      itemCount: 5,
                      minRating: 1,
                      itemBuilder: (context, _context) {
                        switch (_context) {
                          case 0:
                            return const Icon(
                              Icons.sentiment_very_dissatisfied,
                              color: Colors.red,
                            );
                          case 1:
                            return const Icon(
                              Icons.sentiment_dissatisfied,
                              color: Colors.redAccent,
                            );
                          case 2:
                            return const Icon(
                              Icons.sentiment_neutral,
                              color: Colors.amber,
                            );
                          case 3:
                            return const Icon(
                              Icons.sentiment_satisfied,
                              color: Colors.lightGreen,
                            );
                          case 4:
                            return const Icon(
                              Icons.sentiment_very_satisfied,
                              color: Colors.green,
                            );
                          default:
                            return const Icon(
                              Icons.sentiment_very_satisfied,
                              color: Colors.green,
                            );
                        }
                      },
                      onRatingUpdate: (rating) {
                        _rating = rating;
                        setState(() {});
                      },
                    ),
                  ),
                  Text(
                    'Rating: $_rating',
                    style: GoogleFonts.montserrat(
                        fontSize: 13,
                        fontWeight: FontWeight.bold,
                        color: Colors.white),
                  ),
                  Padding(
                    padding: const EdgeInsets.fromLTRB(0, 20, 0, 20),
                    child: Text(
                      'What can the app improve on?',
                      style: GoogleFonts.montserrat(
                          textStyle: const TextStyle(
                              fontSize: 18,
                              color: Colors.white,
                              fontWeight: FontWeight.bold)),
                    ),
                  ),
                  SizedBox(
                    width: 350,
                    height: 100,
                    child: TextField(
                      maxLines: null,
                      style: GoogleFonts.montserrat(
                          textStyle: const TextStyle(color: Colors.white)),
                      controller: improvementsController,
                      decoration: InputDecoration(
                        contentPadding:
                            const EdgeInsets.symmetric(vertical: 40.0),
                        hintText: ' Enter your feedback here',
                        hintStyle: const TextStyle(color: Colors.white),
                        enabledBorder: OutlineInputBorder(
                          borderRadius: BorderRadius.circular(20.0),
                          borderSide: const BorderSide(color: Colors.white),
                        ),
                      ),
                      onChanged: (context) {
                        improvementComment = context;
                      },
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.all(30),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: <Widget>[
                        ElevatedButton(
                            style: ElevatedButton.styleFrom(
                                primary: Colors.black54,
                                shadowColor: Colors.white24,
                                padding: const EdgeInsets.all(10)),
                            onPressed: () {
                              _popup(context);
                              improvementsController.clear();
                            },
                            child: Text('Send \nFeedback',
                                textAlign: TextAlign.center,
                                style: GoogleFonts.montserrat(
                                  textStyle: const TextStyle(
                                      fontSize: 15,
                                      fontWeight: FontWeight.bold),
                                ))),
                        const SizedBox(width: 40),
                        ElevatedButton(
                            style: ElevatedButton.styleFrom(
                                padding: const EdgeInsets.all(10),
                                primary: Colors.black54,
                                shadowColor: Colors.white24),
                            onPressed: () {},
                            child: Text('See Previous\nFeedback',
                                textAlign: TextAlign.center,
                                style: GoogleFonts.montserrat(
                                  textStyle: const TextStyle(
                                      fontSize: 15,
                                      fontWeight: FontWeight.bold),
                                ))),
                      ],
                    ),
                  )
                ],
              ),
            ),
          ),
        ],
      );
    });
  }
}

0 Answers0