Home > OS >  CircularProgressIndicator not stopping while moving back
CircularProgressIndicator not stopping while moving back

Time:08-03

Created a demo for understanding future..

I have taken two screen....Using Future.delayed I am forwarding to next screen in 5 seconds with CircularProgressIndicator...but when I move to back..it still showing CircularProgressbar...

What should I correct my code?

here is my code


class _MyHomePageState extends State<MyHomePage> {
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  bool isloading = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Async demo'),
      ),
      body: isloading == true
          ? Center(child: CircularProgressIndicator())
          : Center(
              child: TextButton(
                child: Text('Pressed'),
                onPressed: () {
                  setState(() {
                    isloading = true;
                  });

                  Future.delayed(Duration(seconds: 5), () {
                    Navigator.of(context)
                        .push(MaterialPageRoute(builder: (context) {
                      return NextScreen();
                    }));

                    isloading = false;
                  });
                },
              ),
            ),
    );
  }
}

CodePudding user response:

What should I correct my code?

Wrap the second assignment of isloading to false inside setState (just as you did when setting to true). Something like the following:

onPressed: () {
  setState(() {
    isloading = true;
  });
                  
 Future.delayed(Duration(seconds: 5), () {
   Navigator.of(context).push(MaterialPageRoute(builder: (context) {
    return NextScreen();
  }));

    setState(() {
      isloading = false;
    });
  });
},

CodePudding user response:

you need to use setState when you are assign variable = false

For that do as below

replace this

isloading = false; 

with

 setState(() {
           isloading = false; 
    });
  • Related