class _InputPageState extends State<InputPage>{
Gender? selectedGender;
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: mainColor,
appBar: AppBar(
title: Text(
'BMI Calculator'
),
backgroundColor: Colors.transparent,
elevation: 0,
centerTitle: true,
),
body: Column(
children: <Widget>[
Expanded(
child: Row(
children: [
Expanded(
child: GestureDetector(
child: ReusableCard(
colour: selectedGender == Gender.male ? activeCardColor : inactiveCardColor,
cardChild: IconContent(
icon: FontAwesomeIcons.mars,
label: 'MALE',
),
),
onTap: (){
setState(() {
selectedGender == Gender.male;
});
},
),
),`
`
setState() method isn’t updating the card color despite setting Gender? selectedGender to nullable, why is this happening?
I have tried changing selectedGender
to late
yet it gives the LateInitializationError
, I wanted the colour: selectedGender == Gender.male ? activeCardColor : inactiveCardColor
to set color to inactiveCardColor (this is working fine) and then setState() method should change it to active one upon Tap (that’s not working).
2
Answers
Please change
selectedGender == Gender.male
toselectedGender = Gender.male
inside the setstatePlease assign value properly