wakelock_plus
v1.4.0Plugin that allows you to keep the device screen awake, i.e. prevent the screen from sleeping on Android, iOS, macOS, Windows, Linux, and web.
Package archive: https://pubdev.letsnova.ru/api/archives/wakelock_plus/1.4.0.tar.gz
dart pub add wakelock_plusReadme
wakelock_plus
A continuation of the original wakelock Flutter Plugin written by creativecreatorormaybenot that allows you to keep the device screen awake, i.e. prevent the screen from sleeping.
You can enable and toggle the screen wakelock, which prevents the screen from turning off automatically.
Table of Contents
Supported Platforms
| Platform | wakelock_plus support |
|---|---|
| Android | ✅ |
| iOS | ✅ |
| Web | ✅ |
| macOS | ✅ |
| Windows | ✅ |
| Linux | ✅ |
Framework and Platform Requirements
- Flutter
- Flutter SDK >=3.22.0
- Dart SDK >=3.4.0
- Android
- Android API 21 (Lollipop) or higher
- Java 17 or higher
- Android Gradle Plugin >=8.12.1
- Gradle wrapper >=8.13
- iOS
- iOS 12.0 or higher
- macOS
- macOS 10.15 or higher
- Windows
- Windows 7 or higher
- Linux
- gtk+ >= 3.0
- glib >= 2.0
- gio >= 2.0
- Web
- Modern browsers
Usage
To use this plugin, follow the installation guide.
The wakelock_plus plugin does not require any special 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.
Implementation
Everything in this plugin is controlled via the
WakelockPlus class.
If you want to enable the wakelock, i.e. keep the device awake, you can simply call
WakelockPlus.enable
and to disable it again, you can use
WakelockPlus.disable:
import 'package:wakelock_plus/wakelock_plus.dart';
// ...
// The following line will enable the Android and iOS wakelock.
WakelockPlus.enable();
// The next line disables the wakelock again.
WakelockPlus.disable();
For more advanced usage, you can pass a bool to
WakelockPlus.toggle
to enable or disable the wakelock and also retrieve the current wakelock status using
WakelockPlus.isEnabled:
import 'package:wakelock_plus/wakelock_plus.dart';
// ...
// The following lines of code toggle the wakelock based on a bool value.
bool enable = true;
// The following statement enables the wakelock.
WakelockPlus.toggle(enable: enable);
enable = false;
// The following statement disables the wakelock.
WakelockPlus.toggle(enable: enable);
// If you want to retrieve the current wakelock status,
// you will have to be in an async scope
// to await the Future returned by `enabled`.
bool wakelockEnabled = await WakelockPlus.enabled;
If you want to wait for the wakelock toggle to complete (which takes an insignificant amount of
time), you can also await any of WakelockPlus.enable, WakelockPlus.disable, and
WakelockPlus.toggle.
Ensure the WidgetsBinding is initialized
If you want to call WakelockPlus.enable() or the other functions before runApp()
(e.g. in main()), you will have to ensure that the WidgetsBinding is initialized first:
void main() {
WidgetsFlutterBinding.ensureInitialized();
WakelockPlus.enable();
runApp(..);
}
In general, it is advisable to make your wakelock dependent on certain components within your app
instead, e.g. by only enabling it (continually) when a certain widget is visible.
There is no negative impact in calling WakelockPlus.enable() more often.
Calling WakelockPlus.enable() in main()
As touched on in the previous paragraph, calling WakelockPlus.enable() in your main()
function is not the best approach for a number of reasons.
The most important factors are:
- Users expect their screen to automatically turn off unless e.g. a video is playing.
It is unlikely that your whole app requires the screen to always stay on. - The wakelock can be released by external sources at any time (e.g. by the OS).
Only callingWakelockPlus.enable()once will most likely mean that the screen turns off at one point or another anyway.
This is why you should instead prefer to enable the wakelock whenever components inside of your app
that require the screen to stay on are active. This can e.g. happen in the build method of your
widget.
Platform Specific Integration Instructions
Android
When building your app on Android and using this library, you may be prompted by the following:
One or more plugins require a higher Android NDK version.
If this occurs, simply add the NDK version specified in the error message in your app module's
build.gradle file's android closure. For example:
android {
// Current version at the time of this writing.
// Use the version specified by the error message.
ndkVersion flutter.ndkVersion
//....
}
Learn more
If you want to learn more about how this plugin works, how to contribute, etc., you can read through the main README on GitHub.
Changelog
[1.4.0]
- #107: Flutter 3.35 upgrade. Thanks diegotori.
- BREAKING CHANGES:
- Dart
- Library now requires Dart version
3.4or higher. - Library now requires Flutter version
3.22or higher.
- Library now requires Dart version
- Android
- Library now requires Android API 21 (Lollipop) or higher
- Library now requires Java 17 or higher
- Library now requires Android Gradle Plugin >=8.12.1
- Library now requires Gradle wrapper >=8.13
- Dart
[1.3.3]
[1.3.2]
- #98: fix(ios): fix path to PrivacyInfo.xcprivacy. Thanks slobodanmilinkovic.
[1.3.1]
[1.3.0]
[1.2.11]
1.2.10
1.2.9
1.2.8
- #70 chore(deps): Allow web ^1.0.0. Thanks provokateurin.
1.2.7
1.2.6
1.2.5
1.2.4
1.2.3
- #44 fix(wakelock_plus): WASM-compatible conditional imports. Thanks IchordeDionysos.
1.2.2
- #40 Android Gradle 8.x upgrade fixes. Thanks diegotori.
- #41 Fix: dependency minimums adjustments. Thanks diegotori.
1.2.1
- Emergency hotfix by upgrading
wakelock_plus_platform_interfaceto version1.2.0.
1.2.0
- #26 Update web implementation to depend on Dart 3.3 JS interop + the recommended web package. Thanks davidmartos96.
- #33 Upgraded package_info_plus to version 6.0.0. Thanks diegotori.
- #34 Migrate to gradle 8.0. Thanks hasanm08.
- #35 Issue #18. Thanks diegotori.
- BREAKING CHANGES:
- Library now requires Dart version
3.3or higher. - Library now requires Flutter version
3.19or higher. - Library now requires Java 17 or higher and your app must build on API 34 (14 Upside Down Cake) on Android.
- Library now requires Dart version
1.1.6
- #23 fix: privacy manifest. Thanks SputNikPlop.
1.1.5
1.1.4
1.1.3
1.1.2
- #10: Allow use of
package_info_plusversion5.x.x. Thanks dkrutskikh.
1.1.1
1.1.0
- #2: Downgraded minimum
metato version1.3.0in order to maintain compatibility with versions of Flutter below3.10. Also downgradedjsto version0.6.3due to the above. Thanks diegotori. - BREAKING CHANGE: Increased the minimum supported Dart version to
2.18in order to align it withwakelock_plus_platform_interfaceversion1.1.0.
1.0.0+3
README.mdnow attributes the original author of thewakelockplugin, which this plugin is based on.
1.0.0+2
- Added another update to
README.md.
1.0.0+1
- Added update to
README.md.
1.0.0
- Initial release for the continuation of the orginal wakelock Flutter Plugin.
