[英]How to set value on selected items with checkboxListTile
What is the correct way to Setstate of checkboxListTile to get both toggle button and added value to it?将 checkboxListTile 的 Setstate 设置为同时获得切换按钮和附加值的正确方法是什么?
What I want is when you check the box, You see the check button.我想要的是当您选中该框时,您会看到选中按钮。 then the value added into the base price.然后是添加到基本价格中的价值。 Please kindly help.请帮助。
CheckboxListTile(
activeColor: Colors.blue,
dense: true,
//font change
title: new Text(
listTopping[i].price.toStringAsFixed(0) +
' บาท',
style: TextStyle(
fontSize: 25,
fontWeight: FontWeight.bold,
),
),
value: listTopping[i].isCheck,
secondary: Container(
height: 50,
width: 300,
child: Text(
listTopping[i].topping,
style: TextStyle(
fontSize: 25,
fontWeight: FontWeight.bold,
),
),
),
onChanged: (bool? val) {
itemChange(val!, i);
},
),
Here is the setstate that I believe is wrong...这是我认为错误的 setstate ......
void itemChange(bool val, int i) {
setState(() {
listTopping[i].isCheck = val;
});
}
}
Here is a small example of your code.这是您的代码的一个小示例。 Since you didn't provide the AddonTopping
class I created a simply version for myself.由于您没有提供AddonTopping
类,我为自己创建了一个简单的版本。 You can try it out and run this Widget in your MaterialApp
.您可以尝试一下并在MaterialApp
运行这个 Widget。 Everything should worked as expected.一切都应该按预期工作。
class AddonTopping {
AddonTopping({
required this.id,
required this.topping,
required this.isCheck,
required this.price,
});
final int id;
final String topping;
bool isCheck;
final int price;
}
class Americano extends StatefulWidget {
@override
_AmericanoState createState() => _AmericanoState();
}
class _AmericanoState extends State<Americano> {
List<AddonTopping> listTopping = [
AddonTopping(
id: 8,
topping: 'Whipcream',
price: 0,
isCheck: true,
),
AddonTopping(
id: 9,
topping: 'Javachip',
price: 30,
isCheck: false,
),
AddonTopping(
id: 10,
topping: 'SoyMilk',
price: 20,
isCheck: false,
),
AddonTopping(
id: 11,
topping: 'ExtraSyrup',
price: 30,
isCheck: false,
),
];
@override
Widget build(BuildContext context) {
return Column(
children: [
Text('SUM: ${calculatePrice()}'),
ListView.builder(
shrinkWrap: true,
itemCount: listTopping.length,
itemBuilder: (context, i) => CheckboxListTile(
activeColor: Colors.blue,
dense: true,
//font change
title: Text(
listTopping[i].price.toStringAsFixed(0),
style: TextStyle(
fontSize: 25,
fontWeight: FontWeight.bold,
),
),
value: listTopping[i].isCheck,
secondary: Container(
height: 50,
width: 300,
child: Text(
listTopping[i].topping,
style: TextStyle(
fontSize: 25,
fontWeight: FontWeight.bold,
),
),
),
onChanged: (bool? val) {
itemChange(val!, i);
},
),
),
],
);
}
void itemChange(bool val, int i) {
setState(() {
listTopping[i].isCheck = val;
});
}
double calculatePrice() {
if (listTopping.isNotEmpty) {
double _price = 0.0;
// Get those toppings that are chosen (`isCheck` is true)
final chosenTopping = listTopping.where((element) => element.isCheck);
// Calculate the sum
for (final AddonTopping item in chosenTopping) {
if (item.isCheck) {
_price += item.price;
}
}
return _price;
}
return 0.00;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.