【问题标题】:How to start/stop inactivity timer in Flutter如何在 Flutter 中启动/停止不活动计时器
【发布时间】:2020-07-28 14:43:09
【问题描述】:

有没有办法在函数执行时启动 5 秒或 X 秒的不活动计时器?

这是我正在做的事情:

我正在创建一个屏幕截图:

     //todo: I need an activity timer [start] here

                   screenshotController.capture(
                    //delay: Duration(seconds: 5),
                      pixelRatio: 2,
                      path: newPath
                  ).then((io.File image) {
                    //Capture Done
                    _imageFile = image;

                  }).catchError((onError) {
                    print(onError);
                  });

   // todo: I need an activity timer [stop] here. 

用户不能中断 PNG 文件的创建。我需要某种进度计时器来启动/停止。我不想使用 Progress_HUD。这是非常丑陋的。我试过了。我必须更改我的整个代码以适应这个应用程序的工作方式。

我倾向于使用 CircularProgress..().. 但我怎样才能让它开始呢?我怎样才能让它停下来??

我如何知道 screenshotController 何时仍处于活动状态?

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    我对截图插件了解不多,但如果你只想每 x 秒运行一个函数,你可以使用 Timer

    在initState()中

      void initState() {
        super.initState();
        Timer.periodic(Duration(seconds: 1), (_) {
    
          //Write here your function and logic
    
        });
      }
    

    如果您的函数是异步的,请将计时器放入 didChangeDependencies(), 您可以通过编写一些逻辑来停止计时器

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多