wakelock.dart
1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import 'dart:async';
import 'package:flutter/services.dart';
/// To enable the wakelock, you can use [Wakelock.enableWakelock] and to disable it,
/// you can call [Wakelock.disableWakelock].
/// You do not need to worry about making redundant calls, e.g. calling [Wakelock.enableWakelock]
/// when the wakelock is already enabled as the plugin handles this for you.
/// If you want the flexibility to pass a [bool] to control whether the wakelock should be
/// enabled or disabled, you can use [Wakelock.toggleWakelock].
///
/// The [Wakelock.isWakelockEnabled] function allows you to retrieve the current wakelock
/// status from Android or iOS.
class Wakelock {
static const MethodChannel _channel = const MethodChannel('wakelock');
/// This can simply be called using `Wakelock.enableWakelock()` and does not return anything.
/// You can await the [Future] to wait for the operation to complete.
static Future<void> enableWakelock() =>
_channel.invokeMethod('toggleWakelock', {'enable': true});
/// This can simply be called using `Wakelock.disableWakelock()` and does not return anything.
/// You can await the [Future] to wait for the operation to complete.
static Future<void> disableWakelock() =>
_channel.invokeMethod('toggleWakelock', {'enable': false});
/// You can simply use this function to toggle the wakelock using a [bool] value.
/// ```dart
/// bool enable = true;
/// Wakelock.toggleWakelock(enable);
/// ```
/// You can await the [Future] to wait for the operation to complete.
static Future<void> toggleWakelock(bool enable) =>
_channel.invokeMethod('toggleWakelock', {'enable': enable});
/// If you want to retrieve the current wakelock status, you will have to call [Wakelock.isWakelockEnabled]
/// and await its result: `bool isWakelockEnabled = await Wakelock.isWakelockEnabled()`
static Future<bool> get isWakelockEnabled =>
_channel.invokeMethod('isWakelockEnabled');
}