Flutter increment counter for a specific product?

£可爱£侵袭症+ 提交于 2020-01-24 00:17: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.



来源:https://stackoverflow.com/questions/55232501/flutter-increment-counter-for-a-specific-product

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!