flutter dropdownbutton issue with provider statemangement

94 views Asked by At

hello guys as a beginner on flutter i'm having an issue with the dropdownbutton widget as shown on the joined GIF

enter image description here

on the backend everything seems to work normally every row n the list has it Answer properly but on the screen the values changes for the whole screen not row by row that's the source code for the ChangeNotifier() :

class ChoiceHandler extends ChangeNotifier {
  final List<String> _dropdownElements = ['Not Done', 'Partially Done', 'Done'];
  List<String> get dropdownElement => _dropdownElements;
  late String _selectedItemValue;
  String get selected => _selectedItemValue;

  selectedValues(String s) {
    _selectedItemValue = s;
    notifyListeners();
  }
}

and under listView here is the code for the dropdownButton :

Expanded(
            child: ListView.builder(
              itemCount: propositions.length,
              itemExtent: 50.0,
              itemBuilder: (BuildContext context, int index) {
                return ListTile(
                    title: Text(propositions[index]),
                    trailing: Consumer<ChoiceHandler>(
                      builder: (_, provider, __) {
                        return DropdownButton<String>(
                          value: dropdownValue,
                          onChanged: (newValue) {
                            provider.selectedValues(newValue as String);
                            dropdownValue = newValue;
                            print((propositions[index]) + "  " + newValue); // here i can the results when i'm changing values
                          },
                          items: provider.dropdownElement
                              .map<DropdownMenuItem<String>>((String value) {
                            return DropdownMenuItem<String>(
                              value: value,
                              child: Text(value),
                            );
                          }).toList(),
                        );
                      },
                    ) //_dropdown(index),
                    );

thanks in advance

1

There are 1 answers

0
Benderradji Khireddine On

i've found the issue i've misplaced the declaration of a veriable ,th variable "dropdownValue" was declared above as a global variable , and then i've changed it place to where it's shown on the source code :

child: ListView.builder(
              itemCount: propositions.length,
              itemExtent: 50.0,
              itemBuilder: (BuildContext context, int index) {
              String dropdownValue = "Not Done"; // <==== i declared it here
                return ListTile(
                    title: Text(propositions[index]),
                    trailing: Consumer<ChoiceHandler>(
                      builder: (_, provider, __) {
                        return DropdownButton<String>(
                          value: dropdownValue,
                          onChanged: (newValue) {
                            provider.selectedValues(newValue as String);
                            dropdownValue = newValue;
                            print((propositions[index]) + "  " + newValue); 
                          },
                          items: provider.dropdownElement
                              .map<DropdownMenuItem<String>>((String value) {
                            return DropdownMenuItem<String>(
                              value: value,
                              child: Text(value),
                            );
                          }).toList(),
                        );
                      },
                    ) //_dropdown(index),
                    );