wakelock 0.1.4+1

  • Readme
  • Changelog
  • Example
  • Installing
  • 98

Wakelock #

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

Usage #

To use this plugin, follow the installing guide.

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 on = true;
// The following statement enables the wakelock.
Wakelock.toggle(on: on);

on = false;
// The following statement disables the wakelock.
Wakelock.toggle(on: on);

// If you want to retrieve the current wakelock status,
// you will have to be in an async scope
// to await the Future returned by isEnabled.
bool isEnabled = await Wakelock.isEnabled;

If you want to wait for the wakelock toggle on Android or iOS to complete (which takes an insignificant amount of time), you can also await any of Wakelock.enable, Wakelock.disable, and Wakelock.toggle.

Notes #

This plugin is originally based on screen.
Specifically, the wakelock functionality was extracted into this plugin due to lack of maintenance by the author of the screen plugin.

If you want to contribute to this plugin, follow the contributing guide.

0.1.4+1 #

  • Finished migration to new Flutter plugin embedding.
  • Fixed missing activity on Android for apps using the old plugin embedding.
  • Lowered Flutter SDK version constraint to 1.12.0.

0.1.4 #

  • Added assertion for on in Wakelock.toggle to be non-null and @required annotation. This is not a breaking change since it was logically required before.
  • Switched from Java to Kotlin for the Android implementation.
  • Updated plugin files to the latest Flutter standards.
  • Updated structure to ignore unnecessary files.
  • Added unit testing.
  • Improved the integration tests to make use of the example app.
  • Updated example app.
  • Added GitHub actions and removed Travis CI.
  • Made CHANGELOG.md formulations more consistent.
  • Updated year in LICENSE.
  • Enforced pedantic lint rules.

0.1.3+4 #

  • Fixed iOS simulator issue.

0.1.3+3 #

  • Fixed Flutter SDK version constraint.

0.1.3+2 #

  • Fixed pubspec.yaml.

0.1.3+1 #

  • Updated pubspec.yaml to match new format.

0.1.3 #

  • Completed AndroidX migration.

0.1.2+8 #

  • Updated documentation.

0.1.2+7 #

  • Formatted AndroidManifest.xml.

0.1.2+6 #

  • Cleaned up the Android manifest.
  • Changed a test name in test_driver.
  • Updated the plugin description.
  • Updated README.md.
  • Updated CONTRIBUTING.md.
  • Updated .travis.yml.
  • Removed unnecessary Assets directory from the ios folder.

0.1.2+5 #

  • Expanded continuous integration to include format checking and code analysis.

0.1.2+4 #

  • Updated the example's README.

0.1.2+3 #

  • Improved Travis CI setup.
  • Updated badges.

0.1.2+2 #

  • Updated description.
  • Flutter master is used in integration tests now.

0.1.2+1 #

  • Added integration testing.
  • Removed unnecessary Android Manifest permission.
  • Added a contributing guide.
  • Added CI.

0.1.2 #

  • Changed Wakelock.toggle's parameter to a named parameter.
  • Improved iOS implementation.

0.1.1+2 #

  • Made the plugin description more concise.

0.1.1+1 #

  • Elaborated a bit more in description.

0.1.1 #

  • Renamed functions.
  • Improved README.

0.1.0+3 #

  • Added wakelock permission in Android Manifest.

0.1.0+2 #

  • Improved README.
  • Removed unnecessary files.

0.1.0+1 #

  • Fixed dependency issue.
  • Removed unnecessary dependencies.

0.1.0 #

  • Bumped version to indicate that the plugin is fully usable.
  • Improved README's.
  • Formatted Dart files.

0.0.1 #

  • Initial version.

example/lib/main.dart

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

void main() {
  runApp(ExampleApp());
}

/// The wakelock implementation is located inside the [FlatButton.onPressed] functions and a [FutureBuilder].
/// The [FlatButton]'s and the [FutureBuilder] sit inside the [Column] that is a child of the [Scaffold] in [_ExampleAppState].
class ExampleApp extends StatefulWidget {
  const ExampleApp({Key key}) : super(key: key);

  @override
  _ExampleAppState createState() => _ExampleAppState();
}

class _ExampleAppState extends State<ExampleApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: <Widget>[
              FlatButton(
                onPressed: () {
                  // The following code will enable the wakelock on Android or iOS using the wakelock plugin.
                  setState(() {
                    Wakelock.enable();
                    // You could also use Wakelock.toggle(on: true);
                  });
                },
                child: const Text('enable wakelock'),
              ),
              FlatButton(
                onPressed: () {
                  // The following code will disable the wakelock on Android or iOS using the wakelock plugin.
                  setState(() {
                    Wakelock.disable();
                    // You could also use Wakelock.toggle(on: false);
                  });
                },
                child: const Text('disable wakelock'),
              ),
              FutureBuilder(
                future: Wakelock.isEnabled,
                builder: (context, AsyncSnapshot<bool> snapshot) {
                  // The use of FutureBuilder is necessary here to await the bool value from isEnabled.
                  if (!snapshot.hasData)
                    return Container(); // The Future is retrieved so fast that you will not be able to see any loading indicator.
                  return Text(
                      'wakelock is currently ${snapshot.data ? 'enabled' : 'disabled'}');
                },
              ),
            ],
          ),
        ),
      ),
    );
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  wakelock: ^0.1.4+1

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:wakelock/wakelock.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
97
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
98
Learn more about scoring.

We analyzed this package on Mar 27, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.6
  • Flutter: 1.12.13+hotfix.8

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.7.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.11 1.14.12
meta 1.1.8
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test
pedantic 1.9.0