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
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
),];