【问题标题】:Flutter increment counter for a specific product?特定产品的颤振增量计数器?
【发布时间】:2019-03-19 01:44:21
【问题描述】:

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

我留下图像和我的代码,谢谢你的帮助

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

}

【问题讨论】:

    标签: flutter counter product


    【解决方案1】:

    您的所有产品都有一个计数器,这就是为什么它会为所有产品显示相同的值。您需要在 _productSearchResult 模型中添加计数器并在其中增加它。而不是显示

    Text('$_counter',
    

    你会显示

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

    您将更改 addProduct 方法以获取索引,这样您就可以增加每个产品的计数器。

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

    删除产品也是如此。

    【讨论】:

    • @ErickReyes 不客气。如果答案正确,您应该接受答案:) 让其他开发人员知道它是否已解决。
    • 谢谢朋友,你让我开心
    • 我在Future Builder 中使用了相同的方法,但是在_productSearchResult[index].counter++ 的位置出现了一些错误我使用的是snapshot.data[index].counter++,但它显示错误。我们应该如何在未来的建设者中实现同样的目标
    • @Youandtheweb 它正在使用的构建器无关紧要。你的计数器值是最终值吗?
    • @FilledStacks 是的,我尝试输入final int _counter =0,但点击按钮后数量没有变化
    猜你喜欢
    • 2021-09-23
    • 1970-01-01
    • 2019-07-03
    • 1970-01-01
    • 2017-10-01
    • 2020-06-18
    • 1970-01-01
    • 2021-05-08
    相关资源
    最近更新 更多