How to keep application awake in flutter?
This package does the work https://pub.dev/packages/wakelock
It depends on Flutter Wakelock class.
Permissions The wakelock plugin does not require any permissions on any platform. This is because it only enables the screen wakelock and not any partial (CPU) wakelocks that would keep the app alive in the background.
How to Use it?
// to enable the Android and iOS wakelock
Wakelock.enable();
// to disables the wakelock again.
Wakelock.disable();
import 'package:flutter/material.dart';
import 'package:wakelock/wakelock.dart';
void main() {
runApp( MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
Wakelock.enable(); // Here :)
return MaterialApp(
home: MyHomePage(),
);
}
}
Noteðð»♂️: You have to Stop and Run again
I found plugin that does the job. https://pub.dartlang.org/packages/screen
import 'package:screen/screen.dart';
// Prevent screen from going into sleep mode:
Screen.keepOn(true);
You also need to set permission for android
<uses-permission android:name="android.permission.WAKE_LOCK" />
As support for the screen plugin that @Tree mentioned has been discontinued and there are some issues with it now, you can use wakelock
.
Full disclosure: I am the author of this plugin, however, it is basically a port of the wakelock functionality from the screen
plugin, with the issues fixed:
import 'package:wakelock/wakelock.dart';
// To keep the screen on:
Wakelock.enable(); // or Wakelock.toggle(on: true);
// To let the screen turn off again:
Wakelock.disable(); // or Wakelock.toggle(on: false);
Learn more.