Returning a value from class in flutter after Raisedbutton

1,949

@Pim, why not just create a function instead of class and receive the value inside Stateful widget? Then you can refresh the state to update the value like so,

void main() {
  runApp(new Buis());
}

class Buis extends StatefulWidget {
  @override
  BuisState createState() => BuisState();
}

class BuisState extends State<Buis> {
  var value = '';

  @override
  Widget build(BuildContext context) {
    String spev = '0';
    TextEditingController kapController = TextEditingController(text: '10.60');
    return MaterialApp(home: Scaffold(
      appBar: AppBar(
        title: Center(
          child: Text("Power Outcome"),
        ),
      ),
      body: Center(
        child: Container(
          decoration: BoxDecoration(
          ),
          child: SingleChildScrollView(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                SizedBox(height: 20),
                Column(
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: <Widget>[

                    // Calculate
                    Center(
                      child: RaisedButton(
                        shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(18.0),
                            side: BorderSide(color: Colors.red)),
                        onPressed: () {
                          value = getValue(kapController.text).toString();
                          setState(() {});
                        },
                        color: Colors.blue,
                        textColor: Colors.white,
                        child: Text("Calculate".toUpperCase(),
                            style: TextStyle(fontSize: 14)),
                      ),
                    ),

                    // Outcome Spev
                    Container(
                      alignment: Alignment(0, 0),
                      width: 80,
                      height: 30,
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.horizontal(
                          left: Radius.circular(40),
                          right: Radius.circular(40),
                        ),
                        border: Border.all(width: 1.0),
                      ),
                      child: Text(
                        value.toString(),
                        style: TextStyle(
                          fontSize: 15,
                        ),
                      ),
                    ),
                    SizedBox(height: 10),
                  ],
                ),
              ],
            ),
          ),
        ),
      ),
    )
    );
  }
}

// This can be within your BuisState
getValue (kap) {
    var kap1 = double.parse(kap);
    print(kap1);
    var spev = 1.7 * (50 / kap1);
    spev = num.parse(spev.toStringAsFixed(2));
    //testing (print works)
    //how to send back Outcome Spev
    print("Spev is " '$spev');
    return spev;
}

demo

Hope this helps.

Share:
1,949
Pim L
Author by

Pim L

Updated on December 16, 2022

Comments

  • Pim L
    Pim L over 1 year

    I am building my first app for calculating values I need for work. I already have this working in Python, but that is really different. I have a textformfield where I put in 'the data I need. With succes I call a class from 'onPress' button and pass the data. After calculation I need the calculated value to show in a textfield (spev). How can I get the value out of the class 'Buisber' back to the textfield. I am searching for days, but could not find it. I am sure I read over it somewhere, but just did not understand. Here a stripped version:

    import 'package:flutter/material.dart';
    void main() {
      runApp(MaterialApp(
        home: Buis(),
      ));
    }
    class Buis extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        String spev = '0';
        TextEditingController kapController = TextEditingController(text: '10.60');
        return Scaffold(
          appBar: AppBar(
            title: Center(
              child: Text("Power Outcome"),
            ),
          ),
          body: Center(
            child: Container(
              decoration: BoxDecoration(
              ),
              child: SingleChildScrollView(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: <Widget>[
                    SizedBox(height: 20),
                    Column(
                      mainAxisAlignment: MainAxisAlignment.end,
                      children: <Widget>[
    
                        // Calculate
                        Center(
                          child: RaisedButton(
                            shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(18.0),
                                side: BorderSide(color: Colors.red)),
                            onPressed: () {
                              Buisber(
                                kapController.text);
                            },
                            color: Colors.blue,
                            textColor: Colors.white,
                            child: Text("Calculate".toUpperCase(),
                                style: TextStyle(fontSize: 14)),
                          ),
                        ),
    
                        // Outcome Spev
                        Container(
                          alignment: Alignment(0, 0),
                          width: 80,
                          height: 30,
                          decoration: BoxDecoration(
                            borderRadius: BorderRadius.horizontal(
                              left: Radius.circular(40),
                              right: Radius.circular(40),
                            ),
                            border: Border.all(width: 1.0),
                          ),
                          child: Text(
                            '$spev',
                            style: TextStyle(
                              fontSize: 15,
                            ),
                          ),
                        ),
                        SizedBox(height: 10),
                      ],
                    ),
                  ],
                ),
              ),
            ),
          ),
        );
      }
    }
    class Buisber {
      Buisber(kap) {
        var kap1 = double.parse(kap);
        print(kap1);
        var spev = 1.7 * (50 / kap1);
        spev = num.parse(spev.toStringAsFixed(2));
        //testing (print works)
        //how to send back Outcome Spev
        print("Spev is " '$spev');
      }
    }
    
  • Pim L
    Pim L over 4 years
    Looks great thanks. I am going to implement and work with this. Just missing the experience. I need to pass 3 values and later on in different part of the program about 10 or more, any idea how to go about that? Or just repeat the lines
  • Pro
    Pro over 4 years
    Great, please mark it as answer to help others with similar question. Regarding passing more values, there are different ways you can achieve that. You can create three different textfields to pass the values and call the same getValue function with different parameters. You can also create utils.dart and move that getValue function to utils to use it in other classes.
  • Pim L
    Pim L over 4 years
    Thanks a lot. This helped really a lot. I will mark it as answer.
  • Pim L
    Pim L over 4 years
    I have another question regarding this same problem. In the getValue function I have 2 or more calculation outcome. How can I return these 2 or more values? The rest is working perfect.
  • Pro
    Pro over 4 years
    You can return a list of double values in getValue instead . You can instantiate the list first as List<double> valueList = new List<double>() and then you can add your values as valueList.add(value).
  • Pim L
    Pim L over 4 years
    I was trying to get it to work with a list already. How can I read and use the 'value' 'value1' etc.
  • Pim L
    Pim L over 4 years
    I got the valuelist in the function working. I only can not access the values outside the function.
  • Pro
    Pro over 4 years
    You can just use valueList[index].toString() to use it in textfield.
  • Pim L
    Pim L over 4 years
    Sorry but I am really trying. If I use valueList[index].tostring() outside the function getValue it says undefined name valueList. when I use value[index] it returns the caracter position of valueList even the [ and dots.
  • Pro
    Pro over 4 years
    Could you create another question with updated code? I'm assuming you're missing something.
  • Pim L
    Pim L over 4 years
    I asked a new question here: stackoverflow.com/questions/59253637/…