'flutter dropdownbutton issue with provider statemangement
hello guys as a beginner on flutter i'm having an issue with the dropdownbutton widget as shown on the joined GIF
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
Solution 1:[1]
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),
);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Benderradji Khireddine |