if else函数在Flutter上不起作用

km0tfn4u  于 2022-12-14  发布在  Flutter
关注(0)|答案(1)|浏览(199)

我的if else代码没有按照我希望的方式运行。如果用户在控制器1、2、3、4中输入了一些内容,结果将根据这些输入进行计算和生成。但是,如果控制器1、2、3、4为空,则程序应该在控制器5、6为空的情况下执行else if语句。否则,最后如果所有6个输入都为空,则程序应该不返回任何结果。

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;
      });
    }

  }
}

controller 1: Real ZL controller 2 : Imaginary ZL controller 3 :Real Zo controller 4 :Imaginary Zo controller 5 : test1 controller 6 : test2

e4yzc0pl

e4yzc0pl1#

检查controller1 != 0没有多大意义。控制器从来都不是0。例如,您可能希望检查controller1.text != '',或者更好的方法是检查controller1.text.isNotEmpty

相关问题