i'm crating an app where the user can make different choices from a list of questions , just the dropdownbutton won't keep the chosen answers after the user scrolls down to the other questions check the GIF
i' working with the Provider state management system , here is the code :
import 'package:flutter/material.dart';
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 here is the dropdownbutton widget code :
Expanded(
child: ListView.builder(
itemCount: propositions.length,
itemExtent: 50.0,
itemBuilder: (BuildContext context, index) {
String dropdownValue = "Not Done";
return ListTile(
title: Text(propositions[index]),
trailing: Consumer<ChoiceHandler>(
builder: (_, provider, __) {
return DropdownButton<String>(
value: dropdownValue,
onChanged: (newValue) {
dropdownValue = newValue as String;
Provider.of<ChoiceHandler>(context, listen: false)
.selectedValues(dropdownValue);
print((propositions[index]) + " " + newValue);
dropdown_answer.add(dropdownValue);
},
items: provider.dropdownElement
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
);
},
) //_dropdown(index),
);
},
),