我正在尝试使用下面的代码显示一个带有CheckBoxes ListView的AlertDialog:

showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text(' ', textAlign: TextAlign.right,),
content: Directionality(
textDirection: TextDirection.rtl,
child: Container(
            height: 300.0,
            width: 300.0,
            child: new ListView.builder(
                   shrinkWrap: true,
                   itemCount: dropEntitiesList.length,
                   itemBuilder: (BuildContext context, int index) {
                   return new Row(
                          children: [
                                    new Checkbox(
                                    value: globals.entitiesFilter.contains(dropEntitiesList[index]),
                                    onChanged: (bool newValue) {
                                    setState(() {
                                                        dropEntitiesList[index].isClicked = !dropEntitiesList[index].isClicked;
                                                        if (dropEntitiesList[index].isClicked){
                                                          globals.entitiesFilter.add(dropEntitiesList[index].name);
                                                        }else{
                                                          globals.entitiesFilter.remove(dropEntitiesList[index].name);
                                                        }
                                                      });
                                                      print(globals.entitiesFilter);
                                                    }),
                                                new Text(
                                                  dropEntitiesList[index].name,
                                                  style: TextStyle(fontSize: 16.0),
                                                ),
                                              ],
                                            );
                                          }),
                                    ),
                                  ),
                                    actions: <Widget>[
                                      new FlatButton(
                                          child: new Text('انتهيت'),
                                          onPressed: () {
                                            Navigator.of(context).pop(true);
                                          }),
                                      new FlatButton(
                                        child: new Text('إلغاء'),
                                        onPressed: () {
                                          Navigator.of(context).pop(false);
                                        },
                                      )
                                    ],
                                  );

onChanged的newValue参数始终为true。要查看选中的CheckBox,我需要关闭对话框,然后再次打开它,单击不会立即更改。

我该如何解决?

最佳答案

编辑:
作为showDialog()状态的文档,如果对话框需要更新,则需要使用StatefulBuilder。因此,将您的Directionality小部件包装在StatefulBuilder中:

showDialog(
  context: context,
  builder: (BuildContext context) {
    return AlertDialog(
      title: Text(' ', textAlign: TextAlign.right,),
      content: StatefulBuilder(builder: (BuildContext context, StateSetter stateUpdater) {
        return Directionality(..rest of code
      }

在onChange()回调内部,然后使用StateSetter参数更新状态:
stateUpdater(() {
   dropEntitiesList[index].isClicked = !dropEntitiesList[index].isClicked;
   // rest of the code
});

您正在混合一些用于状态的列表。例如,您根据entityFilter列表中存在的行的实体来设置CheckBox的值,但这将始终为false,因为在onChanged()方法中,您仅使用dropEntitiesList [index]中的名称来更新entityFilter列表。实体本身。您可以使用以下方法解决此问题:
value: globals.entitiesFilter.contains(dropEntitiesList[index].name),

(正如我所说的,您只将名称保存在onChanged()方法中)。

关于checkbox - CheckBox值在onChanged回调中始终为true,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54089120/

10-10 17:55