为什么按下按钮后没有任何按钮出现?只出现一个 0。这是在 Flutter 中 顺便说一句

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

这是完整的代码。我觉得这里我真的缺少一些东西,而且我一直在研究它这么久,它可能是一个小错误导致了一个大错误,或者它是一个大错误。我只需要第二双眼睛来告诉我哪里出了问题。

import 'package:flutter/material.dart';

void main() => runApp(CalculatorApp());

//main app widget
class CalculatorApp extends StatelessWidget {
  const CalculatorApp({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Calculator',
      theme: ThemeData(primarySwatch: Colors.pink),
      home: Calculator(),
    );
  }
}

class Calculator extends StatefulWidget {
  const Calculator({Key? key}) : super(key: key);
  @override
  _CalculatorState createState() => _CalculatorState();
}

//It should store the value of whatever button the user presses
class _CalculatorState extends State<Calculator> {
  String _output = "0";
  String _operand = "";
  int _num1 = 0;
  int _num2 = 0;
  String _currentValue = "0";
  void buttonPressed(String buttonText) {
  if (buttonText == "CLEAR") {
    setState(() {
      _output = "0";
      _operand = "";
      _num1 = 0;
      _num2 = 0;
    });
  } else if (buttonText == "+" ||
      buttonText == "-" ||
      buttonText == "*" ||
      buttonText == "/") {
    setState(() {
      _num1 = int.parse(_output);
      _operand = buttonText;
      _output = "0";
    });
  } else if (buttonText == "=") {
    setState(() {
      _num2 = int.parse(_output);
      if (_operand == "+") {
        _output = (_num1 + _num2).toString();
      }
      if (_operand == "-") {
        _output = (_num1 - _num2).toString();
      }
      if (_operand == "*") {
        _output = (_num1 * _num2).toString();
      }
      if (_operand == "/") {
        _output = (_num1 ~/ _num2).toString();
      }
      _operand = "";
      _num1 = 0;
      _num2 = 0;
    });
  } else {
    setState(() {
      _output = _output + buttonText;
    });
  }
}

  Widget buildButton(String buttonText, Color buttonColor, Color textColor) {
    return Expanded(
      child: Padding(
        padding: const EdgeInsets.all(4.0),
        child: ElevatedButton(
          onPressed: () {
            setState(() {
              if (_currentValue == "0") {
                _currentValue = buttonText;
              } else {
                _currentValue = _currentValue + buttonText;
              }
            });
          },
          style: ElevatedButton.styleFrom(
            backgroundColor: buttonColor,
            foregroundColor: textColor,
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(20.0),
            ),
          ),
          child: Text(
            buttonText,
            style: TextStyle(
              fontSize: 24.0,
              fontWeight: FontWeight.bold,
            ),
          ),
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Flutter Calculator'),
      ),
      body: Column(
        children: <Widget>[
          Container(
            alignment: Alignment.centerRight,
            padding: EdgeInsets.symmetric(vertical: 24.0, horizontal: 12.0),
            child: Text(
              _output,
              style: TextStyle(
                fontSize: 48.0,
                fontWeight: FontWeight.bold,
              ),
            ),
          ),
          Expanded(
            child: Divider(),
          ),
          //building buttons
          Column(
            children: <Widget>[
              Row(
                children: <Widget>[
                  buildButton("7", Colors.grey, Colors.white),
                  buildButton("8", Colors.grey, Colors.white),
                  buildButton("9", Colors.grey, Colors.white),
                  buildButton("/", Colors.blue, Colors.white),
                ],
              ),
              Row(
                children: <Widget>[
                  buildButton("4", Colors.grey, Colors.white),
                  buildButton("5", Colors.grey, Colors.white),
                  buildButton("6", Colors.grey, Colors.white),
                  buildButton("*", Colors.blue, Colors.white),
                ],
              ),
              Row(
                children: <Widget>[
                  buildButton("1", Colors.grey, Colors.white),
                  buildButton("2", Colors.grey, Colors.white),
                  buildButton("3", Colors.grey, Colors.white),
                  buildButton("-", Colors.blue, Colors.white),
                ],
              ),
              Row(
                children: <Widget>[
                  buildButton("0", Colors.grey, Colors.white),
                  buildButton("CLEAR", Colors.red, Colors.white),
                  buildButton("=", Colors.blue, Colors.white),
                ],
              ),
            ],
          ),
        ],
      ),
    );
  }
} 

