【问题标题】:How to set value on selected items with checkboxListTile如何使用 checkboxListTile 设置选定项目的值
【发布时间】:2021-07-25 09:51:33
【问题描述】:

checkboxListTile的Setstate的正确方法是什么 获得切换按钮和附加值?

我想要的是当您选中该框时,您会看到选中按钮。 然后将价值添加到基本价格中。请帮忙。

CheckboxListTile(
                                activeColor: Colors.blue,
                                dense: true,
                                //font change
                                title: new Text(
                                  listTopping[i].price.toStringAsFixed(0) +
                                      ' บาท',
                                  style: TextStyle(
                                    fontSize: 25,
                                    fontWeight: FontWeight.bold,
                                  ),
                                ),
                                value: listTopping[i].isCheck,
                                secondary: Container(
                                  height: 50,
                                  width: 300,
                                  child: Text(
                                    listTopping[i].topping,
                                    style: TextStyle(
                                      fontSize: 25,
                                      fontWeight: FontWeight.bold,
                                    ),
                                  ),
                                ),
                                onChanged: (bool? val) {
                                  itemChange(val!, i);
                                },
                              ),

这是我认为是错误的 setstate...

  void itemChange(bool val, int i) {
    setState(() {
      listTopping[i].isCheck = val;
    });
  }
}

【问题讨论】:

  • 试过你的代码。在CheckboxListTile 列表上方添加了一个Text 和检查总和。按预期工作。
  • 很抱歉再次打扰您,但您又做了什么?我真的不明白。请问我可以了解更多详情吗?
  • 我复制并粘贴了您的代码。并添加了一个Text 小部件来显示总和。即使我更改了checks,总和也会正确显示。
  • 当我在 CheckBoxListTile 上方添加文本时,它给了我整个红线。我会尝试重复你所说的,并尝试直到我理解为止。你现在一直在帮助我参加两个论坛。我很感激。非常感谢。
  • 我添加了我的代码。希望对你有帮助!

标签: list flutter dart checkbox setstate


【解决方案1】:

这是您的代码的一个小示例。由于您没有提供 AddonTopping 类,我为自己创建了一个简单的版本。您可以尝试并在您的MaterialApp 中运行此小部件。一切都应该按预期工作。

class AddonTopping {
  AddonTopping({
    required this.id,
    required this.topping,
    required this.isCheck,
    required this.price,
  });
  final int id;
  final String topping;
  bool isCheck;
  final int price;
}

class Americano extends StatefulWidget {
  @override
  _AmericanoState createState() => _AmericanoState();
}

class _AmericanoState extends State<Americano> {
  List<AddonTopping> listTopping = [
    AddonTopping(
      id: 8,
      topping: 'Whipcream',
      price: 0,
      isCheck: true,
    ),
    AddonTopping(
      id: 9,
      topping: 'Javachip',
      price: 30,
      isCheck: false,
    ),
    AddonTopping(
      id: 10,
      topping: 'SoyMilk',
      price: 20,
      isCheck: false,
    ),
    AddonTopping(
      id: 11,
      topping: 'ExtraSyrup',
      price: 30,
      isCheck: false,
    ),
  ];

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Text('SUM: ${calculatePrice()}'),
        ListView.builder(
          shrinkWrap: true,
          itemCount: listTopping.length,
          itemBuilder: (context, i) => CheckboxListTile(
            activeColor: Colors.blue,
            dense: true,
            //font change
            title: Text(
              listTopping[i].price.toStringAsFixed(0),
              style: TextStyle(
                fontSize: 25,
                fontWeight: FontWeight.bold,
              ),
            ),
            value: listTopping[i].isCheck,
            secondary: Container(
              height: 50,
              width: 300,
              child: Text(
                listTopping[i].topping,
                style: TextStyle(
                  fontSize: 25,
                  fontWeight: FontWeight.bold,
                ),
              ),
            ),
            onChanged: (bool? val) {
              itemChange(val!, i);
            },
          ),
        ),
      ],
    );
  }

  void itemChange(bool val, int i) {
    setState(() {
      listTopping[i].isCheck = val;
    });
  }

  double calculatePrice() {
    if (listTopping.isNotEmpty) {
      double _price = 0.0;
      // Get those toppings that are chosen (`isCheck` is true)
      final chosenTopping = listTopping.where((element) => element.isCheck);

      // Calculate the sum
      for (final AddonTopping item in chosenTopping) {
        if (item.isCheck) {
          _price += item.price;
        }
      }
      return _price;
    }
    return 0.00;
  }
}

【讨论】:

猜你喜欢
  • 2021-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-29
  • 2020-12-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多