【发布时间】:2020-12-30 09:42:34
【问题描述】:
我有这个布局:
我想在单击“选择时间”时将此蓝色容器更改为另一个容器
这是单选按钮的代码:
class TimeRadioButtonsClass extends StatefulWidget {
@override
_TimeRadioButtonsClassState createState() => _TimeRadioButtonsClassState();
List<String> labels;
String picked;
Function function;
TimeRadioButtonsClass({this.picked , this.labels , this.function});
}
class _TimeRadioButtonsClassState extends State<TimeRadioButtonsClass> {
@override
Widget build(BuildContext context) {
return RadioButtonGroup(
orientation: GroupedButtonsOrientation.VERTICAL,
margin: const EdgeInsets.only(left: 12.0),
onSelected: (String selected) => setState((){
widget.picked = selected;
}),
labels: widget.labels,
picked: widget.picked,
activeColor: Color(0xffFFD243),
onChange: (String label, int index) {
print("label: $label index: $index");
widget.function(label,index);
},
);
}
}
在这里我将这个类称为CheckoutClass:
TimeRadioButtonsClass(picked: picked , labels: when),
注意:我在CheckoutClass 中使用了setState,通过在TimeRadioButtonsClass 中将其作为参数访问,它只更改了一次容器。
我不知道setState不起作用的原因!
编辑:我将函数作为参数传递给TimeRadioButtonsClass(我在上面编辑TimeRadioButtonsClass代码),这是调用代码:
TimeRadioButtonsClass(picked: picked , labels: when , function: (String label , int index){
setState(() {
if(index == 1){
indexRadio = 1;
print("indexRadio 1 : "+indexRadio.toString());
}
else{
indexRadio = 0;
print("indexRadio 2 : "+indexRadio.toString());
}
});
},),
indexRadio == 1 ? Container(
height: 50.0,
color: Colors.blue,
):
Container(
height: 50.0,
color: Colors.red,
),
【问题讨论】:
-
你的容器在哪里,你能不能也显示代码,你想改变颜色?