flutter - 如何计算动态小部件 totalPrice 变量值
问题描述
class _ChildDataState extends State<ChildData> {
int totalPrice = 0;
List totalHarga = List();
@override
Widget build(BuildContext context) {
return Stack(
children: [
Container(
child: ListView.builder(
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
itemCount: widget.items.length,
itemBuilder: (context, i) {
int price = int.parse(widget.items[i].price.split('.').first);
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Container(
height: 100,
width: 100,
decoration: BoxDecoration(
shape: BoxShape.circle,
image: DecorationImage(
fit: BoxFit.cover,
image: NetworkImage(
widget.items[i].image ?? '',
),
),
),
),
SizedBox(width: 10),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
widget.items[i].name,
style: blackTextFont.copyWith(
fontWeight: FontWeight.bold),
),
SizedBox(height: 5),
Text(
"Rp. ${widget.items[i].price.split('.').first} / ${widget.items[i].unit}",
style: blackNumberFont),
SizedBox(height: 8),
Text(widget.items[i].description),
SizedBox(height: 8),
SizedBox(height: 8),
widget.items[i].counter != 0
? Row(
mainAxisAlignment:
MainAxisAlignment.spaceEvenly,
children: [
Container(
decoration: BoxDecoration(
borderRadius:
BorderRadius.circular(10),
color: mainColor,
),
width: 30,
height: 30,
child: IconButton(
icon: Icon(
Icons.remove,
size: 15,
color: Colors.white,
),
onPressed: () {
setState(
() {
widget.items[i].counter--;
totalPrice -= price;
widget.onTotalChange(totalPrice.toString());
},
);
},
),
),
Text(widget.items[i].counter.toString()),
Container(
decoration: BoxDecoration(
borderRadius:
BorderRadius.circular(10),
color: mainColor,
),
width: 30,
height: 30,
child: IconButton(
icon: Icon(
Icons.add,
size: 15,
color: Colors.white,
),
onPressed: () {
setState(() {
widget.items[i].counter++;
totalPrice += price;
widget.onTotalChange(totalPrice.toString());
});
},
),
),
],
)
: Center(
child: GestureDetector(
child: Container(
decoration: BoxDecoration(
borderRadius:
BorderRadius.circular(4),
color: mainColor,
),
width: double.infinity,
height: 30,
child: Center(
child: Text("ADD",
style: whiteTextFont),
)),
onTap: () {
setState(() {
widget.items[i].counter++;
totalPrice += price;
widget.onTotalChange(totalPrice.toString());
});
},
),
),
],
),
)
],
),
],
);
},
),
),
],
);
我有一个产品数据和产品子数据,当我想显示产品数据和产品子数据时,我必须创建一个新的小部件并将其放在产品数据“ListView.builder”中。我已经解决了计算产品子总价格的问题,现在我想从不同的小部件中计算整个总价格。
解决方案
您可以执行以下操作。我从我的项目中提供此代码以供您理解。你可以检查一次。
double getPrice() {
double price = 0;
AppData.cartList.forEach((x) {
price += x.price * x.widget.items[i];
});
return price;
}
为了更好地理解,您可以检查想要传递的总价格在底部导航栏内颤振
推荐阅读
- html - x-frame-options 标头集,但仍然可以嵌入 iframe?
- javascript - 在工具提示弹出延迟窗口期间工具提示打开变为假时工具提示未关闭
- r - 在一次调用中管道多步数据整理/转换
- python - 为什么每次运行语音助手时都会出现“预期”错误?
- python - discord.py:机器人不识别作者
- r - 如何从矩阵中选择行。R
- python - VS 代码中的相对路径不一致
- reactjs - 如何为 React 单页应用程序实现 OAuth2 授权代码授权?
- python - 在 Python 中生成 OAuth2 令牌
- react-bootstrap-typeahead - React-Bootstrap-Typeahead:在将选择设置为框之前捕获时刻(以允许有条件的拒绝)