Search code examples
flutterdartdart-async

Timer control in flutter


I'm trying to set one boolean false after 2 seconds(showSpinner) but can't handle it, main idea is showing loading spinner for 2 seconds after than showing solution but spinner never stops and solution never shows up(spinner is loading spinner, text = solution) enter image description here

@override
  void initState(){
    super.initState();
    showSpinner=true;

      Timer(Duration(seconds: 2),(){
        setState(() {
          showSpinner=false;
        });
      });

  }

Widget build(BuildContext context) {


     Widget child;
      if (showSpinner == true && isPressed4 == true) {
        setState(() {
          Timer(Duration(seconds: 2), () {
            showSpinner == false;
          });
        });

        child = spinkit;
      }

      if (showSpinner == false && isPressed4 == true) {
        text = simpleInterest.accumulationFunction(
            time, yearlySimpleInterestRate, principal);
        child = Text(
          text,
          style: TextStyle(fontSize: 18),
          textAlign: TextAlign.center,
        );
      }

there are 3 buttons(isPressed1 for button 1 and isPressed2 for button2 and isPressd3 for button 3, if all true isPressed4 becomes true)

 var floatingActionButton1 = FloatingActionButton(
          onPressed: () {
            setState(() {
              isPressed1 = !isPressed1;
            });
            setState(() {
              principal = double.parse(_principalController.text);
            });
            setState(() {
              if (isPressed3 == true && isPressed2 == true && isPressed1 == true) {
                isPressed4 = true;
              }
            });
          },
          elevation: 40,
          backgroundColor: isPressed1 ? Colors.lightGreenAccent : null,
          heroTag: "btn1",
          child: Icon(Icons.check),
        );

Solution

  • I don't know what is spinkit and isPressed4, but I would do it like this:

      bool showSpinner;
    
      @override
      void initState() {
        super.initState();
        showSpinner = true;
    
        Timer(Duration(seconds: 2), () {
          setState(() {
            showSpinner = false;
          });
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
            appBar: AppBar(
              title: Text('Title'),
            ),
            body: buildBody()
        );
      }
    
      Widget buildBody(){
        return showSpinner ?
        CircularProgressIndicator() :
        Text(
          'The answer',
        );
      }
    

    UPD:

    Initially I have 2 seconds. Then:

    • If showSpinner == false and floatingActionButton1 has not been pressed, then I get text The time is up!.
    • If showSpinner == true and floatingActionButton1 has not been pressed, then I get text Button 4 is not pressed yet.
    • If showSpinner == true and floatingActionButton1 has been pressed, then I get CircularProgressIndicator()(as in the question).
    • If showSpinner == false and floatingActionButton1 has been pressed, then I get Text with The answer(as in the question):
      bool showSpinner;
    
      var floatingActionButton1;
    
      bool isPressed1;
      bool isPressed2;
      bool isPressed3;
      bool isPressed4;
    
      @override
      void initState() {
        super.initState();
        showSpinner = true;
    
        isPressed1 = false;
        isPressed2 = true;
        isPressed3 = true;
        isPressed4 = false;
    
        floatingActionButton1 = FloatingActionButton(
          onPressed: () {
            setState(() {
              isPressed1 = !isPressed1;
    
              if (isPressed3 == true && isPressed2 == true && isPressed1 == true) {
                isPressed4 = true;
              }
            });
          },
          backgroundColor: isPressed1 ? Colors.lightGreenAccent : null,
          child: Icon(Icons.check),
        );
    
        Timer(Duration(seconds: 2), () {
          setState(() {
            showSpinner = false;
          });
        });
      }
    
    
      Widget build(BuildContext context) {
        Widget child;
    
        if(showSpinner == false && isPressed4 == false)
          child = Text('The time is up!');
    
        else if(showSpinner == true && isPressed4 == false)
          child = Text('Button 4 is not pressed yet');
    
        else if (showSpinner == true && isPressed4 == true) {
          child = CircularProgressIndicator();
        }
    
        else if (showSpinner == false && isPressed4 == true) {
          child = Text(
            'The answer',
            style: TextStyle(fontSize: 18),
            textAlign: TextAlign.center,
          );
        }
    
        return Scaffold(
            appBar: AppBar(
              title: Text('Title'),
            ),
            body: child,
          floatingActionButton: floatingActionButton1,
        );
      }