在 Raisedbutton 之后从 class 中返回一个值

Returning a value from class in flutter after Raisedbutton

我正在构建我的第一个应用程序来计算我工作所需的值。 我已经在 Python 中使用了它,但这真的很不一样。 我有一个 textformfield,我在其中输入“我需要的数据”。 成功后,我从 'onPress' 按钮调用 class 并传递数据。 计算后,我需要将计算值显示在文本字段 (spev) 中。 如何将 class 'Buisber' 中的值返回到文本字段。 我找了好几天,但找不到。 我确定我在某个地方读过它,但就是不明白。 这里是一个精简版:

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,为什么不创建一个函数而不是 class 并在 Stateful 小部件中接收值?然后你可以像这样刷新状态来更新值,

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

希望对您有所帮助。