1

嗨,我正在使用 statefulwidget 开发一个评级应用程序。我不使用任何状态管理库。我希望每个用户都能够对一个项目进行评论。但我的问题是用户可以多次评论同一个项目,我希望他只能做一次。有人能帮我吗 ?这就是方法的样子:

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());
    }
4

0 回答 0