Showing
6 changed files
with
29 additions
and
13 deletions
| @@ -30,13 +30,13 @@ import 'package:wakelock/wakelock.dart'; | @@ -30,13 +30,13 @@ import 'package:wakelock/wakelock.dart'; | ||
| 30 | // ... | 30 | // ... |
| 31 | 31 | ||
| 32 | // The following lines of code toggle the wakelock based on a bool value. | 32 | // The following lines of code toggle the wakelock based on a bool value. |
| 33 | -bool enable = true; | 33 | +bool on = true; |
| 34 | // The following statement enables the wakelock. | 34 | // The following statement enables the wakelock. |
| 35 | -Wakelock.toggle(enable); | 35 | +Wakelock.toggle(on: on); |
| 36 | 36 | ||
| 37 | -enable = false; | 37 | +on = false; |
| 38 | // The following statement disables the wakelock. | 38 | // The following statement disables the wakelock. |
| 39 | -Wakelock.toggle(enable); | 39 | +Wakelock.toggle(on: on); |
| 40 | 40 | ||
| 41 | // If you want to retrieve the current wakelock status, | 41 | // If you want to retrieve the current wakelock status, |
| 42 | // you will have to be in an async scope | 42 | // you will have to be in an async scope |
| @@ -26,6 +26,7 @@ class _MyAppState extends State<MyApp> { | @@ -26,6 +26,7 @@ class _MyAppState extends State<MyApp> { | ||
| 26 | // The following code will enable the wakelock on Android or iOS using the wakelock plugin. | 26 | // The following code will enable the wakelock on Android or iOS using the wakelock plugin. |
| 27 | setState(() { | 27 | setState(() { |
| 28 | Wakelock.enable(); | 28 | Wakelock.enable(); |
| 29 | + // You could also use Wakelock.toggle(on: true); | ||
| 29 | }); | 30 | }); |
| 30 | }, | 31 | }, |
| 31 | ), | 32 | ), |
| @@ -35,6 +36,7 @@ class _MyAppState extends State<MyApp> { | @@ -35,6 +36,7 @@ class _MyAppState extends State<MyApp> { | ||
| 35 | // The following code will disable the wakelock on Android or iOS using the wakelock plugin. | 36 | // The following code will disable the wakelock on Android or iOS using the wakelock plugin. |
| 36 | setState(() { | 37 | setState(() { |
| 37 | Wakelock.disable(); | 38 | Wakelock.disable(); |
| 39 | + // You could also use Wakelock.toggle(on: false); | ||
| 38 | }); | 40 | }); |
| 39 | }, | 41 | }, |
| 40 | ), | 42 | ), |
| @@ -12,11 +12,16 @@ | @@ -12,11 +12,16 @@ | ||
| 12 | - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result { | 12 | - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result { |
| 13 | if ([@"toggle" isEqualToString:call.method]) { | 13 | if ([@"toggle" isEqualToString:call.method]) { |
| 14 | NSNumber *enable = call.arguments[@"enable"]; | 14 | NSNumber *enable = call.arguments[@"enable"]; |
| 15 | - [[UIApplication sharedApplication] setIdleTimerDisabled:enable.boolValue]; | ||
| 16 | - result(nil); | 15 | + NSNumber *enabled = [NSNumber numberWithBool:[[UIApplication sharedApplication] isIdleTimerDisabled]]; |
| 16 | + | ||
| 17 | + if([enable isEqualToNumber:enabled]) { | ||
| 18 | + result(nil); | ||
| 19 | + } else { | ||
| 20 | + [[UIApplication sharedApplication] setIdleTimerDisabled:enable.boolValue]; | ||
| 21 | + result(nil); | ||
| 22 | + } | ||
| 17 | } else if ([@"isEnabled" isEqualToString:call.method]) { | 23 | } else if ([@"isEnabled" isEqualToString:call.method]) { |
| 18 | - bool enabled = [[UIApplication sharedApplication] isIdleTimerDisabled]; | ||
| 19 | - result([NSNumber numberWithBool:enabled]); | 24 | + result([NSNumber numberWithBool:[[UIApplication sharedApplication] isIdleTimerDisabled]]); |
| 20 | } else { | 25 | } else { |
| 21 | result(FlutterMethodNotImplemented); | 26 | result(FlutterMethodNotImplemented); |
| 22 | } | 27 | } |
| @@ -26,12 +26,16 @@ class Wakelock { | @@ -26,12 +26,16 @@ class Wakelock { | ||
| 26 | 26 | ||
| 27 | /// You can simply use this function to toggle the wakelock using a [bool] value. | 27 | /// You can simply use this function to toggle the wakelock using a [bool] value. |
| 28 | /// ```dart | 28 | /// ```dart |
| 29 | - /// bool enable = true; | ||
| 30 | - /// Wakelock.toggle(enable); | 29 | + /// // This line keeps the screen on. |
| 30 | + /// Wakelock.toggle(on: true); | ||
| 31 | + /// | ||
| 32 | + /// bool turnOnWakelock = false; | ||
| 33 | + /// // The following line disables the wakelock. | ||
| 34 | + /// Wakelock.toggle(on: turnOnWakelock); | ||
| 31 | /// ``` | 35 | /// ``` |
| 32 | /// You can await the [Future] to wait for the operation to complete. | 36 | /// You can await the [Future] to wait for the operation to complete. |
| 33 | - static Future<void> toggle(bool enable) => | ||
| 34 | - _channel.invokeMethod('toggle', {'enable': enable}); | 37 | + static Future<void> toggle({bool on}) => |
| 38 | + _channel.invokeMethod('toggle', {'enable': on}); | ||
| 35 | 39 | ||
| 36 | /// If you want to retrieve the current wakelock status, you will have to call [Wakelock.isEnabled] | 40 | /// If you want to retrieve the current wakelock status, you will have to call [Wakelock.isEnabled] |
| 37 | /// and await its result: `bool isEnabled = await Wakelock.isEnabled()` | 41 | /// and await its result: `bool isEnabled = await Wakelock.isEnabled()` |
| 1 | name: wakelock | 1 | name: wakelock |
| 2 | description: This Flutter plugin allows you to keep Android and iOS devices awake, i.e. prevent them from sleeping by toggling the wakelock of the phone or tablet on or off. | 2 | description: This Flutter plugin allows you to keep Android and iOS devices awake, i.e. prevent them from sleeping by toggling the wakelock of the phone or tablet on or off. |
| 3 | -version: 0.1.1+2 | 3 | +version: 0.1.2 |
| 4 | author: creativecreatorormaybenot <19204050+creativecreatorormaybenot@users.noreply.github.com> | 4 | author: creativecreatorormaybenot <19204050+creativecreatorormaybenot@users.noreply.github.com> |
| 5 | homepage: https://github.com/creativecreatorormaybenot/wakelock | 5 | homepage: https://github.com/creativecreatorormaybenot/wakelock |
| 6 | 6 |
-
Please register or login to post a comment