Let’s learn how to keep the application in flutter. You may install a plugin called
wakelock
This plugin keeps your the mobile screen awake as long as your application runs.
You can do like below
Wakelock.enable() // to start enabling awaking the screen
Wakelock.disable() // to turn off the wake lock. It means OS will shut down //the screen when necessary.
Here is an example how to use it in an app code.
class WorkoutCubit extends Cubit<WorkoutState>{
WorkoutCubit():super(const WorkoutInitial());
Timer? _timer;
onTick(Timer timer){
if(state is WorkoutInProgress){
WorkoutInProgress wip = state as WorkoutInProgress;
if(wip.elapsed!< 110){
emit(WorkoutInProgress( wip.elapsed!+1));
}else{
_timer!.cancel();
Wakelock.disable();
emit(const WorkoutInitial());
}
}
}
startWorkout( [int? index]){
Wakelock.enable();
if(index != null){
emit(const WorkoutInProgress( 0));
}else{
emit(const WorkoutInProgress( 0));
}
_timer = Timer.periodic(const Duration(seconds: 1), onTick);
}
}
See how we used Wakelock.enable() to start out timer app.
We disable it using Wakelock.disable() when our timer counts down to the time.
Flutter BLoC App