大佬教程收集整理的这篇文章主要介绍了如何在计算器应用程序颤振中添加功能,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我第一次在功能方面构建了一个 Flutter 计算器应用程序,但介于两者之间的是下面的代码
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: () {},child: Text(
text,style: kCalculatorStyleSymbols,);
}
Widget calculatorbuttontopSymbols(String text) {
return Expanded(
child: Materialbutton(
onpressed: () {},style: kCalculatorStyleSymbols1,);
}
Widget calculatorresetbutton() {
return Expanded(
child: Materialbutton(
onpressed: () {},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,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('='),);
}
}
有人可以帮我吗?我想知道如何在按下数字键时更新屏幕“OUTPUT”上的数字
请帮帮我我真的很想学Flutter!!
您在 calculatorButton
中的代码被反转:
setState(() {
output += numberText;
});
通过这种方式,您将把 numberText
连接到 output
。
很简单。试试这个代码。
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: () {},child: Text(
text,);
}
Widget calculatorButtonTopSymbols(String text) {
return Expanded(
child: MaterialButton(
onPressed: () {},);
}
Widget calculatorResetButton() {
return Expanded(
child: MaterialButton(
onPressed: () {},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('='),);
}
}
你在setState()中错了应该是这样的,output是我们更新的状态numberText是一个参数
onPressed: () {
setState(() {
output= numberText;
});
},
,
正如其他人回答的那样,您的作业是错误的,这是正确的版本:
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',calculatorButton('9',
到目前为止,我们对每个按钮都有相同的通用回调。当然,其他按钮需要不同的回调函数。
为了实现运算符,您需要记住运算符之前的先前输出。如果要实现运算符优先级,则需要一堆值。我相信你可以谷歌搜索计算器算法,祝你好运!
以上是大佬教程为你收集整理的如何在计算器应用程序颤振中添加功能全部内容,希望文章能够帮你解决如何在计算器应用程序颤振中添加功能所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。