Problem with adding two numbers stored in textfield in flutter. I don't know what is wrong

Viewed 400

I am trying to add two numbers that is it but can't do it. I think it should be simple enough but i guess it is not so. I think everything is fine but yet it doesn't work at all.

 import 'package:flutter/material.dart';
 void main() {  runApp(ClassMaterial());  }
 class ClassMaterial extends StatefulWidget {  @override  _Tax 
 createState() => _Tax();    }
   class _Tax extends State<ClassMaterial> {
 bool _value = false;
 bool a = false;
 bool b = false;

 final TextEditingController x = TextEditingController();
 final TextEditingController y = TextEditingController();
 var basic = 0, other = 0, sum = 0;

 void doSum() {
   setState(() {
     basic = int.parse(x.text);
     other = int.parse(y.text);
     sum += basic + other;
   });
 }

 @override
 Widget build(BuildContext context) {
   return MaterialApp(
     home: Scaffold(
       appBar: AppBar(
         title: Text('SALARY TAX CALCULATOR'),
         centerTitle: true,
       ),
       body: Card(
           child:
               Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
         Container(
           margin: EdgeInsets.fromLTRB(10, 10, 0, 0),
           child: Text('GENDER',
               textAlign: TextAlign.left,
               style: TextStyle(fontSize: 25, fontWeight: FontWeight.bold)),
         ),
         Row(
           children: <Widget>[
             Container(margin: EdgeInsets.fromLTRB(20, 0, 15, 0)),
             Text('Male'),
             (Checkbox(
                 value: _value,
                 onChanged: (value) {
                   setState(() {
                     _value = value;
                     a = false;
                     b = false;
                   });
                 }))
           ],
         ),
         Row(
           children: <Widget>[
             Container(margin: EdgeInsets.fromLTRB(20, 0, 0, 0)),
             Text('Female'),
             (Checkbox(
                 value: a,
                 onChanged: (value) {
                   setState(() {
                     a = value;
                     _value = false;
                     b = false;
                   });
                 }))
           ],
         ),
         Row(
           children: <Widget>[
             Container(margin: EdgeInsets.fromLTRB(20, 0, 3, 0)),
             Text('Couple'),
             (Checkbox(
                 value: b,
                 onChanged: (value) {
                   setState(() {
                     b = value;
                     _value = false;
                     a = false;
                   });
                 }))
           ],
         ),
         Container(
           margin: EdgeInsets.fromLTRB(10, 10, 0, 0),
           child: Text('SALARY DETAILS',
               textAlign: TextAlign.left,
               style: TextStyle(fontSize: 25, fontWeight: FontWeight.bold)),
         ),
         TextField(
           controller: x,
           decoration: InputDecoration(
             labelText: "Basic Salary(Monthly)",
             fillColor: Colors.white,
             border: new OutlineInputBorder(
               borderRadius: new BorderRadius.circular(25.0),
               borderSide: new BorderSide(),
             ),
             //fillColor: Colors.green
           ),
           keyboardType: TextInputType.number,
           style: new TextStyle(
             fontFamily: "Poppins",
           ),
         ),
         TextField(
           controller: y,
           decoration: InputDecoration(
             labelText: "Other Salary(Monthly)",
             fillColor: Colors.white,
             border: new OutlineInputBorder(
               borderRadius: new BorderRadius.circular(25.0),
               borderSide: new BorderSide(),
             ),
             //fillColor: Colors.green
           ),
           keyboardType: TextInputType.number,
           style: new TextStyle(
             fontFamily: "Poppins",
           ),
         ),
         Text("$sum")
       ])),
     ),
   );
 }    }
2 Answers

Change doSum() like this:

  void doSum() {
    setState(() {
      basic = x.text.isEmpty ? 0 : int.parse(x.text);
      other = y.text.isEmpty ? 0 : int.parse(y.text);
      sum = basic + other;
    });
  }

And add onChnaged in TextFields like this:

onChanged: (String value) {
              doSum();
            },

Let me know if this resolves your issue.

In your code, you have added 2 text boxes where you can enter two data. There is nothing in your code that calls to add these two data.

Now what you need is to add a button called "add" and add another textbox that shows the result of the two numbers added.

Related