你好,我正在进行多项选择。
List<ContactModel> contacts = [
ContactModel("Tüm ilanları göster", false),
ContactModel("En çok puan alan", false),
ContactModel("En çok teklif alan", false),
ContactModel("En az teklif alan", false),
ContactModel("Tarihe göre (Önce en yeni ilan)", false),
ContactModel("Tarihe göre (Önce en eski ilan)", false),
];
List<ContactModel> selectedContact = [];
这些都是我的选择。
pop() {
showDialog(
context: context,
builder: (context) {
Widget content = wici();
return StatefulBuilder(
builder: (context, SetState) {
return AlertDialog(
title: Text("Title of Dialog"),
content: content,
);
},
);
},
);
}
Widget wici() {
return Container(
height: 400,
width: 40,
child: ListView.builder(
itemCount: contacts.length,
itemBuilder: (BuildContext context, int index) {
return contactItem(
contacts[index].name, contacts[index].isSelected, index);
}),
);
}
Widget contactItem(String deger, bool isSelected, int index) {
return InkWell(
child: Container(
height: 40,
child: Text(deger),
color: isSelected ? purple : grey,
),
onTap: () {
setState(() {
contacts[index].isSelected = !contacts[index].isSelected;
if (contacts[index].isSelected == true) {
selectedContact.add(ContactModel(deger, true));
} else if (contacts[index].isSelected == false) {
selectedContact
.removeWhere((element) => element.name == contacts[index].name);
}
});
},
);
}
当我使用pop((函数时。然后我选择了一些选项,但一切都没有改变。要查看更改,我需要进行热重新加载。如何更新警报对话框中的列表视图。感谢
有状态小部件的setState和statefulbuilder的setState是两个不同的状态设置器。。解决这个问题的一个选项是将有状态构建器的状态设置器传递给小部件,并像下面的一样调用该方法
pop() {
showDialog(
context: context,
builder: (context) {
return StatefulBuilder(
builder: (context, setState) {
return AlertDialog(
title: Text("Title of Dialog"),
content: wici(setState),
);
},
);
},
);
}
Widget wici(StateSetter setState) {
return Container(
height: 400,
width: 40,
child: ListView.builder(
itemCount: contacts.length,
itemBuilder: (BuildContext context, int index) {
return contactItem(
contacts[index].name, contacts[index].isSelected, index, setState);
}),
);
}
Widget contactItem(String deger, bool isSelected, int index, StateSetter setState) {
return InkWell(
child: Container(
height: 40,
child: Text(deger),
color: isSelected ? purple : grey,
),
onTap: () {
contacts[index].isSelected = !contacts[index].isSelected;
if (contacts[index].isSelected == true) {
selectedContact.add(ContactModel(deger, true));
} else if (contacts[index].isSelected == false) {
selectedContact
.removeWhere((element) => element.name == contacts[index].name);
}
setState((){});
},
);
}