Home > Mobile >  I want to make setState ListView alert dialog
I want to make setState ListView alert dialog

Time:08-13

Hello I am working on a multiple choice select.

  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 = [];

those are mine selecting options.

 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);
          }
        });
      },
    );
  }

When I use pop() func coming. Then I am choosing some options but nothing is changing. To see the change I need to make hot reload. How can I update a listview which is in an alert dialog. Thanks

CodePudding user response:

The setState of stateful widget and setState of statefulbuilder are 2 different state setters here.. One option to fix this is to pass the state setter of stateful builder to the widget and call that method like the following

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((){});
      },
    );
  }
  • Related