I'm trying to enable the functionality of changing the color of the icon once it is pressed, but the GestureDetector
is not working as expected. How do I fix this?
Color maleCardColor = inactiveCardColor;
Color femaleCardColor = inactiveCardColor;
void updateColor(int i) {
if (int == 1) {
if (maleCardColor == inactiveCardColor) {
maleCardColor == activeCardColor;
}
} else {
maleCardColor == inactiveCardColor;
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
body: Column(
children: [
Expanded(
child: Row(
children: [
Expanded(
child: GestureDetector(
onTap: () {
setState(() {
updateColor(1);
});
},
child: Contain(
c: maleCardColor,
w: IconWidget(
i: FontAwesomeIcons.mars,
s: 'MALE',
),
),
),
),
Expanded(
child: Contain(
c: femaleCardColor,
w: IconWidget(
i: FontAwesomeIcons.venus,
s: 'FEMALE',
),
),
)
],
),
),
],
),
),
);
}
Change your updateColor() to this.
void updateColor(int i) {
if (i == 1) {
if (maleCardColor == inactiveCardColor) {
maleCardColor = activeCardColor;
}
} else {
maleCardColor = inactiveCardColor;
}
}
and if you want single selected colour. male or female, then do it like this and add GestureDetector in female Icon Widget and pass updatColor(), any value except 1.
void updateColor(int i) {
if (i == 1) {
if (maleCardColor == inactiveCardColor) {
maleCardColor = activeCardColor;
femaleCardColor = inactiveCardColor;
}
} else {
femaleCardColor = activeCardColor;
maleCardColor = inactiveCardColor;
}
}