我有三个文本字段,想在其中的前两个之间进行选择,并使用第三个文本字段



我有三个文本字段。我想在前两个之间做出选择,并与第三个一起工作。

怎么办?请指导

import 'package:flutter/material.dart';
void main() => runApp(new CalculatorApp());
class CalculatorApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(title:'Bending Calculator',
home: Calculator()
);
}
}
class Calculator extends StatefulWidget {
@override
State<StatefulWidget> createState() => Calculatore();
}
class Calculatore extends State<Calculator> {
final a = TextEditingController();
final b = TextEditingController();
final c = TextEditingController();
// controller mentioned
String total= "";
void calculate()  {
int numberA = int.parse(a.text);
int numberB = int.parse(b.text);
int numberC = int.parse(c.text);
int  result;
// if numberA have value then answer will be a+c

if( 
// what condition to do here for between choosing between  textfields a or b.
// i tried numberB ==null   that does not work
// very much confused, no idea what to do please help
){
result = numberA + numberC
} else{ result = numberB + numberC
}

setState(() {
total = "$result";
});
}
@override
Widget build(BuildContext context) {
return Scaffold(appBar: AppBar(title:Text("Calculator")),
body: SafeArea(
child: ListView(
children: <Widget>[
Row( mainAxisAlignment: MainAxisAlignment.center ,
children: <Widget>[
// first textfield
Container(width: MediaQuery.of(context).size.width *0.45 ,height: 50,
child: TextField(
controller: a,
decoration: InputDecoration(hintText: " Enter a value"),
keyboardType: TextInputType.number),
),
Container(width: MediaQuery.of(context).size.width * 0.04,height: 50,),
// second textfield
Container(width: MediaQuery.of(context).size.width* 0.45,height: 50,
child: TextField(
controller: b,
decoration: InputDecoration(hintText: " Enter  b value"),
keyboardType: TextInputType.number),), ],
),

Row(mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
// third textfield
Container(width: MediaQuery.of(context).size.width *0.9, height: 50,
child: TextField(
controller: c,
decoration: InputDecoration(hintText: "   Enter c value"),
keyboardType: TextInputType.number),),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
//button
RaisedButton(
onPressed: calculate,
child: Text('Calculate'),),
],
),
Text("   Total : $total", style: TextStyle(fontSize: 20.0),),
],
))
);
}
}

根据您的评论,这也许会有所帮助。如果您提供的要解析的文本为空,正常int.parse将引发错误。请改用此处文档中的int.tryParse。如果提供的字符串为空,这将返回一个null。真人版在这个飞镖垫中可用。

int numberA = int.tryParse(a.text);
int numberB = int.tryParse(b.text);
int numberC = int.tryParse(c.text);
int result;
// if numberA have value then answer will be a+c
// Note following conditions have order precedence. So only one of them execute.     
if (numberA != null && numberC != null) {
result = numberA + numberC;
} else if (numberB != null && numberC != null){
result = numberB + numberC;
} 

如果要计算所有三个字段都存在的托塔特。使用以下条件。

if (numberA != null && numberB != null && numberC != null) {
result = numberA + numberB + numberC;
} else if (numberA != null && numberC != null) {
result = numberA + numberC;
} else if (numberB != null && numberC != null){
result = numberB + numberC;
} 

您可以通过检查文本而不是解析的数字来实现相同的目的。

据我了解,您已经尝试检查文本字段 b 的值是否为空。您是否尝试过检查是b.text.isEmpty还是isNotEmpty.

" 无法解析为 int。 您可以使用"try & catch"或"tryParese"。 怎么样?

void calculate() {
int numberA = int.tryParse(a.text);
int numberB = int.tryParse(b.text);
int numberC = int.tryParse(c.text);
int result;
if (numberB == null) {
result = numberA + numberC;
} else {
result = numberB + numberC;
}
setState(() {
total = "$result";
});
}

最新更新