How to Update Elevated Button title on Pressed in Particular index of ListView.builder

0

Here is my code.

bool isAddedToCart = false;
return ListView.builder(
........
 ElevatedButton (
                        child: isAddedToCart? Text('Added to cart') : Text('Add to cart'),
                         style: ElevatedButton.styleFrom(
                           primary: Constants.primaryColor,
                           onPrimary: Constants.appColor
                         ),
                         onPressed: () async{
                           setState(() {
                             isAddedToCart = !isAddedToCart;
                           });
                           
                         },
                      
                       ),

The problem is, if I click on that elevated Button the text of that button has to change on that index only. But it is changing in all the index which are in listview.builder.

Can any one have a solution for this that only one button on selected index has to update with changed name.

flutter
2021-11-24 05:13:26
2

2

You need to keep the flag isAddedToCart for each index. You can achieve it by using a Map. Something like this:

// class variable scope.
Map<int, bool> isAddedToCartMap = {};

then use it in your widget:

ElevatedButton (
    // if isAddedToCartMap[index] not found, use false as default value.
    child: isAddedToCartMap[index]??false ? Text('Added to cart') : Text('Add to cart'),
     style: ElevatedButton.styleFrom(
       primary: Constants.primaryColor,
       onPrimary: Constants.appColor
     ),
     onPressed: () async{
       setState(() {
         isAddedToCartMap[index] = !isAddedToCartMap[index]??false;
       });
       
     },
  
   ),
2021-11-24 05:24:43

its working fine, i have a dout.. How to update the same button to 'Add to cart' again. thanks for reply @ישו אוהב אותך
H ă ɤ í
1

All items were depends on isAddedToCart but You need to store the selected item with separate indexing

List<int> _selected_item = List();

 ElevatedButton(
              child: _selected_item.contains(index)
                  ? Text('Added to cart')
                  : Text('Add to cart'),
              style: ElevatedButton.styleFrom(),
              onPressed: () async {
                setState(() {
                  // remove or add index to _selected_item
                  if (_selected_item.contains(index))
                    _selected_item.remove(index);
                  else
                    _selected_item.add(index);
                  print(index);
                });
              },
            )

complete source code

ListView.builder(
          itemCount: 5,
          itemBuilder: (context, index) {
            return ElevatedButton(
              child: _selected_item.contains(index)
                  ? Text('Added to cart')
                  : Text('Add to cart'),
              style: ElevatedButton.styleFrom(),
              onPressed: () async {
                setState(() {
                  // remove or add index to _selected_item
                  if (_selected_item.contains(index))
                    _selected_item.remove(index);
                  else
                    _selected_item.add(index);
                  print(index);
                });
              },
            );
          })
2021-11-24 06:01:37

@GH you can try out this I hope, it will work for you
Jahidul Islam

that was super. thankyou.
H ă ɤ í

it was getting to default when it is reloading. Can we do it using model, if yes, how?
H ă ɤ í

you should follow this link stackoverflow.com/questions/57380673/…
Jahidul Islam

In other languages

This page is in other languages

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................