我已经尝试了各种各样的方法,但是我的代码似乎没有错误。我还检查了扑打医生,但仍然没有任何结果。谁能帮我,我是编码的初学者。 我猜我的set状态类有问题,但是终端窗口中什么也没显示。请帮助我解决这个问题,在Google上找不到任何可以回答我问题的东西。
这是我的代码
class InputPage extends StatefulWidget {
@override
InputPageState createState() => InputPageState();
}
class InputPageState extends State<InputPage> {
Color maleCardColor = inactiveCardColor;
Color femaleCardColor = inactiveCardColor;
void updateColor ( int gender) {
// male was pressed
if (gender == 1) {
if (maleCardColor == inactiveCardColor) {
maleCardColor = activeCardColor;
} else {
maleCardColor = inactiveCardColor;
}
}
if (gender == 2) {
if (femaleCardColor == inactiveCardColor) {
femaleCardColor = activeCardColor;
} else {
femaleCardColor = inactiveCardColor;
}
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Color(0xFF10A0E21),
appBar: AppBar(
title: Text("BMI CALCULATOR",
),
backgroundColor: Colors.black,
centerTitle: true,
),
body: Column(
children: [
Expanded(
child: Row(
children: [
Expanded(
child: GestureDetector(
onTap: () {
setState(() {
updateColor(1);
});
},
child: ReusableCard(
cardColor:inactiveCardColor,
cardChild: IconContent(
iconImage: FontAwesomeIcons.mars,
iconText: "MALE",) ,
),
),
),
Expanded(
child:GestureDetector(
onTap: (){
setState(() {
updateColor(2);
});
},
child:ReusableCard(cardColor:inactiveCardColor,
cardChild:
IconContent(iconImage:FontAwesomeIcons.venus,
iconText: "FEMALE",),
),
),`enter code here`
),
],
),
),
答案 0 :(得分:0)
使用setState改变状态
if (gender == 2) {
if (femaleCardColor == inactiveCardColor) {
setState((){
femaleCardColor = activeCardColor;
});
} else {
setState((){
femaleCardColor = inactiveCardColor;
});
}
}
还要从此处删除setState
setState(() {
updateColor(2);
});