Flutter increment counter for a specific product?

2020-07-28 22:32发布

问题:

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

When press on add icon increment two counter product

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++;
});

}

回答1:

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',

You'll display

Text('${_productSearchResult[i].counter}',

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++;
});

and the same for remove product.