我想添加CheckBox值,如果用户选中了CheckBox,则应该添加amount = amount + 18%,如果没有,则仅添加我的代码

问题描述 投票:0回答:2

请帮助我我想添加,如果用户选中复选框,则应添加数量=数量+ 18%,如果不是,则仅添加数量我的密码最后的amountController = TextEditingController();

Container(
  height: 50.0,
  padding: EdgeInsets.only(left: 8),
  margin: EdgeInsets.only(top: 8),
  decoration:
      BoxDecoration(border: Border.all(color: Colors.grey[300], width: 1)),
  child: TextFormField(
    controller: amountController,
    decoration: new InputDecoration(
      border: InputBorder.none,
      hintStyle: new TextStyle(color: Colors.grey[500]),
      hintText: "Assesment Amount",
      fillColor: Colors.transparent,
    ),
    keyboardType: TextInputType.number,
  ),
);

// =======复选框代码=============>

Container(
  child: Checkbox(
      value: checkBoxValue,
      onChanged: (value) {
        setState(() {
          checkBoxValue = value;
        });
      }),
);

// =========插入按钮代码========= =>

else if (update == false && imageFile != null) {
  setState(() {
    name = titleController.text;
    nameList.add(name);
    amountList.add(amountController.text);
    img.add(imageFile);
    titleController.clear();
    amountController.clear();
    imageFile = null;
  });
}

[请帮助我,我想添加,如果用户选中了复选框,则应该添加amount = amount + 18%,如果不是,则仅添加我的代码最终的amountController = TextEditingController();容器(...

flutter dart flutter-layout flutter-dependencies flutter-test
2个回答
0
投票

首先,我们将String中的写入量解析为整数,以便我们可以执行计算,然后可以使用boolean checkBoxValue获取checkbox的状态。如果checkBoxValue == true,我们将输入金额变量的18%,否则我们将按原样添加金额。

else if (update == false && imageFile != null) {
      setState(() {
      name = titleController.text;
      nameList.add(name);
      int _amount = int.parse(amountController.text);
      _amount = checkBoxValue ? _amount + ((18 / _amount) * 100) : _amount;
      amountList.add(_amount);
      img.add(imageFile);
      titleController.clear();
      amountController.clear();
      imageFile = null;
      });
}

0
投票
int amount=0;
if (checkBoxValue){
    amount = int.parse(amountController.text) + int.parse(amountController.text)*18/100;
}
else {
    amount = int.parse(amountController.text);
}
© www.soinside.com 2019 - 2024. All rights reserved.