So I created my own widget that uses the DropDownButton class and it used to work, it would update in a way where after you select a value, it would display the selected value. I refactored my code to involve the whole model-view/viewmodel technique and now after you select a value from the list it still just displays the default first value of the list after you click it.
Here is the code for my DropDownWidget class:
class DropDownWidget extends StatefulWidget {
//const DropDownWidget({Key? key}) : super(key: key);
List<String> dropdownOptions = [];
String? dropdownValue;
DropDownWidget(List<String> options) {
dropdownOptions = options;
dropdownValue = dropdownOptions.isNotEmpty ? dropdownOptions[0] : null;
}
@override
State<DropDownWidget> createState() => _DropDownWidgetState();
}
class _DropDownWidgetState extends State<DropDownWidget>
with TickerProviderStateMixin {
late AnimationController controller;
@override
void initState() {
controller = AnimationController(
vsync: this,
duration: const Duration(seconds: 1),
)..addListener(() {
setState(() {});
});
controller.repeat(reverse: true);
super.initState();
}
@override
void dispose() {
controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
var dropdownOptions = widget.dropdownOptions;
var dropdownValue = widget.dropdownValue;
if (dropdownOptions.isEmpty) {
return LinearProgressIndicator(
value: controller.value, //0.75,
color: const Color(0xFFFFA663),
backgroundColor: const Color(0x3498DBA5),
);
}
return DropdownButton<String>(
value: dropdownValue, //?? dropdownOptions[0],
icon: const Icon(
Icons.agriculture_rounded,
color: Color(0xFF773608), //Color(0xFF2E7D32), //Colors.green,
),
elevation: 16,
underline: Container(
height: 2,
color: vo.shade200,
),
onChanged: (String? newValue) {
setState(() {
dropdownValue = newValue!;
print(newValue);
});
},
items: dropdownOptions.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
);
}
}
Let me know if any more code is needed.