Javascript 如何在计算器应用程序颤振中添加功能

Javascript 如何在计算器应用程序颤振中添加功能,javascript,java,flutter,kotlin,dart,Javascript,Java,Flutter,Kotlin,Dart,我第一次在功能上构建了一个颤振计算器应用程序,但夹在中间的是下面的代码 import 'package:calculator/constants/constants.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; void main() { runApp(MyApp()); } class MyApp extends Sta

我第一次在功能上构建了一个颤振计算器应用程序,但夹在中间的是下面的代码

import 'package:calculator/constants/constants.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      // This widget is the root of your application.
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Calculator',
          theme: ThemeData(
            // This is the theme of your application.
            //
            // Try running your application with "flutter run". You'll see the
            // application has a blue toolbar. Then, without quitting the app, try
            // changing the primarySwatch below to Colors.green and then invoke
            // "hot reload" (press "r" in the console where you ran "flutter run",
            // or simply save your changes to "hot reload" in a Flutter IDE).
            // Notice that the counter didn't reset back to zero; the application
            // is not restarted.
            primarySwatch: Colors.blue,
          ),
          home: MyHomePage(),
          debugShowCheckedModeBanner: false,
        );
      }
    }
    
    class MyHomePage extends StatefulWidget {
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      String output = '0';
    
      /* String _privateOutput = "0"; */
      /* double num1 = 0.0;
      double num2 = 0.0;
      String task = ""; */
    
      Widget calculatorButton(String numberText) {
        return Expanded(
          child: MaterialButton(
            onPressed: () {
              setState(() {
                numberText = output;
              });
            },
            padding: EdgeInsets.all(24),
            child: Text(
              numberText,
              style: kCalculatorStyle,
            ),
          ),
        );
      }
    
      Widget calculatorButtonSymbols(String text) {
        return Expanded(
          child: MaterialButton(
            onPressed: () {},
            padding: EdgeInsets.all(24),
            child: Text(
              text,
              style: kCalculatorStyleSymbols,
            ),
          ),
        );
      }
    
      Widget calculatorButtonTopSymbols(String text) {
        return Expanded(
          child: MaterialButton(
            onPressed: () {},
            padding: EdgeInsets.all(24),
            child: Text(
              text,
              style: kCalculatorStyleSymbols1,
            ),
          ),
        );
      }
    
      Widget calculatorResetButton() {
        return Expanded(
          child: MaterialButton(
            onPressed: () {},
            padding: EdgeInsets.all(24),
            child: Icon(
              Icons.refresh,
              size: 30,
            ),
          ),
        );
      }
    
      @override
      Widget build(BuildContext context) {
        SystemChrome.setSystemUIOverlayStyle(
          SystemUiOverlayStyle(
            statusBarColor: Colors.white10,
            statusBarIconBrightness: Brightness.dark,
          ),
        );
        return Scaffold(
          backgroundColor: Colors.white,
          body: Container(
            child: Column(
              children: [
                SafeArea(
                  child: Text(
                    output,
                    style: kCalculatorStyle,
                  ),
                ),
                Expanded(child: Divider()),
                Column(
                  children: [
                    Row(
                      children: [
                        calculatorButtonTopSymbols('AC'),
                        calculatorButtonTopSymbols('+/-'),
                        calculatorButtonTopSymbols('%'),
                        calculatorButtonSymbols('/'),
                      ],
                    ),
                    Row(
                      children: [
                        calculatorButton('7'),
                        calculatorButton('8'),
                        calculatorButton('9'),
                        calculatorButtonSymbols('x'),
                      ],
                    ),
                    Row(
                      children: [
                        calculatorButton('4'),
                        calculatorButton('5'),
                        calculatorButton('6'),
                        calculatorButtonSymbols('-'),
                      ],
                    ),
                    Row(
                      children: [
                        calculatorButton('1'),
                        calculatorButton('2'),
                        calculatorButton('3'),
                        calculatorButtonSymbols('+'),
                      ],
                    ),
                    Row(
                      children: [
                        calculatorResetButton(),
                        calculatorButton('0'),
                        calculatorButton('.'),
                        calculatorButtonSymbols('='),
                      ],
                    ),
                  ],
                ),
              ],
            ),
          ),
        );
      }
    }
