skip to Main Content
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


  1. Please change selectedGender == Gender.male to selectedGender = Gender.maleinside the setstate

    setState(() {
       selectedGender = Gender.male;
    });
    
    Login or Signup to reply.
  2. Please assign value properly

    setState(() {
                        selectedGender = Gender.male;
                      });
    
        
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search