Flutter计算器问题

7hiiyaii  于 2023-03-31  发布在  Flutter
关注(0)|答案(3)|浏览(149)

我如何在Flutter中解决这个问题,以及在Native Android中id的相同之处是什么?我已经制作了一个计算器,但结果不会显示在文本小部件中。在按下其中一个算术运算符后
它应该在文本“null”上显示结果,
但什么都没发生。
我已经附加了一个图像的problem

这是代码

import 'package:flutter/material.dart';
import 'dart:math';

void main() {
runApp(MaterialApp(home: Calculator()));
}

class Calculator extends StatefulWidget {
@override
_CalculatorState createState() => _CalculatorState();
}

class _CalculatorState extends State<Calculator> {
var theResult, firstNum, secondNum;
void divideTheNums() {
setState(() {
theResult = firstNum / secondNum;
});
}

void multiplyTheNums() {
setState(() {
theResult = firstNum * secondNum;
});
}

void addTheNums() {
setState(() {
theResult = firstNum + secondNum;
});
}

void subtractTheNums() {
setState(() {
theResult = firstNum - secondNum;
});
}

Widget build(BuildContext context) {
return new Scaffold(
resizeToAvoidBottomPadding: false,
appBar: AppBar(
backgroundColor: Colors.redAccent,
centerTitle: true,
title: Text(
"Calculator",
),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Container(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
"OutPut:",
style: TextStyle(
fontSize: 25.0, fontWeight: FontWeight.w700),
),
Text(theResult.toString()),
Container(
margin: EdgeInsets.only(bottom: 30.0), child: Text(""))
],
),
Container(
margin: EdgeInsets.only(bottom: 20.0),
child: TextField(
controller: TextEditingController(),
decoration: InputDecoration(
hintText: "Enter First Number:",
border: OutlineInputBorder()),
),
),
TextField(
controller: TextEditingController(),
decoration: InputDecoration(
hintText: "Enter Second Number:",
border: OutlineInputBorder()),
), Row(mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Container(
margin: EdgeInsets.only(right: 20.0),
child: RaisedButton(
color: Colors.redAccent,
onPressed: divideTheNums,
child: Text(
"/",
style: TextStyle(color: Colors.white, fontSize: 23.0),
),
),
),
Container(
margin: EdgeInsets.only(),
child: RaisedButton(
color: Colors.redAccent,
onPressed: multiplyTheNums,
child: Text(
"*",
style: TextStyle(color: Colors.white, fontSize: 23.0),
),
),
)
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Container(
margin: EdgeInsets.only(right: 20.0, top: 20.0),
child: RaisedButton(
color: Colors.redAccent,
highlightColor: Colors.white,
onPressed: subtractTheNums,
child: Text(
"-",
style: TextStyle(color: Colors.white, fontSize: 23.0),
),
),
),
Container(
margin: EdgeInsets.only(top: 20.0),
child: RaisedButton(
color: Colors.redAccent,
onPressed: addTheNums,
child: Text(
"+",
style: TextStyle(color: Colors.white, fontSize: 23.0),
),
),
)
],
)
],
),
),
),
),
);
}
}
eblbsuwk

eblbsuwk1#

问题是你没有在任何地方接收到**TextField的值,并且你正在分配你的第一个和第二个值的值,这是默认的null。
要解决这个问题,你可以使用
FormTextFormField**来做到这一点。我认为下面的代码可以帮助你明确你的想法。
你也可以在TextFormField中使用有效性验证,或者检查它是否为null。

import 'package:flutter/material.dart';
import 'dart:math';

void main() {
  runApp(MaterialApp(home: Calculator()));
}

class Calculator extends StatefulWidget {
  @override
  _CalculatorState createState() => _CalculatorState();
}

class _CalculatorState extends State<Calculator> {
  double theResult, firstNum, secondNum;
  final _form =  GlobalKey<FormState>();

  void divideTheNums() {
    _form.currentState.save();
    setState(() {
      theResult = firstNum / secondNum;
    });
  }

  void multiplyTheNums() {
    _form.currentState.save();
    setState(() {
      theResult = firstNum * secondNum;
    });
  }

  void addTheNums() {
    _form.currentState.save();
    setState(() {
      theResult = firstNum + secondNum;
    });
  }

  void subtractTheNums() {
    _form.currentState.save();
    setState(() {
      theResult = firstNum - secondNum;
    });
  }

