【问题标题】:How to Update Elevated Button title on Pressed in Particular index of ListView.builder如何在 ListView.builder 的特定索引中按下时更新提升的按钮标题
【发布时间】:2021-11-24 05:13:26
【问题描述】:

这是我的代码。

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

问题是,如果我点击那个提升的按钮,那么该按钮的文本只能在该索引上更改。但它在 listview.builder 中的所有索引中都在发生变化。

任何人都可以为此提供解决方案,即所选索引上的一个按钮必须使用更改的名称进行更新。

【问题讨论】:

  • 确保使用有状态的小部件。而不是使用单个布尔变量,您应该有一个字符串列表并使用 ListView 构建器索引对其进行索引。
  • 是的,我正在使用 stateFulWidget。
  • 这显然是一个逻辑错误。您必须为列表的每个索引创建一个标志。并检查标志是真(添加到购物车)还是假(未添加到购物车)。在这种情况下,您所做的只是更新列表中所有按钮的标题。

标签: flutter


【解决方案1】:

您需要为每个索引保留标志isAddedToCart。您可以使用Map 来实现它。像这样的:

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

然后在您的小部件中使用它:

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

【讨论】:

  • 它工作正常,我有一个问题..如何再次将相同的按钮更新为“添加到购物车”。感谢您的回复@ישואוהבאותך
【解决方案2】:

所有项目都依赖于isAddedToCart ,但您需要使用单独的索引存储所选项目

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

完整的源代码

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

【讨论】:

  • @GH 你可以试试这个,希望对你有用
  • 那太棒了。谢谢。
  • 它在重新加载时已恢复为默认值。我们可以使用模型来做吗,如果可以,怎么做?
  • 你应该点击这个链接stackoverflow.com/questions/57380673/…
猜你喜欢
  • 1970-01-01
  • 2015-07-12
  • 1970-01-01
  • 2018-08-07
  • 2023-03-25
  • 1970-01-01
  • 1970-01-01
  • 2019-05-07
  • 1970-01-01
相关资源
最近更新 更多