Home > Software engineering >  Thee problem with setState(), It does not work in function
Thee problem with setState(), It does not work in function

Time:04-06

class BmiCalc extends StatefulWidget {
  const BmiCalc({Key? key}) : super(key: key);

  @override
  State<BmiCalc> createState() => _BmiCalcState();
}

class _BmiCalcState extends State<BmiCalc> {

  double _value = 150;
  int weight = 60;
  int age = 25;
  double answer = 10;
  String calc = "CALCULATE";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Color.fromARGB(255, 12, 9, 34),
      body: SafeArea(
        child: SingleChildScrollView(
          child: Column(
            children: [
              Row(
                children: [
                  FemaleBox("MALE", Icons.male),
                  FemaleBox("FEMALE", Icons.female),
                ],
              ),
              Column(children: [
                Container(
                    padding: EdgeInsets.all(32),
                    margin: EdgeInsets.all(10),
                    decoration: BoxDecoration(
                      color: _hasBeenPressedFemale
                          ? Color.fromARGB(255, 230, 72, 124)
                          : colorOfLittleBox,
                      borderRadius: BorderRadius.circular(15),
                    ),
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        const Text("HEIGHT",
                            style: TextStyle(color: Colors.grey, fontSize: 20)),
                        const SizedBox(
                          height: 10,
                        ),
                        Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            Text(_value.toStringAsFixed(0),
                                style: const TextStyle(
                                    fontSize: 45,
                                    color: Colors.white,
                                    fontWeight: FontWeight.w900)),
                            const Text(
                              "cm",
                              style:
                                  TextStyle(fontSize: 20, color: Colors.grey),
                            ),
                          ],
                        ),
                        Slider(
                          min: 100,
                          max: 230,
                          thumbColor: Colors.pink,
                          value: _value,
                          onChanged: (value) {
                            setState(() {
                              _value = value;
                            });
                          },
                        ),
                      ],
                    ))
              ]),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children:<Widget> [
                  Operation(),
                  Operation(),
                ],
              ),
              Padding(
                padding: const EdgeInsets.only(bottom: 6),
                child: Container(
                  color: Colors.pink,
                  height: 55,
                  width: MediaQuery.of(context).size.width,
                  child: TextButton(
                    child: Text(
                      calc,
                      style: const TextStyle(
                          fontSize: 22,
                          color: Colors.white,
                          fontWeight: FontWeight.w900),
                    ),
                    onPressed: () {
                      calculate();
                    },
                  ),
                ),
              )
            ],
          ),
        ),
      ),
    );
  }

  
}


Widget Operations() {
  int weight = 60;
  int age = 25;
  return   Expanded(
                    child: Container(
                        padding: EdgeInsets.all(35),
                        margin: EdgeInsets.all(10),
                        decoration: BoxDecoration(
                          color: Color.fromARGB(255, 27, 28, 48),
                          borderRadius: BorderRadius.circular(20),
                        ),
                        child: Center(
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: [
                              Text("AGE",
                                  style: TextStyle(
                                      color: Colors.grey, fontSize: 20)),
                              Text(
                                age.toString(),
                                style: TextStyle(
                                    fontSize: 40,
                                    color: Colors.white,
                                    fontWeight: FontWeight.w900),
                              ),
                              Row(
                                mainAxisAlignment: MainAxisAlignment.center,
                                children: [
                                  Container(
                                    height: 40,
                                    width: 40,
                                    decoration: BoxDecoration(
                                      border: Border.all(),
                                      borderRadius: BorderRadius.circular(45),
                                      color: Color.fromARGB(255, 76, 79, 94),
                                    ),
                                    child: IconButton(
                                      iconSize: 23,
                                      icon: Icon(FontAwesomeIcons.minus,
                                          color: Colors.white),
                                      onPressed: () {
                                        setState(() {
                                          age--;
                                        });
                                      },
                                    ),
                                  ),
                                  SizedBox(
                                    width: 5,
                                  ),
                                  Container(
                                    height: 40,
                                    width: 40,
                                    decoration: BoxDecoration(
                                      borderRadius: BorderRadius.circular(45),
                                      color: Color.fromARGB(255, 76, 79, 94),
                                    ),
                                    child: IconButton(
                                      iconSize: 23,
                                      icon: Icon(FontAwesomeIcons.plus,
                                          color: Colors.white),
                                      onPressed: () {
                                        setState(() {
                                          age  ;
                                        });
                                      },
                                    ),
                                  ),
                                ],
                              ),
                            ],
                          ),
                        )),
                  );
}

in Widget Operations(), setState() has an error: The function 'setState' isn't defined. Try importing the library that defines 'setState', correcting the name to the name of an existing function, or defining a function named 'setState'.What is the problem? How can I fix it? Without function everything is working properly, but in function sth is wrong

CodePudding user response:

I have seen your code and i have noticed that you have defined the function outside _BmiCalcState class. Define your Operations widget within the class and it should solve the problem. Alternatively you can ask for the buildcontext within the function parameters and send the context whenever you call the function it will also solve the problem

Widget Operations (BuildContext context){//and your operations here}
  • Related