1

Hi I'm developing a rating application with statefulwidget. I don't use any state management libraries. I want each user to be able to do a review on an item. But my problem is that the user can reviews the same item several times, and I would like him to be able to do it only once. Can someone help me ? this is how the method looks like :

void _showReviewSheet() async {
      showModalBottomSheet(
        context: context,
        builder: (context) {
          return Container(
            color: Color(0xFF737373),
            child: Container(
              padding: EdgeInsets.all(20.0),
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.only(
                  topLeft: Radius.circular(10.0),
                  topRight: Radius.circular(10.0),
                ),
              ),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.stretch,
                children: [
                  Expanded(
                    flex: 1,
                    child: TextFormField(
                      controller: _noteController,
                      keyboardType: TextInputType.number,
                      inputFormatters: <TextInputFormatter>[
                        FilteringTextInputFormatter.digitsOnly
                      ],
                      decoration: InputDecoration(
                        contentPadding:
                            EdgeInsets.only(left: 10.0, top: 20.0, right: 10.0),
                        border: OutlineInputBorder(
                          borderSide: BorderSide(),
                        ),
                        hintText: "give a note ",
                        hintStyle:
                            TextStyle(fontSize: 16.0, color: Colors.black),
                      ),
                    ),
                  ),
                  Expanded(
                    flex: 3,
                    child: TextField(
                      maxLines: 13,
                      controller: _bodyController,
                      decoration: InputDecoration(
                        contentPadding:
                            EdgeInsets.only(left: 10.0, top: 20.0, right: 10.0),
                        border: OutlineInputBorder(
                          borderSide: BorderSide(),
                        ),
                        hintText: "Add your Review",
                        hintStyle:
                            TextStyle(fontSize: 16.0, color: Colors.black),
                      ),
                    ),
                  ),
                  InkWell(
                    onTap: () async {
                      var uid = await storage.read(key: "userId");
                      if (int.parse(_noteController.text) >= 20) {
                        displayDialog(context, "Error",
                            "the Note should be inferior than 20 ");
                      } else if (_noteController.text.isEmpty) {
                        displayDialog(
                            context, "Error", "you should give a note");
                      } else if (_bodyController.text.isEmpty) {
                        displayDialog(context, "Error",
                            "you should fill the content box");
                      } else {
                        _reviewService
                            .addReview(_bodyController.text, uid,
                                int.parse(_noteController.text), itemId)
                            .then(
                              (_) => Navigator.of(context).pop(),
                            )
                            .then((_) => _noteController.text = "")
                            .then((_) => _bodyController.text = "");
                      }
                    },
                    child: Container(
                      margin: EdgeInsets.only(top: 20.0),
                      padding: EdgeInsets.all(20.0),
                      decoration: BoxDecoration(
                        color: Colors.deepPurple,
                        borderRadius: BorderRadius.circular(10.0),
                      ),
                      child: Center(
                        child: Text(
                          "send",
                          style: TextStyle(
                            fontSize: 18.0,
                            fontWeight: FontWeight.bold,
                            color: Colors.white,
                          ),
                        ),
                      ),
                    ),
                  )
                ],
              ),
            ),
          );
        },
      ).then((_) => _loadReviews()).then((_) => _loadItem());
    }
  • Try to save rating requests count and check it before showing rating. If user reaches it's limit to view rating - show previous rating value or warning dialog. – fartem Jan 26 '21 at 14:51

0 Answers0