flutterflutter-debug

setstate is not answering when i prees the elvated button


seState should answer when user press the button....

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'BMI APP',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.blueAccent),
        useMaterial3: true,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  var whcontroller = TextEditingController();

  var ftcontroller = TextEditingController();

  var incontroller = TextEditingController();

  var result = "";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          backgroundColor: Theme.of(context).colorScheme.inversePrimary,
          title: Text("BMI App"),
        ),
        body: Center(
          child: Container(
            width: 300,
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Text(
                  "BMI APP",
                  style: TextStyle(
                      fontSize: 22,
                      fontWeight: FontWeight.w700,
                      color: Colors.blueAccent.shade700),
                ),
                SizedBox(
                  height: 12,
                ),
                TextField(
                  controller: whcontroller,
                  decoration: InputDecoration(
                    label: Text("input you weight in kg"),
                    prefixIcon: Icon(Icons.line_weight),
                  ),
                  keyboardType: TextInputType.number,
                ),
                SizedBox(
                  height: 12,
                ),
                TextField(
                  controller: ftcontroller,
                  decoration: InputDecoration(
                    label: Text("input your height in feet"),
                    prefixIcon: Icon(Icons.height),
                  ),
                  keyboardType: TextInputType.number,
                ),
                SizedBox(
                  height: 12,
                ),
                TextField(
                  controller: incontroller,
                  decoration: InputDecoration(
                      label: Text("input your height in inches"),
                      prefixIcon: Icon(Icons.height)),
                  keyboardType: TextInputType.number,
                ),
                SizedBox(
                  height: 12,
                ),
                ElevatedButton(
                    onPressed: () {
                      var wh = whcontroller.text.toString();
                      var ft = ftcontroller.text.toString();
                      var inc = incontroller.text.toString();

                      if (whcontroller != "" &&
                          ftcontroller != "" &&
                          incontroller != "") {
                        //logic
                        setState(() {
                          //setstate
                        });
                      } else {
                        setState(() {
                          result = "please fill";
                        });
                      }
                    },
                    child: Text("Check")),
                SizedBox(
                  height: 15,
                ),
                Text(" ${result}")
              ],
            ),
          ),
        ));
  }
}

Solution

  • Check your onPressed function code:

    onPressed: () {
         var wh = whcontroller.text.toString();
         var ft = ftcontroller.text.toString();
         var inc = incontroller.text.toString();
    
         if (whcontroller.text != "" &&
             ftcontroller.text != "" &&
             incontroller.text != "") {
             //logic
             setState(() {
               //setstate
                result = "";
            });
        } else {
          setState(() {
           result = "please fill";
          });
        }
    },