Home > database >  Why setState is not working, is there any mistake in my code?
Why setState is not working, is there any mistake in my code?

Time:12-22

I tried to show a circular progress indicator while loading when clicked on the elevated button here I used setState to update the state, but somehow state is not getting updated. I didn't understand why it's not getting updated any mistake in the below code? is there something that I can improve my code please help me out thanks in Advance.

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  final String title;

  const MyHomePage({
    Key? key,
    required this.title,
  }) : super(key: key);

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
 
  bool isLoading = false;


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: ElevatedButton(
                    onPressed: () async {
                  setState(() {
                    isLoading == true;
                  });
                  await Future.delayed(const Duration(seconds: 5));
                  setState(() {
                    isLoading = false;
                  });
               
                },
                  child: FittedBox(
                  child: isLoading 
                      ? CircularProgressIndicator(
                    color: Colors.white,
                  )
                      : Row(
                    children: const [
                      Text(
                        "Submit",
                      ),
                      SizedBox(
                        width: 2,
                      ),
                      Icon(Icons.check_circle),
                    ],
                  ),
                ),
                  style: ElevatedButton.styleFrom(
                    primary: Colors.yellow,
                    minimumSize: const Size(100, 40),
                    elevation: 3,
                  )
                ),
      ),
      
    );
  }
}

CodePudding user response:

Brother you make small mistake in

    setstate(){
IsLoding = !isloading;
    }

You write double equalto (==) insted of one

So it's better to write reverse condition i write above. Let me know if you still facing issue

Try to print isloading on every click

CodePudding user response:

Just change == to = in set state

onPressed: () async {
              setState(() {
                isLoading == true; -----> isLoading = true;
              });
              await Future.delayed(const Duration(seconds: 5));
              setState(() {
                isLoading = false;
              });
           
            },
  • Related