Giter Site home page Giter Site logo

os01ri / wakelock Goto Github PK

View Code? Open in Web Editor NEW

This project forked from creativecreatorormaybenot/wakelock

0.0 0.0 0.0 458 KB

Flutter plugin that allows you to keep the device screen awake on Android, iOS, macOS, Windows, and web.

Home Page: https://pub.dev/packages/wakelock

License: BSD 3-Clause "New" or "Revised" License

Ruby 7.82% Objective-C 35.09% Java 22.29% Kotlin 10.38% Dart 24.42%

wakelock's Introduction

wakelock Pub version GitHub stars Twitter Follow

Plugin that allows you to keep the device screen awake, i.e. prevent the screen from sleeping


The plugin allows you to enable and toggle the screen wakelock, which prevents the screen from turning off automatically.
Essentially, this allows you to keep the device awake, i.e. prevent the device from sleeping.

Supported platforms

Platform wakelock support
Android
iOS
Web
macOS
Windows
Linux planned

Usage

To use this plugin, follow the installing guide.

The wakelock 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 Wakelock class.
If you want to enable the wakelock, i.e. keep the device awake, you can simply call Wakelock.enable and to disable it again, you can use Wakelock.disable:

import 'package:wakelock/wakelock.dart';
// ...

// The following line will enable the Android and iOS wakelock.
Wakelock.enable();

// The next line disables the wakelock again.
Wakelock.disable();

For more advanced usage, you can pass a bool to Wakelock.toggle to enable or disable the wakelock and also retrieve the current wakelock status using Wakelock.isEnabled:

import 'package:wakelock/wakelock.dart';
// ...

// The following lines of code toggle the wakelock based on a bool value.
bool enable = true;
// The following statement enables the wakelock.
Wakelock.toggle(enable: enable);

enable = false;
// The following statement disables the wakelock.
Wakelock.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 Wakelock.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 Wakelock.enable, Wakelock.disable, and Wakelock.toggle.

Ensure the WidgetsBinding is initialized

If you want to call Wakelock.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();
  Wakelock.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 Wakelock.enable() more often.

Calling Wakelock.enable() in main()

As touched on in the previous paragraph, calling Wakelock.enable() in your main() function is not the best approach for a number of reasons.

The most important factors are:

  1. 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.
  2. The wakelock can be released by external sources at any time (e.g. by the OS).
    Only calling Wakelock.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.

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.

wakelock's People

Contributors

creativecreatorormaybenot avatar dev-os01ri avatar ened avatar github-actions[bot] avatar grahamsmith avatar irvine5k avatar jon-salmon avatar michalsrutek avatar miracoli avatar os01ri avatar roulljdh avatar suyaolingoace avatar tahamv avatar wilsonowilson avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.