  Widget build(BuildContext context) {
    return new Scaffold(
      resizeToAvoidBottomPadding: false,
      appBar: AppBar(
        backgroundColor: Colors.redAccent,
        centerTitle: true,
        title: Text(
          "Calculator",
        ),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Container(
          child: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    Text(
                      "OutPut:",
                      style: TextStyle(
                          fontSize: 25.0, fontWeight: FontWeight.w700),
                    ),
                    Text(theResult==null?"":theResult.toString()),
                    Container(
                        margin: EdgeInsets.only(bottom: 30.0), child: Text(""))
                  ],
                ),
                Form(
                  key: _form,
                  child: Container(
                    margin: EdgeInsets.only(bottom: 20.0),
                    child: Column(
                      children: <Widget>[
                        TextFormField(
                          controller: TextEditingController(),
                          onSaved: (value){
                            firstNum = double.parse(value);
                          },
                          keyboardType: TextInputType.numberWithOptions(),
                          decoration: InputDecoration(
                              hintText: "Enter First Number:",
                              border: OutlineInputBorder()),
                        ),
                        Padding(padding: EdgeInsets.only(top: 10.0),),
                        TextFormField(
                          controller: TextEditingController(),
                          onSaved: (value){
                            secondNum = double.parse(value);
                          },
                          keyboardType: TextInputType.numberWithOptions(),
                          decoration: InputDecoration(
                              hintText: "Enter Second Number:",
                              border: OutlineInputBorder()),
                        ),
                      ],
                    ),
                  ),
                ),
                Row(mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: <Widget>[
                    Container(
                      margin: EdgeInsets.only(right: 20.0),
                      child: RaisedButton(
                        color: Colors.redAccent,
                        onPressed: divideTheNums,
                        child: Text(
                          "/",
                          style: TextStyle(color: Colors.white, fontSize: 23.0),
                        ),
                      ),
                    ),
                    Container(
                      margin: EdgeInsets.only(),
                      child: RaisedButton(
                        color: Colors.redAccent,
                        onPressed: multiplyTheNums,
                        child: Text(
                          "*",
                          style: TextStyle(color: Colors.white, fontSize: 23.0),
                        ),
                      ),
                    )
                  ],
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: <Widget>[
                    Container(
                      margin: EdgeInsets.only(right: 20.0, top: 20.0),
                      child: RaisedButton(
                        color: Colors.redAccent,
                        highlightColor: Colors.white,
                        onPressed: subtractTheNums,
                        child: Text(
                          "-",
                          style: TextStyle(color: Colors.white, fontSize: 23.0),
                        ),
                      ),
                    ),
                    Container(
                      margin: EdgeInsets.only(top: 20.0),
                      child: RaisedButton(
                        color: Colors.redAccent,
                        onPressed: addTheNums,
                        child: Text(
                          "+",
                          style: TextStyle(color: Colors.white, fontSize: 23.0),
                        ),
                      ),
                    )
                  ],
                )
              ],
            ),
          ),
        ),
      ),
    );
  }
}
wfypjpf4

wfypjpf42#

对不同的用户输入使用不同的TextEditingController

import 'package:flutter/material.dart';
import 'dart:math';

void main() {
  runApp(MaterialApp(home: Calculator()));
}

class Calculator extends StatefulWidget {
  @override
  _CalculatorState createState() => _CalculatorState();
}

class _CalculatorState extends State<Calculator> {
  TextEditingController first = TextEditingController();
  TextEditingController second = TextEditingController();
  double theResult=0, firstNum=0, secondNum=0;

  void divideTheNums() {
   setState(() {
     firstNum = double.parse(first.text);
     secondNum = double.parse(second.text);
     theResult = firstNum / secondNum;
   });
  }

  void multiplyTheNums() {
    setState(() {
      firstNum = double.parse(first.text);
      secondNum = double.parse(second.text);
      theResult = firstNum * secondNum;
    });
  }

  void addTheNums() {
   setState(() {
     firstNum = double.parse(first.text);
     secondNum = double.parse(second.text);
     theResult = firstNum + secondNum;
   });
  }

  void subtractTheNums() {
   setState(() {
    firstNum = double.parse(first.text);
    secondNum = double.parse(second.text);
    theResult = firstNum - secondNum;
   });
  }

 Widget build(BuildContext context) {
   return new Scaffold(
     resizeToAvoidBottomPadding: false,
     appBar: AppBar(
     backgroundColor: Colors.redAccent,
      centerTitle: true,
      title: Text(
       "Calculator",
      ),
  ),
  body: Padding(
    padding: const EdgeInsets.all(16.0),
    child: Container(
      child: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Text(
                  "OutPut:",
                  style: TextStyle(
                      fontSize: 25.0, fontWeight: FontWeight.w700),
                ),
                Text(theResult.toString()),
                Container(
                    margin: EdgeInsets.only(bottom: 30.0), child: Text(""))
              ],
            ),
            Container(
              margin: EdgeInsets.only(bottom: 20.0),
              child: TextField(
                controller: first,
                decoration: InputDecoration(
                    hintText: "Enter First Number:",
                    border: OutlineInputBorder()),
              ),
            ),
            TextField(
              controller: second,
              decoration: InputDecoration(
                  hintText: "Enter Second Number:",
                  border: OutlineInputBorder()),
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: <Widget>[
                Container(
                  margin: EdgeInsets.only(right: 20.0),
                  child: RaisedButton(
                    color: Colors.redAccent,
                    onPressed: divideTheNums,
                    child: Text(
                      "/",
                      style: TextStyle(color: Colors.white, fontSize: 23.0),
                    ),
                  ),
                ),
                Container(
                  margin: EdgeInsets.only(),
                  child: RaisedButton(
                    color: Colors.redAccent,
                    onPressed: multiplyTheNums,
                    child: Text(
                      "*",
                      style: TextStyle(color: Colors.white, fontSize: 23.0),
                    ),
                  ),
                )
              ],
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: <Widget>[
                Container(
                  margin: EdgeInsets.only(right: 20.0, top: 20.0),
                  child: RaisedButton(
                    color: Colors.redAccent,
                    highlightColor: Colors.white,
                    onPressed: subtractTheNums,
                    child: Text(
                      "-",
                      style: TextStyle(color: Colors.white, fontSize: 23.0),
                    ),
                  ),
                ),
                Container(
                  margin: EdgeInsets.only(top: 20.0),
                  child: RaisedButton(
                    color: Colors.redAccent,
                    onPressed: addTheNums,
                    child: Text(
                      "+",
                      style: TextStyle(color: Colors.white, fontSize: 23.0),
                    ),
                  ),
                )
              ],
            )
          ],
        ),
      ),
    ),
  ),
);
}
}
epggiuax

epggiuax3#

大家好,我想分享我的代码扑计算器希望你们了解它. enter link description here

相关问题