我期待被按下的数字出现在屏幕上来做方程式。

flutter
1个回答
0
投票

您需要进行代码优化。

函数

buttonPressed
永远不会在您的代码中调用。
_currentValue
永远不会显示在用户界面上。这是出现在用户界面上的
_output

这里是修改后的代码。

注意:这只是更新以显示您在屏幕上点击的数字,而不是功能齐全的计算器。

import 'package:flutter/material.dart';

void main() => runApp(CalculatorApp());

//main app widget
class CalculatorApp extends StatelessWidget {
  const CalculatorApp({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Calculator',
      theme: ThemeData(primarySwatch: Colors.pink),
      home: Calculator(),
    );
  }
}

class Calculator extends StatefulWidget {
  const Calculator({Key? key}) : super(key: key);
  @override
  _CalculatorState createState() => _CalculatorState();
}

//It should store the value of whatever button the user presses
class _CalculatorState extends State<Calculator> {
  String _output = "0";
  String _operand = "";
  int _num1 = 0;
  int _num2 = 0;
  String _currentValue = "0";
  void buttonPressed(String buttonText) {}

  Widget buildButton(String buttonText, Color buttonColor, Color textColor) {
    return Expanded(
      child: Padding(
        padding: const EdgeInsets.all(4.0),
        child: ElevatedButton(
          onPressed: () {
            if (buttonText == "CLEAR") {
              _output = "0";
              _operand = "";
              _num1 = 0;
              _num2 = 0;
            } else if (buttonText == "+" ||
                buttonText == "-" ||
                buttonText == "*" ||
                buttonText == "/") {
              _num1 = int.parse(_output);
              _operand = buttonText;
              _output = "0";
            } else if (buttonText == "=") {
              _num2 = int.parse(_output);
              if (_operand == "+") {
                _output = (_num1 + _num2).toString();
              }
              if (_operand == "-") {
                _output = (_num1 - _num2).toString();
              }
              if (_operand == "*") {
                _output = (_num1 * _num2).toString();
              }
              if (_operand == "/") {
                _output = (_num1 ~/ _num2).toString();
              }
              _operand = "";
              _num1 = 0;
              _num2 = 0;
            } else {
              if (_output == "0") {
                _output = buttonText;
              } else {
                _output = _output + buttonText;
              }
            }
            setState(() {});
          },
          style: ElevatedButton.styleFrom(
            backgroundColor: buttonColor,
            foregroundColor: textColor,
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(20.0),
            ),
          ),
          child: Text(
            buttonText,
            style: TextStyle(
              fontSize: 24.0,
              fontWeight: FontWeight.bold,
            ),
          ),
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Flutter Calculator'),
      ),
      body: Column(
        children: <Widget>[
          Container(
            alignment: Alignment.centerRight,
            padding: EdgeInsets.symmetric(vertical: 24.0, horizontal: 12.0),
            child: Text(
              _output,
              style: TextStyle(
                fontSize: 48.0,
                fontWeight: FontWeight.bold,
              ),
            ),
          ),
          Expanded(
            child: Divider(),
          ),
          //building buttons
          Column(
            children: <Widget>[
              Row(
                children: <Widget>[
                  buildButton("7", Colors.grey, Colors.white),
                  buildButton("8", Colors.grey, Colors.white),
                  buildButton("9", Colors.grey, Colors.white),
                  buildButton("/", Colors.blue, Colors.white),
                ],
              ),
              Row(
                children: <Widget>[
                  buildButton("4", Colors.grey, Colors.white),
                  buildButton("5", Colors.grey, Colors.white),
                  buildButton("6", Colors.grey, Colors.white),
                  buildButton("*", Colors.blue, Colors.white),
                ],
              ),
              Row(
                children: <Widget>[
                  buildButton("1", Colors.grey, Colors.white),
                  buildButton("2", Colors.grey, Colors.white),
                  buildButton("3", Colors.grey, Colors.white),
                  buildButton("-", Colors.blue, Colors.white),
                ],
              ),
              Row(
                children: <Widget>[
                  buildButton("0", Colors.grey, Colors.white),
                  buildButton("CLEAR", Colors.red, Colors.white),
                  buildButton("=", Colors.blue, Colors.white),
                ],
              ),
            ],
          ),
        ],
      ),
    );
  }
}

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