Skip to content

Commit 98516c7

Browse files
committed
Add Wakelock service to Flutter and Python SDKs
Introduced a new Wakelock service to prevent device sleep in both the Flutter and Python Flet SDKs. Added the wakelock_plus dependency to the Flutter project, implemented the WakelockService in Dart, and exposed the Wakelock control in Python with documentation and an example. Updated documentation and navigation to include the new control.
1 parent 36fb660 commit 98516c7

File tree

8 files changed

+127
-0
lines changed

8 files changed

+127
-0
lines changed

packages/flet/lib/src/flet_core_extension.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ import 'services/screen_brightness.dart';
118118
import 'services/storage_paths.dart';
119119
import 'services/tester.dart';
120120
import 'services/url_launcher.dart';
121+
import 'services/wakelock.dart';
121122
import 'services/window.dart';
122123
import 'utils/cupertino_icons.dart';
123124
import 'utils/material_icons.dart';
@@ -404,6 +405,8 @@ class FletCoreExtension extends FletExtension {
404405
return UserAccelerometerService(control: control);
405406
case "UrlLauncher":
406407
return UrlLauncherService(control: control);
408+
case "Wakelock":
409+
return WakelockService(control: control);
407410
default:
408411
return null;
409412
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import 'package:flutter/foundation.dart';
2+
import 'package:wakelock_plus/wakelock_plus.dart';
3+
4+
import '../flet_service.dart';
5+
6+
class WakelockService extends FletService {
7+
WakelockService({required super.control});
8+
9+
@override
10+
void init() {
11+
super.init();
12+
debugPrint("WakelockService(${control.id}).init");
13+
control.addInvokeMethodListener(_invokeMethod);
14+
}
15+
16+
Future<dynamic> _invokeMethod(String name, dynamic args) async {
17+
switch (name) {
18+
case "enable":
19+
return WakelockPlus.enable();
20+
case "disable":
21+
return WakelockPlus.disable();
22+
case "is_enabled":
23+
return WakelockPlus.enabled;
24+
default:
25+
throw Exception("Unknown Wakelock method: $name");
26+
}
27+
}
28+
29+
@override
30+
void dispose() {
31+
debugPrint("WakelockService(${control.id}).dispose()");
32+
control.removeInvokeMethodListener(_invokeMethod);
33+
super.dispose();
34+
}
35+
}

packages/flet/pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ dependencies:
4646
web_socket_channel: ^3.0.2
4747
window_manager: ^0.5.1
4848
window_to_front: ^0.0.3
49+
wakelock_plus: ^1.2.8
4950

5051
dev_dependencies:
5152
flutter_test:
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import flet as ft
2+
3+
4+
async def main(page: ft.Page):
5+
wakelock = ft.Wakelock()
6+
7+
status = ft.Text()
8+
9+
async def update_status():
10+
enabled = await wakelock.is_enabled()
11+
status.value = f"Wakelock enabled: {enabled}"
12+
13+
async def enable_lock():
14+
await wakelock.enable()
15+
await update_status()
16+
17+
async def disable_lock():
18+
await wakelock.disable()
19+
await update_status()
20+
21+
await update_status()
22+
23+
page.add(
24+
ft.Column(
25+
[
26+
status,
27+
ft.Row(
28+
[
29+
ft.Button("Enable wakelock", on_click=enable_lock),
30+
ft.Button("Disable wakelock", on_click=disable_lock),
31+
],
32+
wrap=True,
33+
),
34+
],
35+
)
36+
)
37+
38+
39+
ft.run(main)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
class_name: flet.Wakelock
3+
examples: ../../examples/controls/wakelock
4+
---
5+
6+
{{ class_summary(class_name) }}
7+
8+
## Examples
9+
10+
```python
11+
--8<-- "{{ examples }}/basic.py"
12+
```
13+
14+
{{ class_members(class_name) }}

sdk/python/packages/flet/mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,7 @@ nav:
464464
- VerticalDivider: controls/verticaldivider.md
465465
- Video: video/index.md
466466
- View: controls/view.md
467+
- Wakelock: controls/wakelock.md
467468
- WebView: webview/index.md
468469
- WindowDragArea: controls/windowdragarea.md
469470
- CLI:

sdk/python/packages/flet/src/flet/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,7 @@
447447
from flet.controls.services.storage_paths import StoragePaths
448448
from flet.controls.services.url_launcher import UrlLauncher
449449
from flet.controls.services.user_accelerometer import UserAccelerometer
450+
from flet.controls.services.wakelock import Wakelock
450451
from flet.controls.template_route import TemplateRoute
451452
from flet.controls.text_style import (
452453
StrutStyle,
@@ -1001,6 +1002,7 @@
10011002
"View",
10021003
"ViewPopEvent",
10031004
"VisualDensity",
1005+
"Wakelock",
10041006
"WebBrowserName",
10051007
"WebDeviceInfo",
10061008
"WebRenderer",
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from flet.controls.base_control import control
2+
from flet.controls.services.service import Service
3+
4+
__all__ = ["Wakelock"]
5+
6+
7+
@control("Wakelock")
8+
class Wakelock(Service):
9+
"""
10+
Prevents the device from sleeping while enabled.
11+
"""
12+
13+
async def enable(self):
14+
"""
15+
Keeps the device awake.
16+
"""
17+
18+
await self._invoke_method("enable")
19+
20+
async def disable(self):
21+
"""
22+
Allows the device to sleep again.
23+
"""
24+
25+
await self._invoke_method("disable")
26+
27+
async def is_enabled(self) -> bool:
28+
"""
29+
Returns `True` if the wakelock is currently enabled.
30+
"""
31+
32+
return await self._invoke_method("is_enabled")

0 commit comments

Comments
 (0)