列表项之一快速刷新时,Listview不会更新

时间:2020-09-11 06:47:31

标签: flutter listview

我在具有3个字段的listview中使用自定义模型的列表,并且当任何索引上的字段之一在list中更改时,即使我使用setState()方法更改项目,listview也不会反映这些更改。

那该怎么办?

我可以手动刷新列表视图吗?

在下面的代码中,在 onSelectedItemChanged 方法中,我setState更改了 listQuestion 中传递的索引中的答案之一,并且确实更改了我检查过的列表中的值进行调试,但列表视图不会刷新。

选择器对话框:

showPicker(BuildContext context, int index) {
    return showDialog<void>(
      context: context,
      builder: (BuildContext context) {
        return StatefulBuilder(builder: (context, setState) {
          return AlertDialog(
            elevation: 80,
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.all(Radius.circular(25))),
            content: new Container(
              height: 170,
              width: MediaQuery.of(context).size.width,
              alignment: Alignment.center,
              decoration: BoxDecoration(),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Container(
                    height: 120,
                    child: CupertinoPicker(
                        onSelectedItemChanged: (value) {
                          setState(() {
                            listQuestions[index].answer =
                                type_string[value].toString();
                          });
                          _listViewKey.currentState.reassemble();
                        },
                        itemExtent: 34.0,
                        diameterRatio: 1,
                        scrollController:
                        FixedExtentScrollController(initialItem: 2),
                        children: type),
                  ),
                  SizedBox(height: 20),
                  GestureDetector(
                    child: Text(
                      "Ok",
                      style: TextStyle(
                          color: MyColors.orange,
                          fontSize: 17,
                          fontWeight: FontWeight.w900),
                    ),
                    onTap: () {
                      Navigator.of(context).pop();
                    },
                  )
                ],
              ),
            ),
          );
        });
      },
    );
  }

另一个支架中的ListView:

return Scaffold(
      key: _listViewKey,
        body: SafeArea(
            child: new ListView.builder(
              itemCount: listQuestions.length,
              itemBuilder: (BuildContext ctxt, int Index) {
                return new Container(
                  margin: EdgeInsets.fromLTRB(0, 0, 0, 10),
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(15),
                  ),
                  child: IntrinsicHeight(
                    child: Row(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        Container(
                          width: displayWidth(context) * 0.08,
                          alignment: Alignment.topRight,
                          child: Text(
                            listQuestions[Index].id.toString(),
                            style: TextStyle(
                                color: MyColors.orange,
                                fontSize: displayWidth(context) * 0.07,
                                fontWeight: FontWeight.w900),
                          ),
                        ),
                        SizedBox(width: displayWidth(context) * 0.02),
                        Expanded(
                            child: Column(
                          children: [
                            Padding(
                              padding: EdgeInsets.fromLTRB(
                                displayWidth(context) * 0.015,
                                displayWidth(context) * 0.0,
                                displayWidth(context) * 0.0,
                                displayWidth(context) * 0.0,
                              ),
                              child: Text(
                                listQuestions[Index].question.toString(),
                                style: TextStyle(
                                    color: MyColors.white,
                                    fontSize: displayWidth(context) * 0.04,
                                    fontWeight: FontWeight.w900),
                              ),
                            ),
                            Index > 7
                                ? Container(
                                    width: MediaQuery.of(context).size.width,
                                    decoration: BoxDecoration(
                                        color: MyColors.white,
                                        borderRadius: BorderRadius.all(
                                            Radius.circular(20))),
                                    child: TextFormField(
                                      maxLines: 3,
                                      decoration: InputDecoration(
                                          hintText: "Add your comments",
                                          border: InputBorder.none,
                                          focusedBorder: InputBorder.none,
                                          enabledBorder: InputBorder.none,
                                          disabledBorder: InputBorder.none,
                                          errorBorder: InputBorder.none,
                                          focusedErrorBorder: InputBorder.none),
                                    ))
                                : Container(
                                    width: MediaQuery.of(context).size.width,
                                    decoration: BoxDecoration(
                                        color: MyColors.white,
                                        borderRadius: BorderRadius.all(
                                            Radius.circular(20))),
                                    child: GestureDetector(
                                      child: Text(listQuestions[Index]
                                          .answer
                                          .toString()),
                                      onTap: () {
                                        showPicker(context, Index);
                                      },
                                    ),
                                  )
                          ],
                        ))
                      ],
                    ),
                  ),
                );
              })
))

0 个答案:

没有答案