Math expression solution- Flutter

1.Summary of my problem
Getting solution for the following expression: t4.text = ((secondValue - thirdvalue) - (firstValue - thirdvalue)).toString();
Here for some reason Flutter is directly subtracting firstvalue by second value and leaving thirdvalue.

- Details about my goal
Using Textfields to get input from using and showing the output at the last textfield

- Expected Results
Here t4 which is the TextEditingController of textfield 4 should use the whole formula and display the result

- Actual Results
Textfield 1 value is being subtracted from Textfield 2 and the result is shown on textfield 4, no use of textfield 3

-What have I tried so far -
Googling the solution and tried to use some packages like math_expressions etc but wasnt able to figure it out

-Here is the code -

class Watchlist extends StatefulWidget {
  @override
  _WatchlistState createState() => _WatchlistState();
}

class _WatchlistState extends State<Watchlist> {


TextEditingController t1 = TextEditingController();
TextEditingController t2 = TextEditingController();
TextEditingController t3 = TextEditingController();
TextEditingController t4 = TextEditingController();

//  List<bool> isSelected = [false, false];
bool long;



  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        color: Colors.white,
        boxShadow: [kCalculatorShadow] ,
        borderRadius: BorderRadius.all(Radius.circular(10)),
      ),
      margin: EdgeInsets.symmetric(vertical: MediaQuery.of(context).size.height * 0.060, horizontal: MediaQuery.of(context).size.width * 0.07),
      child: Padding(
        padding:  EdgeInsets.symmetric(horizontal: MediaQuery.of(context).size.height * 0.025),
        child: Column(
          children: [
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  SizedBox(height: MediaQuery.of(context).size.height*0.055),
                  TextField(
                      keyboardType: TextInputType.number,//style: TextStyle(fontWeight: FontWeight.w300, color: Colors.white),
                      cursorColor: Colors.blue[900], //Blinking cursor that comes when typing number
                      controller: t1,
                      onChanged: (value){
                        calculate();
                      },
                      decoration: InputDecoration(
                        labelText: 'Initial Price',
                        labelStyle: TextStyle(fontWeight: FontWeight.w400, color: appBarColor),
                    //   fillColor: Colors.white,
                        enabledBorder: OutlineInputBorder(
                          borderSide: BorderSide(color: Colors.blue[900],width: 2),
                          borderRadius: BorderRadius.circular(18.0),
                        ),
                        focusedBorder: OutlineInputBorder(
                          borderSide: BorderSide(color: Colors.blue,width: 2),
                          borderRadius: BorderRadius.all(Radius.circular(30)),
                        ),
                      ),
                    ),
                  SizedBox(height: MediaQuery.of(context).size.height*0.05),
                  TextField(
                        keyboardType: TextInputType.number,//style: TextStyle(fontWeight: FontWeight.w300, color: Colors.white),
                        cursorColor: Colors.blue[900], //Blinking cursor that comes when typing number
                        controller: t2,
                        onChanged: (value){
                          calculate();
                        },
                        decoration: InputDecoration(
                          labelText: 'Target Price',
                          labelStyle: TextStyle(fontWeight: FontWeight.w400, color: appBarColor),
                      //   fillColor: Colors.white,
                          enabledBorder: OutlineInputBorder(
                            borderSide: BorderSide(color: Colors.blue[900],width: 2),
                            borderRadius: BorderRadius.circular(18.0),
                          ),
                          focusedBorder: OutlineInputBorder(
                            borderSide: BorderSide(color: Colors.blue,width: 2),
                            borderRadius: BorderRadius.all(Radius.circular(30)),
                          ),
                        ),
                      ),
                  SizedBox(height: MediaQuery.of(context).size.height*0.05),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceAround,
                    children: [
                      Container(
                          width:  MediaQuery.of(context).size.width*0.37,
                      child: TextField(
                        keyboardType: TextInputType.number,//style: TextStyle(fontWeight: FontWeight.w300, color: Colors.white),
                        cursorColor: Colors.blue[900], //Blinking cursor that comes when typing number
                        controller: t3,
                        onChanged: (value){
                          calculate();
                        },
                        decoration: InputDecoration(
                          labelText: 'Maker/Taker Fee',
                          labelStyle: TextStyle(fontWeight: FontWeight.w400, color: Colors.redAccent,fontSize:  MediaQuery.of(context).size.height*0.02),
                       //   hintText: 'Default: 0.2',
                          enabledBorder: OutlineInputBorder(
                            borderSide: BorderSide(color: Colors.red,width: 2),
                            borderRadius: BorderRadius.circular(18.0),
                          ),
                          focusedBorder: OutlineInputBorder(
                            borderSide: BorderSide(color: Colors.red,width: 2),
                            borderRadius: BorderRadius.all(Radius.circular(30)),
                          ),
                        ),
                      ),
                      //     child: CustomInputTextField(labelText: 'Maker/Taker Fee', controller: t2)),
                      // SizedBox(height: MediaQuery.of(context).size.height*0.05
                          ),
                      SizedBox(width: MediaQuery.of(context).size.width*0.01),
                      LiteRollingSwitch(
                        value: true,
                        textOn: 'Long',
                        textOff: 'Short',
                        colorOn: Colors.green,
                        colorOff: Colors.deepOrange,
                        iconOn: Icons.lightbulb_outline,
                        iconOff: Icons.power_settings_new,
                        animationDuration: const Duration(milliseconds: 200),
                        onChanged: (bool state) {
                          print('turned ${(state) ? 'Long On' : 'Short On'}');
                          if(state == true){ /// long will be used to switch case between which formula to use for calculation
                            long = true;
                          }else{
                            long = false;
                          }
                        },
                      ),
                    ],
                  ),
                  SizedBox(height: MediaQuery.of(context).size.height*0.05),
                  ]),
                  AbsorbPointer(
                                      child: TextField(
                          keyboardType: TextInputType.number,//style: TextStyle(fontWeight: FontWeight.w300, color: Colors.white),
                         // cursorColor: Colors.blue[900], //Blinking cursor that comes when typing number
                          controller: t4,
                          onChanged: (value){
                            calculate();
                          },
                          decoration: InputDecoration(
                            labelText: 'Result',
                            labelStyle: TextStyle(fontWeight: FontWeight.w400, color: appBarColor),
                            enabledBorder: OutlineInputBorder(
                              borderSide: BorderSide(color: Colors.greenAccent,width: 2),
                              borderRadius: BorderRadius.circular(18.0),
                            ),
                          
                          ),
                        ),
                  ),
                  SizedBox(height: MediaQuery.of(context).size.height*0.05),
          ]),
      ),
    );
  }
 void calculate() {
    if (t1.text.trim().isNotEmpty &&
        t2.text.trim().isNotEmpty &&
        t3.text.trim().isNotEmpty) {
      if(long == true){
        final firstValue = double.parse(t1.text);
        final secondValue = double.parse(t2.text);
        final thirdvalue = double.parse(t3.text);
        t4.text = ((secondValue - thirdvalue) - (firstValue - thirdvalue)).toString();
      }else{
        final firstValue = double.parse(t1.text);
        final secondValue = double.parse(t2.text);
        final thirdvalue = double.parse(t3.text);
       t4.text = (firstValue - secondValue -thirdvalue).toString();
      }
    }
  }
}

