当前位置: 首页 > 知识库问答 >
问题:

获取userQuestion和userAnswer错误

章越
2023-03-14

Dart不仅用于移动应用程序开发,而且是一种编程语言。由Ecma(ECMA-408)批准为标准,它用于构建web上的任何东西,服务器,桌面,当然还有移动应用程序(是的,正是这些人标准化了我们最喜欢的ES5和ES6。)

import 'package:flutter/material.dart';
import 'buttons.dart';
import 'package:math_expressions/math_expressions.dart';

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

bool isOperator(String x) {
  if (x == '%' || x == '/' || x == 'x' || x == '+' || x == '-' || x == '=') {
    return true;
  }
  return false;
}

void equalPressed() {
  String finalQuestion = userQuestion;
  finalQuestion = finalQuestion.replaceAll('x', '*');

  Parser p = Parser();
  Expression exp = p.parse(finalQuestion);
  ContextModel cm = ContextModel();
  double eval = exp.evaluate(EvaluationType.REAL, cm);

 userAnswer = eval.toString();
}

class _HomePageState extends State<HomePage> {
  var userQuestion = '';
  var userAnswer = '';

  final List<String> buttons = [
    'C',
    'DEL',
    '%',
    '/',
    '7',
    '8',
    '9',
    'x',
    '4',
    '5',
    '6',
    '-',
    '1',
    '2',
    '3',
    '+',
    '0',
    '00',
    '.',
    '=',
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Color(0xFFE5CCFF),
      body: Column(
        children: <Widget>[
          Expanded(
            child: Container(
              child: Column(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: <Widget>[
                    SizedBox(
                      height: 50,
                    ),
                    Container(
                      padding: EdgeInsets.all(20),
                      alignment: Alignment.centerLeft,
                      child: Text(
                        userQuestion,
                        style: TextStyle(fontSize: 20),
                      ),
                    ),
                    Container(
                      padding: EdgeInsets.all(20),
                      alignment: Alignment.centerRight,
                      child: Text(
                        userAnswer,
                        style: TextStyle(fontSize: 20),
                      ),
                    ),
                  ]),
            ),
          ),
          Expanded(
            flex: 2,
            child: Container(
              child: GridView.builder(
                  itemCount: buttons.length,
                  gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                      crossAxisCount: 4),
                  itemBuilder: (BuildContext context, int index) {
                    // clear button
                    if (index == 0) {
                      return MyButton(
                        buttonTapped: () {
                          setState(() {
                            userQuestion = '';
                          });
                        },
                        buttonText: buttons[index],
                        color: Colors.green,
                        textColor: Colors.white,
                      );
                    } else if (index == 1) {
                      // Delete Button
                      return MyButton(
                        buttonTapped: () {
                          setState(() {
                            userQuestion = userQuestion.substring(
                                0, userQuestion.length-1);
                          });
                        },
                        buttonText: buttons[index],
                        color: Colors.red,
                        textColor: Colors.white,
                      );
                    }
                    // equal button
                    else if (index == buttons.length-1) {
                      return MyButton(
                        buttonTapped: () {
                          setState(() {
                            equalPressed();
                          });
                        },
                        buttonText: buttons[index],
                        color: Colors.deepPurple,
                        textColor: Colors.white,
                      );
                    } else {
                      // Rest of the Button
                      return MyButton(
                        buttonTapped: () {
                          setState(() {
                            userQuestion += buttons[index];
                          });
                        },
                        buttonText: buttons[index],
                        color: isOperator(buttons[index])
                            ? Colors.deepPurple
                            : Colors.deepPurple[50],
                        textColor: isOperator(buttons[index])
                            ? Colors.white
                            : Colors.deepPurple,
                      );
                    }
                  }),
            ),
          ),
        ],
      ),
    );
  }
}


共有1个答案

农飞翔
2023-03-14

在_homePagestate中设置isoperatorequalpressed方法

 类似资料:
  • 问题内容: 从数据库取回数据时遇到问题。我正在尽力解释这个问题。 1.如果在下面的代码中保留“ mode”:“ no- cors”,则可以使用Postman从服务器获取数据,而不能使用自己的服务器获取数据。认为这一定是我的客户端错误 当我删除“ mode”:“ no-cors”时,我得到2个错误:-Fetch API无法加载http:// localhost:3000 /。飞行前响应中的Acces

  • 我无法恢复数据从我的Wordpress API与改造库。错误是:HTTP FAILED:java.net.未知主机异常:无法解决主机"app.divion.fr":没有地址与主机名关联 ApiClient: } API接口: 公共接口{ } 显示xml Thx;)

  • 所以,我从用户提示输入变量。如果变量小于0且大于10。如果用户输入

  • 在将新更新安装到Visual Studio后,我才开始出现此错误。出于某种原因,我的建筑现在都不会从詹金斯那里得到。已经做了几个小时了,似乎还不明白。 我从比特桶里取东西。 请帮助。 由工作区C:\Program Files(x86)\Jenkins\workspace\CA[WS-CLEANUP]中的计时器生成启动删除项目工作区。。。[WS-CLEANUP]已完成克隆远程Git存储库克隆存储库h

  • 问题内容: 我正在使用位于此处的教程:http : //addyosmani.github.io/backbone-fundamentals/#create- a-simple-web-server 并添加了以下代码。 在启动服务器后,我收到一条错误消息,指出访问时的错误,我只是想知道是否有人对这个错误了解很多,因为Express和Node对我来说是新的? 问题答案: 我认为您缺少路线,您需要定义

  • 我想从这个函数中获取星期几(星期一,星期二,...),该函数接收参数3 int,即年份、月份和日期,我已经尝试了Calendar和GregorianCalendar,但它仍然给我错误,例如,如果我今天通过日期(2017,11,04)它给我一周中第5天的结果,星期四是今天星期二,我给你留下代码 Log. i显示此消息: DiaSemana:5 dias:11 mes:4 año:2017