0

我的应用程序利用切换按钮显示不同的 TextFormFields,使用可见性小部件,将其传递到另一个屏幕以根据切换按钮的选择进行计算。但是,当我尝试运行我的应用程序时,它似乎正在从未显示且处于非活动状态的空 TextFormFields 中寻找输入。我让应用程序运行的唯一方法是,如果我在 TextFormFields 中提供否则会被隐藏的值。有什么建议么?

class Screen2 extends StatefulWidget {
  static const String id = 'Screen2';

  @override
  _Screen2State createState() => _Screen2State();
}

class _Screen2State extends State<Screen2> {
  final TextEditingController weightController = TextEditingController();
  final TextEditingController heightController = TextEditingController();
  final MyUnit heightUnit = MyUnit();
  final MyUnit weightUnit = MyUnit(imperial: 'lbs', metric: 'kg');
  List<bool> isSelected = [
    false,
    false,
  ];

  List<bool> rRTIsSelected = [false, false, false];
  String buttonName = 'didntWork';
  String rRTButtonName = 'didntWork';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.purple,
        title: Text('Screen 2'),
      ),
      body: Center(
        child: Column(
          children: <Widget>[
            ToggleButtons(
              color: Colors.red,
              selectedColor: Colors.white,
              fillColor: Colors.black,
              children: <Widget>[
                Container(
                  padding: EdgeInsets.fromLTRB(12, 12, 12, 12),
                  constraints: BoxConstraints.expand(width: 100, height: 56),
                  child: Center(
                    child: Text(
                      'Yes',
                      style:
                          TextStyle(fontSize: 20, fontWeight: FontWeight.w500),
                    ),
                  ),
                ),
                Container(
                  padding: EdgeInsets.fromLTRB(12, 12, 12, 12),
                  constraints: BoxConstraints.expand(width: 100, height: 56),
                  child: Center(
                    child: Text(
                      'No',
                      style:
                          TextStyle(fontSize: 20, fontWeight: FontWeight.w500),
                    ),
                  ),
                ),
              ],
              isSelected: isSelected,
              onPressed: (int index) {
                setState(() {
                  for (int indexBtn = 0;
                      indexBtn < isSelected.length;
                      indexBtn++) {
                    if (indexBtn == index) {
                      isSelected[indexBtn] = !isSelected[indexBtn];
                    } else {
                      isSelected[indexBtn] = false;
                    }
                  }
                  buttonName =
                      isSelected[index] == isSelected[0] ? 'Yes' : 'No';
                });
              },
            ),
            Visibility(
              visible: isSelected[1],
              child: InputRow(
                enable: isSelected[1],
                myUnit: heightUnit,
                inputParameter: 'height',
                textField: heightController,
                colour: Colors.pink,
              ),
            ),
            Visibility(
              visible: isSelected[0],
              child: InputRow(
                enable: isSelected[0],
                myUnit: weightUnit,
                inputParameter: 'weight',
                textField: weightController,
                colour: Colors.red,
              ),
            ),
            FlatButton(
                onPressed: () {
                  CalculatorTest vancCalc;
                  vancCalc = CalculatorTest(
                    weight: double.parse(weightController.text),
                    height: double.parse(heightController.text),
                  );
                  Navigator.push(
                      context,
                      MaterialPageRoute(
                          builder: (context) => Screen1(
                                bmi: vancCalc.calculateBMI(),
                              )));
                },
                child: Text('Calculate'))
          ],
        ),
      ),
    );
  }
}
4

0 回答 0