Hello, I couldn’t run your code (there are missing parts), but reading it I came across this lines for the last t4 TextField, that is confusing me:

              controller: t4,
              onChanged: (value) {
                calculate();
              },

If I get it right your code calls calculate() when values in t1, t2, or t3 changes;
calculate() then sets the value of t4.text. But you are also calling it when t4 changes, so I guess it is calculated on more time (but we should not see that because the result in t4 should be the same). Now I’m guessing if the value for t3 (the last textfield you edited) in this second round of calculate() has become somehow 0 (thus explaining your strange results where thirdvalue disappears).

Yes the calling of Calculate method on t4 was by mistake

I have also used these packages:

  cupertino_icons: ^0.1.3
  google_fonts: ^1.1.2
  line_icons: ^1.3.2
  lite_rolling_switch: ^0.1.1

Here is the full code of the page, some code from other screen is left as its just decoration and has navigation bar which doesnt play any role in calculation, just add normal scaffold to container and things should go;

I also have constants screen

    import 'package:flutter/material.dart';
    const Color appBarColor= const Color(0xFF110F29);
    const Color buttonColor=  const Color(0xFF4CAF50);
        final kCalculatorShadow =  BoxShadow(
          color: Colors.black26.withOpacity(0.3),
          spreadRadius: 5,
          blurRadius: 7,
          offset: Offset(0, 4), // changes position of shadow
          );
        final infinity = double.infinity;

