特定产品的颤动增量计数器? [英] Flutter increment counter for a specific product?
问题描述
我正在开发一个包含产品的应用程序,我需要做的是能够增加并获取要存储的增加产品的密钥,目前它们正在全部增加。
I'm doing an application in flutter that contains products, what I need to do is to be able to increase and obtain the key of the increased product to be stored, currently they are increasing all.
我留下图像和代码,谢谢您的帮助
I leave the image and my code, thanks for the help
Widget _buildSearchResults() {
return new ListView.builder(
itemCount: _productSearchResult.length,
itemBuilder: (context, i) {
return new Card(
child: Column(
children: <Widget>[
ExpansionTile(
leading: new CircleAvatar(
backgroundImage: new NetworkImage(_productSearchResult[i].image_url),
),
title: new Text(
'${_productSearchResult[i].name} \nPrecio: Q${_productSearchResult[i].price} / Medida: ${_productSearchResult[i].unit } ',
),
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
new Container(
child: new IconButton(
icon: new Icon(Icons.remove),
highlightColor: Colors.green,
onPressed: (){
_removeProduct();
},
),
),
new Container(
child: new Text('$_counter',
style: Theme.of(context).textTheme.display1,),
),
new Container(
child: new IconButton(
icon: new Icon(Icons.add),
highlightColor: Colors.green,
onPressed: (){
_addProduct();
},
),
),
new Container(
padding: new EdgeInsets.all(10.0),
width: 100.0,
child: new RaisedButton(
padding: const EdgeInsets.all(8.0),
textColor: Colors.white,
color: Colors.green,
onPressed: _addNumbers,
child: new Text("Agregar"),
),
),
]
),
]
),
],
)
);
},
);
}
_removeProduct() {
setState(() {
if (_counter > 0) {
_counter--;
}
});
}
_addProduct() {
setState(() {
_counter++;
});
}
推荐答案
您的所有产品都有一个计数器,这就是为什么它对所有产品都显示相同的价值。您需要在_productSearchResult模型中添加计数器,然后在其中增加计数器。而不是显示
You have one counter for all your products which is why it's displaying the same value for all of them. You need to add your counter in your _productSearchResult model and increase it in there. And instead of displaying
Text('$_counter',
您将显示
Text('${_productSearchResult[i].counter}',
,您将更改addProduct方法以采用索引,因此您可以增加每种产品的计数器。
And you'll change your addProduct methods to take in an index so you can increase your counter for each product.
_addProduct(int index) {
setState(() {
_productSearchResult[index].counter++;
});
和删除产品相同
这篇关于特定产品的颤动增量计数器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!