颤动中的定时器控制

Timer control in flutter

我试图在 2 秒后设置一个布尔值 false (showSpinner),但无法处理它,主要思想是在显示解决方案之后显示加载微调器 2 秒,但微调器永远不会停止,解决方案也永远不会出现(微调器正在加载微调器,文本 = 解决方案)

@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,
        );
      }

有3个按钮(isPressed1表示按钮1,isPressed2表示按钮2,isPressd3表示按钮3,如果全部为true则isPressed4变为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),
        );

我不知道什么是 spinkitisPressed4,但我会这样做:

  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',
    );
  }

更新:

最初我有2秒。那么:

  • 如果 showSpinner == falsefloatingActionButton1 没有被按下,那么我得到文本 The time is up!.
  • 如果 showSpinner == truefloatingActionButton1 没有被按下,那么我得到文本 Button 4 is not pressed yet.
  • 如果 showSpinner == truefloatingActionButton1 被按下,那么我得到 CircularProgressIndicator()(如问题所示)。
  • 如果 showSpinner == falsefloatingActionButton1 被按下,那么我得到 TextThe answer(如问题所示):
  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,
    );
  }

void startTimer() {
      const oneSec = const Duration(seconds: 1);
      _timer = new Timer.periodic(
        oneSec,
        (Timer timer) => setState(
          () {
            if (_start ==0) {
              showSpinner=false;
              timer.cancel();
            } else {
              _start = _start - 1;
            }
          },
        ),
      );
    }

    @override
    void dispose() {
      _timer.cancel();
      super.dispose();
    }

通过创建此功能并修改按钮解决了问题;

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