Main Code which provides error(same as given in previous edit)

    import 'package:financial_calculator/Utilities/Constants.dart';
    import 'package:flutter/material.dart';
    import 'package:flutter/rendering.dart';

    import 'package:lite_rolling_switch/lite_rolling_switch.dart';



    class Watchlist extends StatefulWidget {
      @override
      _WatchlistState createState() => _WatchlistState();
    }

    class _WatchlistState extends State<Watchlist> {


    TextEditingController t1 = TextEditingController();
    TextEditingController t2 = TextEditingController();
    TextEditingController t3 = TextEditingController();
    TextEditingController t4 = TextEditingController();

    ///To check if we are long or short on a script
    bool long;



      @override
      Widget build(BuildContext context) {
        return Container(
          decoration: BoxDecoration(
            color: Colors.white,
            boxShadow: [kCalculatorShadow] ,
            borderRadius: BorderRadius.all(Radius.circular(10)),
          ),
          margin: EdgeInsets.symmetric(vertical: MediaQuery.of(context).size.height * 0.060, horizontal: MediaQuery.of(context).size.width * 0.07),
          child: Padding(
            padding:  EdgeInsets.symmetric(horizontal: MediaQuery.of(context).size.height * 0.025),
            child: Column(
              children: [
                Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      SizedBox(height: MediaQuery.of(context).size.height*0.055),
                      TextField(
                          keyboardType: TextInputType.number,//style: TextStyle(fontWeight: FontWeight.w300, color: Colors.white),
                          cursorColor: Colors.blue[900], //Blinking cursor that comes when typing number
                          controller: t1,
                          onChanged: (value){
                            calculate();
                          },
                          decoration: InputDecoration(
                            labelText: 'Initial Price',
                            labelStyle: TextStyle(fontWeight: FontWeight.w400, color: appBarColor),
                        //   fillColor: Colors.white,
                            enabledBorder: OutlineInputBorder(
                              borderSide: BorderSide(color: Colors.blue[900],width: 2),
                              borderRadius: BorderRadius.circular(18.0),
                            ),
                            focusedBorder: OutlineInputBorder(
                              borderSide: BorderSide(color: Colors.blue,width: 2),
                              borderRadius: BorderRadius.all(Radius.circular(30)),
                            ),
                          ),
                        ),
                      SizedBox(height: MediaQuery.of(context).size.height*0.05),
                      TextField(
                            keyboardType: TextInputType.number,//style: TextStyle(fontWeight: FontWeight.w300, color: Colors.white),
                            cursorColor: Colors.blue[900], //Blinking cursor that comes when typing number
                            controller: t2,
                            onChanged: (value){
                              calculate();
                            },
                            decoration: InputDecoration(
                              labelText: 'Target Price',
                              labelStyle: TextStyle(fontWeight: FontWeight.w400, color: appBarColor),
                          //   fillColor: Colors.white,
                              enabledBorder: OutlineInputBorder(
                                borderSide: BorderSide(color: Colors.blue[900],width: 2),
                                borderRadius: BorderRadius.circular(18.0),
                              ),
                              focusedBorder: OutlineInputBorder(
                                borderSide: BorderSide(color: Colors.blue,width: 2),
                                borderRadius: BorderRadius.all(Radius.circular(30)),
                              ),
                            ),
                          ),
                      SizedBox(height: MediaQuery.of(context).size.height*0.05),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceAround,
                        children: [
                          Container(
                              width:  MediaQuery.of(context).size.width*0.37,
                          child: TextField(
                            keyboardType: TextInputType.number,//style: TextStyle(fontWeight: FontWeight.w300, color: Colors.white),
                            cursorColor: Colors.blue[900], //Blinking cursor that comes when typing number
                            controller: t3,
                            onChanged: (value){
                              calculate();
                            },
                            decoration: InputDecoration(
                              labelText: 'Maker/Taker Fee',
                              labelStyle: TextStyle(fontWeight: FontWeight.w400, color: Colors.redAccent,fontSize:  MediaQuery.of(context).size.height*0.02),
                           //   hintText: 'Default: 0.2',
                              enabledBorder: OutlineInputBorder(
                                borderSide: BorderSide(color: Colors.red,width: 2),
                                borderRadius: BorderRadius.circular(18.0),
                              ),
                              focusedBorder: OutlineInputBorder(
                                borderSide: BorderSide(color: Colors.red,width: 2),
                                borderRadius: BorderRadius.all(Radius.circular(30)),
                              ),
                            ),
                          ),
                          //     child: CustomInputTextField(labelText: 'Maker/Taker Fee', controller: t2)),
                          // SizedBox(height: MediaQuery.of(context).size.height*0.05
                              ),
                          SizedBox(width: MediaQuery.of(context).size.width*0.01),
                          LiteRollingSwitch(
                            value: true,
                            textOn: 'Long',
                            textOff: 'Short',
                            colorOn: Colors.green,
                            colorOff: Colors.deepOrange,
                            iconOn: Icons.lightbulb_outline,
                            iconOff: Icons.power_settings_new,
                            animationDuration: const Duration(milliseconds: 200),
                            onChanged: (bool state) {
                              print('turned ${(state) ? 'Long On' : 'Short On'}');
                              if(state == true){ /// long will be used to switch case between which formula to use for calculation
                                long = true;
                              }else{
                                long = false;
                              }
                            },
                          ),
                        ],
                      ),
                      SizedBox(height: MediaQuery.of(context).size.height*0.05),
                      ]),
                      AbsorbPointer(
                                          child: TextField(
                              keyboardType: TextInputType.number,//style: TextStyle(fontWeight: FontWeight.w300, color: Colors.white),
                             // cursorColor: Colors.blue[900], //Blinking cursor that comes when typing number
                              controller: t4,
                              // onChanged: (value){
                              //   calculate();
                              // },
                              decoration: InputDecoration(
                                labelText: 'Result',
                                labelStyle: TextStyle(fontWeight: FontWeight.w400, color: appBarColor),
                                enabledBorder: OutlineInputBorder(
                                  borderSide: BorderSide(color: Colors.greenAccent,width: 2),
                                  borderRadius: BorderRadius.circular(18.0),
                                ),
                              
                              ),
                            ),
                      ),
                      SizedBox(height: MediaQuery.of(context).size.height*0.05),
              ]),
          ),
        );
      }
     void calculate() {
        if (t1.text.trim().isNotEmpty &&
            t2.text.trim().isNotEmpty &&
            t3.text.trim().isNotEmpty) {
          if(long == true){
            final firstValue = double.parse(t1.text);
            final secondValue = double.parse(t2.text);
            final thirdvalue = double.parse(t3.text);
            t4.text = ((secondValue - thirdvalue) - (firstValue - thirdvalue)).toString();
          }else{
            final firstValue = double.parse(t1.text);
            final secondValue = double.parse(t2.text);
            final thirdvalue = double.parse(t3.text);
           t4.text = ((firstValue - thirdvalue) - (secondValue - thirdvalue)).toString();
          }
        }
      }
    }

