I am trying to reduce a products(smartphone) price based on user dropdown input & pass the price to next screen. My code has no error & runs perfectly but the problem is, it reduces price based on only one switch statement (the initial one selected globally for dropdown value) because setState
method is working only on changing dropdown value displayed on screen, not in the switch statement containing function. Can someone help me on how to solve this. I tried many things, but nothing helped. Below is my code (It's long, so I skipped widget building parts & designing parts here).
Value has been defined like this
double price = 18000;
String bdropDownValue = 'Less than 6 hours'; //initialvalue
final List<String> bItems = [
'Less than 6 hours',
'Less than 12 hours',
'Less than 24 hours'],
DropdownButtonFormField
DropdownButtonFormField(value: bdropDownValue,
items: bItems.map((bItems) {
return DropdownMenuItem(
value: bItems,
child: Text(bItems),
);
}).toList(),
onChanged: (String? value) {
setState(() {
bdropDownValue = value!;
});
},
),
/// button used to calculate next
TextButton(
onPressed: () {
calculator(bdropDownValue);
},
child: Text(
"confirm",
),
I am using this method to calculate
void calculator(String bdropDownValue) {
switch (bdropDownValue) {
case 'Less than 6 hours':
price = price - 3000;
break;
case 'Less than 12 hours':
price = price - 1500;
break;
case 'Less than 24 hours':
price = price - 1000;
break;
}
And using Navigator
like this inside calculator method
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => priceScreen(
price: price,
)));
}
}