如何根据夹心按钮被按下传递信息?

时间:2019-02-09 21:55:50

标签: button callback flutter parameter-passing

我真的可以在这里使用一些帮助。...我从事此工作已经太久了。

我现在了解如何获得正确的按钮以采取正确的操作(在您的帮助下)。我仍然很困惑,但是如何获得(“ $ a + $ b =”)进入screen2-在这里我将有一个键盘来输入答案。

import 'package:flutter/material.dart';
import 'dart:math';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "Sigh",
      theme: ThemeData(primarySwatch: Colors.green),
      home: MyHome(),
      routes: <String, WidgetBuilder>{
        '/screen1': (BuildContext context) => MyHome(),
        //     '/screen2': (BuildContext context) => MyOutput(),  supposed to be the keyboardscreen.
      },
    );
  }
}

class MyHome extends StatefulWidget {
  @override
  _MyHomeState createState() => _MyHomeState();
}

class _MyHomeState extends State<MyHome> {
  final random = Random();

  int a, b, sum;
  String output;

  void changeData() {
    setState(() {
      a = random.nextInt(10);
      b = random.nextInt(10);

      setState(() {});
    });
  }

  void handleButtonPressed(String buttonName) {
    if (buttonName == '+') {
      sum = a + b;
      output = '$a + $b =';
    } else if (buttonName == '-') {
      if (a >= b) {
        sum = a - b;
        output = "$a - $b =";
      } else if (b > a) {  //sum cannot be negative here
        sum = b - a;
        output = "$b - $a =";
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("SIGH"),
        backgroundColor: Colors.green,
      ),
      backgroundColor: Colors.lightGreen,
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            RaisedButton(
              child: Text("+"),
              onPressed: () {
            Navigator.of(context).pushNamed('/screen2');
            handleButtonPressed('+');
          }, //how can I get the output to my keyboardscreen?
            ),
            RaisedButton(
              child: Text("-"),
              onPressed: () {
            Navigator.of(context).pushNamed('/screen2');
            handleButtonPressed('-');
          },
            ),
          ],
        ),
      ),
    );
  }
}

class MyOutput extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        color: Colors.lightBlue,
      ),
      child: Center(
        child: Text(""),
      ),
    );
  }
}

堆栈溢出希望我添加更多信息以发布此信息,所以...好吧,如果我的问题模糊或疑问少,请告诉我,以便我提出更好的问题。

2 个答案:

答案 0 :(得分:2)

我不是很了解这个问题,但是只要传递一个标识按下按钮的值就可以了:

<option value="<?PHP echo ''.$row['discount'];?>"><?PHP echo ''.$row['discount'];?></option>

答案 1 :(得分:1)

要了解按下了哪个按钮,应使用存储当前操作状态的变量。

例如,变量curOperationState具有两个状态OperationType.plusOperationType.minus。当您按下加号按钮等于curOperationState=OperationType.plus;时,当您按下减号按钮等于curOperationState=OperationType.minus;

稍后,您只需要检查curOperationState变量的当前状态即可。

要进行检查,请使用以下命令:if (curOperationState == OperationType.plus) { ...

使用代码:

...

enum OperationType {plus, minus}

class TestPage extends StatelessWidget {

  OperationType curOperationState = OperationType.minus;

  @override
  Widget build(BuildContext context) {
    return Container(
        alignment: Alignment.center,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: <Widget>[
              RaisedButton(
                child: Text('+'),
                onPressed: (() {
                  curOperationState = OperationType.plus;
                }),
              ),
              RaisedButton(
                child: Text('-'),
                onPressed: (() {
                  curOperationState = OperationType.minus;
                }),
              )
            ],
          ),
    );
  }
...

希望有帮助,祝你好运!