import'package:calculator/constants/constants.dart';
进口“包装:颤振/材料.省道”;
导入“包:flifter/services.dart”;
void main(){
runApp(MyApp());
}
类MyApp扩展了无状态小部件{
//此小部件是应用程序的根。
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“计算器”,
主题:主题数据(
//这是应用程序的主题。
//
//尝试使用“flutter run”运行应用程序。您将看到
//应用程序有一个蓝色工具栏。然后,在不退出应用程序的情况下,重试
//将下面的primarySwatch更改为Colors.green,然后调用
//“热重新加载”(在运行“颤振运行”的控制台中按“r”,
//或者只需将更改保存到颤振IDE中的“热重新加载”。
//请注意,计数器没有重置回零;应用程序
//未重新启动。
主样本:颜色。蓝色,
),
主页:MyHomePage(),
debugShowCheckedModeBanner:false,
);
}
}
类MyHomePage扩展StatefulWidget{
@凌驾
_MyHomePageState createState()=>\u MyHomePageState();
}
类_MyHomePageState扩展状态{
字符串输出='0';
/*字符串_privateOutput=“0”*/
/*双num1=0.0;
双num2=0.0;
字符串task=“”*/
小部件计算器按钮(字符串numberText){
扩大回报(
子:材质按钮(
已按下:(){
设置状态(){
numberText=输出;
});
},
填充:边缘设置。全部(24),
子:文本(
数字文本,
风格:kCalculator风格,
),
),
);
}
小部件计算器按钮符号(字符串文本){
扩大回报(
子:材质按钮(
按下:(){},
填充:边缘设置。全部(24),
子:文本(
文本,
样式:kCalculatorStyleSymbols,
),
),
);
}
小部件计算器按钮符号(字符串文本){
扩大回报(
子:材质按钮(
按下:(){},
填充:边缘设置。全部(24),
子:文本(
文本,
样式:kCalculatorStyleSymbols1,
),
),
);
}
小部件计算器resetbutton(){
扩大回报(
子:材质按钮(
按下:(){},
填充:边缘设置。全部(24),
子:图标(
图标。刷新,
尺码:30,
),
),
);
}
@凌驾
小部件构建(构建上下文){
SystemChrome.SetSystemTimeOverlayStyle(
系统样式(
statusBarColor:Colors.white10,
Status Baricon亮度:亮度。暗,
),
);
返回脚手架(
背景颜色:Colors.white,
主体:容器(
子:列(
儿童:[
安全区(
子:文本(
产出,
风格:kCalculator风格,
),
),
已展开(子项:分隔符()),
纵队(
儿童:[
划船(
儿童:[
计算器按钮符号('AC'),
计算器按钮符号(“+/-”),
计算器按钮符号('%'),
计算器按钮符号('/'),
],
),
划船(
儿童:[
计算器按钮('7'),
计算器按钮('8'),
计算器按钮('9'),
计算器按钮符号('x'),
],
),
划船(
儿童:[
计算器按钮('4'),
计算器按钮('5'),
计算器按钮('6'),
计算器按钮符号('-'),
],
),
划船(
儿童:[
计算器按钮('1'),
计算器按钮('2'),
计算器按钮('3'),
计算器按钮符号(“+”),
],
),
划船(
儿童:[
calculatorResetButton(),
计算器按钮('0'),
计算器按钮('.'),
计算器按钮符号('='),
],
),
],
),
],
),
),
);
}
}
谁能帮帮我吗?我想知道当我按下数字键时,如何更新屏幕“输出”上的数字


请帮助我,我真的很想学习颤振

您在
calculatorButton
中的代码是反向的:

          setState(() {
            output += numberText;
          });

通过这种方式,您可以将
numberText
连接到
输出

,这很简单。试试这个代码


import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Calculator',
      theme: ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
      debugShowCheckedModeBanner: false,
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String output = '0';

  /* String _privateOutput = "0"; */
  /* double num1 = 0.0;
  double num2 = 0.0;
  String task = ""; */

  Widget calculatorButton(String numberText) {
    return Expanded(
      child: MaterialButton(
        onPressed: () {
          setState(() {
            output= numberText;
          });
        },
        padding: EdgeInsets.all(24),
        child: Text(
          numberText,
         
        ),
      ),
    );
  }

  Widget calculatorButtonSymbols(String text) {
    return Expanded(
      child: MaterialButton(
        onPressed: () {},
        padding: EdgeInsets.all(24),
        child: Text(
          text,
          
        ),
      ),
    );
  }

  Widget calculatorButtonTopSymbols(String text) {
    return Expanded(
      child: MaterialButton(
        onPressed: () {},
        padding: EdgeInsets.all(24),
        child: Text(
          text,
         
        ),
      ),
    );
  }

  Widget calculatorResetButton() {
    return Expanded(
      child: MaterialButton(
        onPressed: () {},
        padding: EdgeInsets.all(24),
        child: Icon(
          Icons.refresh,
          size: 30,
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    
    return Scaffold(
      backgroundColor: Colors.white,
      body: Container(
        child: Column(
          children: [
            SafeArea(
              child: Text(
                output,
                
              ),
            ),
            Expanded(child: Divider()),
            Column(
              children: [
                Row(
                  children: [
                    calculatorButtonTopSymbols('AC'),
                    calculatorButtonTopSymbols('+/-'),
                    calculatorButtonTopSymbols('%'),
                    calculatorButtonSymbols('/'),
                  ],
                ),
                Row(
                  children: [
                    calculatorButton('7'),
                    calculatorButton('8'),
                    calculatorButton('9'),
                    calculatorButtonSymbols('x'),
                  ],
                ),
                Row(
                  children: [
                    calculatorButton('4'),
                    calculatorButton('5'),
                    calculatorButton('6'),
                    calculatorButtonSymbols('-'),
                  ],
                ),
                Row(
                  children: [
                    calculatorButton('1'),
                    calculatorButton('2'),
                    calculatorButton('3'),
                    calculatorButtonSymbols('+'),
                  ],
                ),
                Row(
                  children: [
                    calculatorResetButton(),
                    calculatorButton('0'),
                    calculatorButton('.'),
                    calculatorButtonSymbols('='),
                  ],
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}


正如其他人所回答的,您的作业方式错误,以下是正确的版本:

onPressed: () {
  setState(() {
     output= numberText;
  });
},
但是,您需要考虑如何最好地处理不同按钮文本的不同逻辑。这是sta
onPressed: () {
  setState(() {
     output= numberText;
  });
},
  void digitPressed(String digit) {
    String newOutput;
    if (digit != '.') {
      if (output == '0') {
        newOutput = digit;
      } else {
        newOutput = output + digit;
      }
    } else if (!output.contains('.')) {
      newOutput = output + digit;
    }

    setState(() {
      output = newOutput;
    });
  }
  Widget calculatorButton(String numberText, Function digitPressed) {
    return Expanded(
      child: MaterialButton(
        onPressed: () {
          digitPressed(numberText);
        },
            Row(
              children: [
                calculatorButton('7', digitPressed),
                calculatorButton('8', digitPressed),
                calculatorButton('9', digitPressed),
                calculatorButtonSymbols('x'),
              ],
            ),