I’m getting too many errors trying to scaffold it up, sorry… :man_shrugging:

Here, check the app

Ty, I got it running.

I think your formulas are not written the way you expect them to work:

you are doing:

( ( B - C ) - ( A - C ) ) = ( B - C - A + C ) = B - A

and

( ( A - C ) - ( B - C ) ) = ( A - C - B + C ) = A - B

so C (thirdvalue, t3) is always cancelled out.

I have tried using different variable for B-C and A-C but getting same result.
U got any working method?

I mean that the results of your two fomulas in calculate() are the correct results to expect from them, because ((B - C) - (A - C)) = B - A and ((A - C) - (B - C)) = A - B.

The C cancels out in both formulas when you take away the inner parenthesis, i.e. in ((A - C) - (B - C)) you get it equal to (A - C - B + C) , and there the algebraic sum - C + C is 0,
leaving the expected result of A - B (that you observe in the app too).

In example: A = 120, B = 100, C = 3:
((A-C) - (B-C)) = ((120-3) - (100-3)) = (120 - 3 - 100 + 3) = 120 - 100 = 20 = A - B;
…for any value of C;

So the app is doing correctly the calculation described by the formulas and produce the expected results. You have to analyze the formulas themselves.

ok, thank you for your time and assistance sir.

My friend found the solution:
here we should use: ((A-C)-(B+C))
This provides the perfect result