如何按通视至极按钮信息?

问题描述 投票:0回答:2

我真的可以使用一些帮助这里....我一直在做这个的实在太久了。

我现在明白了如何得到正确的按钮,采取正确的行动(从你的帮助)。我还在困惑如何获得(“$ A + $ B =”)屏幕2 - 在那里我将有一个键盘键入答案。

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(""),
      ),
    );
  }
}

堆栈溢出希望我增加更多的信息张贴这一点,所以......好吧,如果我的问题是模糊或质疑不当,请让我知道这样我就可以做出更好的问题。

button callback flutter parameter-passing
2个回答
2
投票

我真的不明白的问题,但只是路过标识按下的按钮应该做一个值:

FlatButton(..., onPressed: () => handleButtonPressed('+'),
FlatButton(..., onPressed: () => handleButtonPressed('-'),

...

void handleButtonPressed(String buttonName) {
  if(buttonName == '+') { ... }
  else if(...) { ... }
}

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;
                }),
              )
            ],
          ),
    );
  }
...

希望它能帮助,祝您好运!

© www.soinside.com 2019 - 2024. All rights reserved.