Reputation: 11
My if else code is not running how I want it to be. If the user input in something in controller 1,2,3,4 the result will be calculated and generated based on those input. However if controller 1,2,3,4 is empty the program should execute the else if statement if controller 5,6 are filled. Else lastly if all 6 inputs are empty, the program should return nothing.
import 'package:extended_math/extended_math.dart';
import 'package:flutter/material.dart';
class calculation extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _calculationState();
}
}
class _calculationState extends State<calculation> {
double Rzl = 0; // Real ZL
double Izl = 0; // Imaginary ZL
double Rzo = 0; // Real Zo
double Izo = 0; // Imaginary Zo
double t1 = 0;
double t2 = 0;
Complex zlvalue = Complex(re:0 , im:0);
Complex zovalue = Complex(re:0 , im:0);
Complex rcvalue = Complex(re:0 , im:0);
Complex infoText = Complex(re:0 , im:0);
TextEditingController controller1 = TextEditingController(text: '');
TextEditingController controller2 = TextEditingController(text: '');
TextEditingController controller3 = TextEditingController(text: '');
TextEditingController controller4 = TextEditingController(text: '');
TextEditingController controller5 = TextEditingController(text: '');
TextEditingController controller6 = TextEditingController(text: '');
@override
Widget build(BuildContext context) {
return Scaffold (
appBar: AppBar(
title: Text('Calculator'),
centerTitle: true,
),
body: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.only(left: 20.0, right: 20.0),
child: Column(
children: [
Row(
children: [
Expanded(
child: SizedBox(
height: 100,
child: Padding(
padding: const EdgeInsets.only(right: 10.0, top: 30.0),
child: TextField(
controller: controller1,
decoration: InputDecoration(
labelText: "Real ZL (ohms)",
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(20))
),
keyboardType: TextInputType.number,
),
),
),
),
Expanded(child:
SizedBox(
height: 100,
child: Padding(
padding: const EdgeInsets.only(left: 10.0, top: 30.0),
child: TextField(
controller: controller2,
decoration: InputDecoration(
labelText: "Imaginary ZL (johms)",
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(20))
),
keyboardType: TextInputType.number,
),
),
),
)
],
),
Row(
children: [
Expanded(
child: SizedBox(
height: 100,
child: Padding(
padding: const EdgeInsets.only(right: 10.0, top: 30.0),
child: TextField(
controller: controller3,
decoration: InputDecoration(
labelText: "Real Zo (ohms)",
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(20))
),
keyboardType: TextInputType.number,
),
),
),
),
Expanded(child:
SizedBox(
height: 100,
child: Padding(
padding: const EdgeInsets.only(left: 10.0, top: 30.0),
child: TextField(
controller: controller4,
decoration: InputDecoration(
labelText: "Imaginary Zo (johms)",
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(20))
),
keyboardType: TextInputType.number,
),
),
),
)
],
),
Row(
children: [
Expanded(
child: SizedBox(
height: 100,
child: Padding(
padding: const EdgeInsets.only(right: 10.0, top: 30.0),
child: TextField(
controller: controller5,
decoration: InputDecoration(
labelText: "test1",
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(20))
),
keyboardType: TextInputType.number,
),
),
),
),
Expanded(child:
SizedBox(
height: 100,
child: Padding(
padding: const EdgeInsets.only(left: 10.0, top: 30.0),
child: TextField(
controller: controller6,
decoration: InputDecoration(
labelText: "test2",
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(20))
),
keyboardType: TextInputType.number,
),
),
),
)
],
),
SizedBox(
height: 25.0,
),
ElevatedButton(
onPressed: () {
_validateUserInput(controller1.text);
_validateUserInput(controller2.text);
_validateUserInput(controller3.text);
_validateUserInput(controller4.text);
_validateUserInput(controller5.text);
_validateUserInput(controller6.text);
},
child: Text('Reflection Coefficient'),
),
SizedBox(
height: 25.0,
),
Text("Result: $infoText", style: TextStyle(color: Colors.red),
),
],
),
),
),
);
}
_validateUserInput(String input) {
if (controller1 != 0 && controller2 !=0 && controller3 !=0 && controller4 !=0 ) {
setState(() {
Rzl = double.parse(controller1.text);
Izl = double.parse(controller2.text);
Rzo = double.parse(controller3.text);
Izo = double.parse(controller4.text);
final zlvalue = Complex(re:Rzl , im:Izl) ;
final zovalue = Complex(re:Rzo , im:Izo) ;
final rcvalue = ((zlvalue)-(zovalue))/((zlvalue)+(zovalue));
infoText = rcvalue;
});
}
else if (controller5 !=0 && controller6 !=0 )
{
setState(() {
t1 = double.parse(controller5.text);
t2 = double.parse(controller6.text);
final rc1 = Complex(re:t1 , im:t2) ;
final rc2 = Complex(re:t1 , im:t2) ;
final rcvalue = rc1 + rc2;
infoText = rcvalue;
});
}
else
{
setState(() {
return;
});
}
}
}
Upvotes: 0
Views: 225
Reputation: 23134
A check on controller1 != 0
doesn't make much sense. The controllers are never 0
. You would want to check on controller1.text != ''
for example, or even better just check on controller1.text.isNotEmpty
Upvotes: 2