有没有办法让颤动开关按钮继续打印,直到再次单击按钮(禁用)

is there a way to make a flutter switch button keep printing until the button is clicked again(disabeled)

我正在使用 flutter switch 插件来启用(打开)我的应用程序以继续执行任务,直到开关被禁用。 但是我一直在尝试为此实现一个功能。

下面是我执行任务的函数。但是它不起作用,(即使它开始起作用,我也无法停止它。

>    onToggle: (value) {
>           setState(() {
>           isRecording = value;
>           });
>                 while (value ==true){
>                 print("data is been recorded");
>                 
>                 }
> 
>           },

I would really appreciate any suggestion or help with this problem. thanks in advance

替换 performTask() 方法而不是 while 循环

void performTask() {
 Timer.periodic(
   const Duration(seconds: 1),
   (Timer t) => {
     isRecording == true ? print("Working Task") : t.cancel(),
   },
 );
}


----------

 onToggle: (value) {
        setState(() {
        isRecording = value;
        });
              performTask();

        },