My Flutter code is not working on the emulator . The gesture detector doesn't seem to detect the gestures

227 Views Asked by At

I've tried all sorts of things , there isn't seem to be an error in my code . I've also checked the flutter doctor , still I didn't get any results. Can anyone help me, I am a beginner at coding. I guess my set state class has some issue but nothing shows in my terminal window . Please help I'm stuck with this problem and I didn't find anything on google that could answer my question.

Here is my code

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`
                  ),
                ],
              ),
            ),
1

There are 1 best solutions below

2
On BEST ANSWER

Use setState to chagne the state

if (gender == 2) {
      if (femaleCardColor == inactiveCardColor) {
         setState((){
           femaleCardColor = activeCardColor;
         }); 
      } else {
         setState((){
        femaleCardColor = inactiveCardColor;
         }); 
      }
    }

And also remove setState from here

setState(() {
   updateColor(2);
});