我正在尝试使用复选框在Flutter中显示/隐藏2个textformfields。我尝试了以下代码,以为它将使用复选框的状态来显示字段。但这不起作用。
复选框本身确实在true和false之间切换,但是布局没有变化。
CheckboxListTile(
title: Text("Do you want to update your price?"),
controlAffinity: ListTileControlAffinity.leading,
value: priceupdate_value,
onChanged: (bool priceupdateValue) {
setState(() {
priceupdate_value = priceupdateValue;
});
if(priceupdate_value == true){
Column(
children: <Widget>[
TextFormField(
decoration: InputDecoration(
labelText: 'New Price',
),
),
TextFormField(
decoration: InputDecoration(
labelText: 'Update Other Information',
),
),
],
);
}
},
),
用可见性控件包装:
Visibility(
visible: priceupdate_value,
child: TextFormField(...),
)
并且您必须在onPressed方法外部创建列Widget(例如,将您的CheckBoxTileListWidget用列Widget包装,然后在子项下同时添加CheckBoxTileList和TextInputField)
类似
Column(
children: <Widget>[
Visibility(
visible: priceupdate_value,
child: TextFormField(
decoration: InputDecoration(
labelText: 'Update Other Information',
),
),
),
CheckBoxTileList(
title: Text("Do you want to update your price?"),
controlAffinity: ListTileControlAffinity.leading,
value: priceupdate_value,
onChanged: (bool priceupdateValue) {
setState(() {
priceupdate_value = priceupdateValue;
});
},
),
],
),