Dart 切换按钮并指定TextEditingController在颤振中所需的输入
我的应用程序使用切换按钮来显示不同的TextFormFields,使用visibility小部件,这些字段被传递到另一个屏幕,以根据切换按钮的选择执行计算。然而,当我尝试运行我的应用程序时,它似乎在寻找未显示且处于非活动状态的空TextFormFields的输入。让应用程序运行的唯一方法是在TextFormFields中提供值,否则这些值将被隐藏。有什么建议吗Dart 切换按钮并指定TextEditingController在颤振中所需的输入,dart,visibility,togglebutton,textformfield,Dart,Visibility,Togglebutton,Textformfield,我的应用程序使用切换按钮来显示不同的TextFormFields,使用visibility小部件,这些字段被传递到另一个屏幕,以根据切换按钮的选择执行计算。然而,当我尝试运行我的应用程序时,它似乎在寻找未显示且处于非活动状态的空TextFormFields的输入。让应用程序运行的唯一方法是在TextFormFields中提供值,否则这些值将被隐藏。有什么建议吗 class Screen2 extends StatefulWidget { static const String id = 'S
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'))
],
),
),
);
}
}
class Screen2扩展了StatefulWidget{
静态常量字符串id='Screen2';
@凌驾
_Screen2State createState()=>u Screen2State();
}
类2状态扩展状态{
最终文本编辑控制器权重控制器=文本编辑控制器();
最终文本编辑控制器高度控制器=文本编辑控制器();
最终MyUnit高度单位=MyUnit();
最终MyUnit weightUnit=MyUnit(英制:'lbs',公制:'kg');
所选名单=[
假,,
假,,
];
所选列表=[false,false,false];
字符串buttonName='didntWork';
字符串rRTButtonName='didntWork';
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
背景颜色:Colors.purple,
标题:文本(“屏幕2”),
),
正文:中(
子:列(
儿童:[
切换按钮(
颜色:颜色,红色,
selectedColor:Colors.white,
fillColor:Colors.black,
儿童:[
容器(
填充:从LTRB(12,12,12,12)开始的边缘设置,
约束:BoxConstraints.展开(宽度:100,高度:56),
儿童:中心(
子:文本(
“是的”,
风格:
TextStyle(fontSize:20,fontWeight:fontWeight.w500),
),
),
),
容器(
填充:从LTRB(12,12,12,12)开始的边缘设置,
约束:BoxConstraints.展开(宽度:100,高度:56),
儿童:中心(
子:文本(
“不”,
风格:
TextStyle(fontSize:20,fontWeight:fontWeight.w500),
),
),
),
],
当选:当选,
onPressed:(int索引){
设置状态(){
对于(int indexBtn=0;
indexBtn屏幕1(
bmi:vancCalc.calculateBMI(),
)));
},
子项:文本('Calculate'))
],
),
),
);
}
}