CheckBox value, I want to add ,if user check the checkBox then it should add amount=amount + 18% , if not then only amount my Code

Issue

I want to add, if the user checks the checkBox then it should add amount=amount + 18%, if not then the only amount
my Code

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

//=======checkBox code============

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

//========Insert button Code=========

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

Solution

First of all we will parse written amount which is in String to integer so we can perform calculations then we can get status of checkbox using boolean checkBoxValue. If checkBoxValue == true we will add 18 % of amount entered into amount variable else we will just add amount as it is.

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

Answered By – Haroon Ashraf Awan

Answer Checked By – Katrina (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *