I am trying to display price based on the selected item from my picker , but my Text ui is not changing after onSelectedItemChanged Picker function is triggered even after using setState. The changes only reflect after i use the flutter hot reload.
//The price varaible is the variable that changes
int price = 0;
// This is the widget displaying the price variable based on selected item
Text(price,
style: TextStyle(
color: Colors.white,
fontFamily: 'Montserrat',
fontSize: 15.0));
//This is the picker widget
Widget buildPicker() {
return SizedBox(
height: 350,
child: StatefulBuilder(builder: (context, setState) {
return CupertinoPicker(
scrollController: scrollController,
itemExtent: 64,
selectionOverlay: CupertinoPickerDefaultSelectionOverlay(
background: CupertinoColors.activeBlue.withOpacity(0.2),
),
children: List.generate(productController.data.length, (index) {
final isSelected = this.index == index;
final item = productController.data[index];
final color =
isSelected ? CupertinoColors.activeBlue : CupertinoColors.black;
return Center(
child: Text(
item.size!,
style: TextStyle(color: color, fontSize: 32),
),
);
}),
onSelectedItemChanged: (index) {
setState(() {
this.index = index;
final item = productController.data[index];
price= item.price!;
print(price);
});
},
);
}),
);
}
// The text widget displaying the price does not change unless i use the flutter hot reload before the changes reflect. Please what am i doing wrong here?