How to automatically ontap in flutter

Viewed 51

Please help i have a video recording app which starts with a tap and a timer is also started with the same tap and after another tap it stops and timer reset . I want to stop and reset timer when the timer reaches 15 second how to do this please help

 GestureDetector(
                    onTap: 
                    () async {
                      if (isRecoring) {
                        stop();
                        XFile videopath =
                            await _cameraController.stopVideoRecording();
                        setState(() {
                          isRecoring = false;

                         
                          Navigator.push(
                              context,
                              MaterialPageRoute(
                                  builder: (builder) => VideoViewPage(
                                        path: videopath.path,
                                      )));
                        }
                        
                        );
                      } else {
                        startTime();
                        await _cameraController.startVideoRecording();

                        setState(() {
                          isRecoring = true;
                          // ignore: use_build_context_synchronously
                        });
                      }
                    },`

i tried adding logical or like this ` if (isRecoring || timer == '15') {stop} but didnt work. create a diffrent function with a if condition for stop even that didnt work

1 Answers

Firstly you need to define a Duration variable and a Timer to track the timer duration:

var videoDuration = const Duration();
Timer? autoStopRecordingTimer;

Then make a function which will stop the video recording:

void stopRecording()async{
      stop();
      XFile videopath = await _cameraController.stopVideoRecording();
      setState(() {
          isRecoring = false;
          Navigator.push(
            context,
            MaterialPageRoute(
               builder: (builder) => VideoViewPage(
                     path: videopath.path,
                     )));
                  }
                        
              );
}

When starting video recording, start the timer as well. Which will stop the recording if it has been 15 or more seconds:

autoStopRecordingTimer = Timer.periodic(const Duration(seconds: 1), (timer) async {
          if(videoDuration.inSeconds>=15){
            autoStopRecordingTimer?.cancel();
            videoDuration = const Duration();
            stopRecording();
          } else {
            videoDuration+=const Duration(seconds: 1);
          }
      });
Related