How to increment counter for a specific list item in Flutter?

后端 未结 2 801
抹茶落季
抹茶落季 2021-01-13 04:47

Like in my sample image, below, I want to increment or decrement quantity upon button click for single list item. If I increase the counter in setState(), its incrementing i

2条回答
  •  野的像风
    2021-01-13 05:42

    Add this code as a children of your Column or Row:

     itemData[index].ShouldVisible?
                                 Center(
                                     child: Container(
                                       height: 30,
                                       width: 70,
                                       decoration: BoxDecoration(
                                           borderRadius: BorderRadius.circular(4),
                                           border: Border.all(color: Colors.white70)
                                       ),
                                       child: Row(
                                         mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                                         children: [
                                           InkWell(
                                           onTap: (){
                                            setState(() {
                                              if(itemData[index].Counter <2)
                                                {
                                                  itemData[index].ShouldVisible = !itemData[index].ShouldVisible;
                                                }else{
                                               itemData[index].Counter--;
                                              }
    
                                            });
                                           }
                                           ,child: Icon(Icons.remove,color: Colors.green,size: 18,)),
                                           Text('${itemData[index].Counter}',style: TextStyle(
                                             color: Colors.white70
                                           ),
                                           ),
                                           InkWell(
                                           onTap: (){
                                             setState(() {
                                               itemData[index].Counter++;
                                             });
                                           }
                                           ,child: Icon(Icons.add,color: Colors.green,size: 18,)),
    
                                         ],
                                       ),
    
                                     )
                                 ) : Center(
                                    child: Container(
                                      padding: EdgeInsets.all(5),
                                      height: 30,
                                      width: 70,
                                      decoration: BoxDecoration(
                                      borderRadius: BorderRadius.circular(4),
                                        border: Border.all(color: Colors.white70)
                                      ),
                                      child: Row(
                                        crossAxisAlignment: CrossAxisAlignment.center,
                                        mainAxisAlignment: MainAxisAlignment.center,
                                        children: [
                                          Text('ADD',style: TextStyle(color: Colors.white70),
                                          ),
                                          InkWell(
                                            onTap: (){
    
                                              setState(() {
                                                itemData[index].ShouldVisible = ! itemData[index].ShouldVisible;
    //                                          
    
                                              });
                                            }
                                              ,child: Center(child: Icon(Icons.add,color: Colors.green,size: 18,)))
    
                                        ],
                                      ),
    
                                    ),
                                  )
    

    Add Counter and ShouldVisible to your DataStructure as shown below.

    class ItemData{
      String Name;
      int Counter;
      bool ShouldVisible;
    
      ItemData({
       this.Name,
        this.Counter,
        this.ShouldVisible
    });
    }
    
    List itemData = [
    ItemData(
      Name: 'Shoes 1',
      Counter: 1,
      ShouldVisible: false
    ),
      ItemData(
          Name: 'Shoes 2',
          Counter: 1,
          ShouldVisible: false
      ),];
    

提交回复
热议问题