Search code examples
flutterdropdown

HOW TO RETURN RESULT FOR CURRENT DROPDOWN SELECTED WHEN I APPLY A CALCULATE BUTTON IN FLUTTER?


I would like to know how to implement this code, with three input variables and three different solutions related to each dropdown option. Each time i choose an option, does it block the entry that is not part of the equation and return the calculation value of the selected item?

var _currencies = ['A', 'B', 'C'];  

DropdownButtonHideUnderline( child: DropdownButton<String>(
                                  items: _currencies.map((String value) {
                                    return DropdownMenuItem<String>(
                                      value: value,
                                      child: Text(value),
                                    );
                                  }).toList(),
                                  value: _currentItemSelected,
                                  isDense: true,
                                  onChanged: (String newValueSelected) {
                                    //code a executar
                                    _onDropDownItemSelected(
                                        newValueSelected);
                                  },
                                ))


 String _calculateTotalReturns() {
double entry1 = double.parse(entry1Controller.text);
double entry2 = double.parse(entry2Controller.text);
double entry3 = double.parse(entry3Controller.text);

//Three equations for each dropdown option

double total1 = entry1 * entry2;             //FOR DROPDOWN A
double total2 = entry1 + entry2 + entry3     //FOR DROPDOWN B
double total3 = entry1*entry2+entry3         //FOR DROPDOWN C


String result = '';

return result; // How to return result for curerrent dropdown selected when i aply a calculate button??? }

//--------------------------------------------------------------------

child: RaisedButton(
                          child: Text(
                            'Calcular',
                            textScaleFactor: 1.5,
                          ),
                          color: Theme.of(context).accentColor,
                          textColor: Theme.of(context).primaryColorDark,
                          onPressed: () {
                            setState(() {
                              if (_formKey.currentState.validate()) {
                                this.displayResult =
                                    _calculateTotalReturns();
                              }
                            });
                          },
                        ),

Solution

  • You will need to have a global variable which will store the value which value user selected from the dropdown. For example,

    String selectedValue;  // <-- this will be a global variable.
    

    Change the global variable on onChanged

    onChanged: (String newValueSelected) {
        selectedValue = newValueSelected;
    }
    

    Then you can access this variable in the _calculateTotalReturns() method and return the correct result accordingly like..

        String _calculateTotalReturns() {
           
           //other code
    
           String result;
      
           if(selectedValue == 'A'){
              result = total1.toString();
           }
           else if(selectedValue == 'B'){
              result = total2.toString();
           }
           else{
              result = total3.toString();
           }
      
           return result;
        }