针对特定产品的颤动增量计数器?

2024-01-24

我正在flutter中做一个包含产品的应用程序,我需要做的是能够增加并获取要存储的增加产品的密钥,目前它们正在增加所有产品。

我留下了图片和代码,感谢您的帮助

当按下添加图标时,增加两个计数器的乘积 https://i.stack.imgur.com/veg6S.jpg

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 模型中添加计数器并在其中增加它。而不是显示

Text('$_counter',

您将显示

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

您将更改 addProduct 方法以接受索引,以便可以增加每个产品的计数器。

_addProduct(int index) {
setState(() {
  _productSearchResult[index].counter++;
});

删除产品也是如此。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

针对特定产品的颤动增量计数器? 的相关文章

随机推荐