Skip to content

Commit 0bde7b8

Browse files
committed
Add Battery service support to Flet
Introduces Battery service integration in Flet, including Dart and Python implementations, documentation, and example usage. Updates plugin registrants for macOS and Windows, adds battery_plus dependency, and exposes Battery-related types and events in the Python SDK.
1 parent cd62232 commit 0bde7b8

File tree

14 files changed

+223
-0
lines changed

14 files changed

+223
-0
lines changed

client/macos/Flutter/GeneratedPluginRegistrant.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import FlutterMacOS
66
import Foundation
77

88
import audioplayers_darwin
9+
import battery_plus
910
import device_info_plus
1011
import file_picker
1112
import geolocator_apple
@@ -27,6 +28,7 @@ import window_to_front
2728

2829
func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
2930
AudioplayersDarwinPlugin.register(with: registry.registrar(forPlugin: "AudioplayersDarwinPlugin"))
31+
BatteryPlusMacosPlugin.register(with: registry.registrar(forPlugin: "BatteryPlusMacosPlugin"))
3032
DeviceInfoPlusMacosPlugin.register(with: registry.registrar(forPlugin: "DeviceInfoPlusMacosPlugin"))
3133
FilePickerPlugin.register(with: registry.registrar(forPlugin: "FilePickerPlugin"))
3234
GeolocatorPlugin.register(with: registry.registrar(forPlugin: "GeolocatorPlugin"))

client/pubspec.lock

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,22 @@ packages:
8181
url: "https://pub.dev"
8282
source: hosted
8383
version: "4.2.1"
84+
battery_plus:
85+
dependency: transitive
86+
description:
87+
name: battery_plus
88+
sha256: "03d5a6bb36db9d2b977c548f6b0262d5a84c4d5a4cfee2edac4a91d57011b365"
89+
url: "https://pub.dev"
90+
source: hosted
91+
version: "6.2.3"
92+
battery_plus_platform_interface:
93+
dependency: transitive
94+
description:
95+
name: battery_plus_platform_interface
96+
sha256: e8342c0f32de4b1dfd0223114b6785e48e579bfc398da9471c9179b907fa4910
97+
url: "https://pub.dev"
98+
source: hosted
99+
version: "2.0.1"
84100
boolean_selector:
85101
dependency: transitive
86102
description:
@@ -1339,6 +1355,14 @@ packages:
13391355
url: "https://pub.dev"
13401356
source: hosted
13411357
version: "1.1.0"
1358+
upower:
1359+
dependency: transitive
1360+
description:
1361+
name: upower
1362+
sha256: cf042403154751180affa1d15614db7fa50234bc2373cd21c3db666c38543ebf
1363+
url: "https://pub.dev"
1364+
source: hosted
1365+
version: "0.7.0"
13421366
uri_parser:
13431367
dependency: transitive
13441368
description:

client/windows/flutter/generated_plugin_registrant.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "generated_plugin_registrant.h"
88

99
#include <audioplayers_windows/audioplayers_windows_plugin.h>
10+
#include <battery_plus/battery_plus_windows_plugin.h>
1011
#include <geolocator_windows/geolocator_windows.h>
1112
#include <media_kit_libs_windows_video/media_kit_libs_windows_video_plugin_c_api.h>
1213
#include <media_kit_video/media_kit_video_plugin_c_api.h>
@@ -23,6 +24,8 @@
2324
void RegisterPlugins(flutter::PluginRegistry* registry) {
2425
AudioplayersWindowsPluginRegisterWithRegistrar(
2526
registry->GetRegistrarForPlugin("AudioplayersWindowsPlugin"));
27+
BatteryPlusWindowsPluginRegisterWithRegistrar(
28+
registry->GetRegistrarForPlugin("BatteryPlusWindowsPlugin"));
2629
GeolocatorWindowsRegisterWithRegistrar(
2730
registry->GetRegistrarForPlugin("GeolocatorWindows"));
2831
MediaKitLibsWindowsVideoPluginCApiRegisterWithRegistrar(

client/windows/flutter/generated_plugins.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
list(APPEND FLUTTER_PLUGIN_LIST
66
audioplayers_windows
7+
battery_plus
78
geolocator_windows
89
media_kit_libs_windows_video
910
media_kit_video

packages/flet/lib/src/flet_core_extension.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ import 'flet_extension.dart';
107107
import 'flet_service.dart';
108108
import 'models/control.dart';
109109
import 'services/browser_context_menu.dart';
110+
import 'services/battery.dart';
110111
import 'services/clipboard.dart';
111112
import 'services/file_picker.dart';
112113
import 'services/haptic_feedback.dart';
@@ -377,6 +378,8 @@ class FletCoreExtension extends FletExtension {
377378
return AccelerometerService(control: control);
378379
case "Barometer":
379380
return BarometerService(control: control);
381+
case "Battery":
382+
return BatteryService(control: control);
380383
case "Clipboard":
381384
return ClipboardService(control: control);
382385
case "FilePicker":
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import 'dart:async';
2+
3+
import 'package:battery_plus/battery_plus.dart';
4+
import 'package:flutter/foundation.dart';
5+
6+
import '../flet_service.dart';
7+
import '../utils/numbers.dart';
8+
9+
class BatteryService extends FletService {
10+
final Battery _battery = Battery();
11+
StreamSubscription<BatteryState>? _stateSub;
12+
13+
BatteryService({required super.control});
14+
15+
@override
16+
void init() {
17+
super.init();
18+
debugPrint("BatteryService(${control.id}).init");
19+
control.addInvokeMethodListener(_invokeMethod);
20+
_updateListeners();
21+
}
22+
23+
@override
24+
void update() {
25+
_updateListeners();
26+
}
27+
28+
Future<dynamic> _invokeMethod(String name, dynamic args) async {
29+
switch (name) {
30+
case "get_battery_level":
31+
return _battery.batteryLevel;
32+
case "get_battery_state":
33+
final state = await _battery.batteryState;
34+
return state.name;
35+
case "is_in_battery_save_mode":
36+
return _battery.isInBatterySaveMode;
37+
default:
38+
throw Exception("Unknown Battery method: $name");
39+
}
40+
}
41+
42+
void _updateListeners() {
43+
final listenState = control.getBool("on_state_change") == true;
44+
if (listenState && _stateSub == null) {
45+
_stateSub = _battery.onBatteryStateChanged.listen((state) {
46+
control.triggerEvent("state_change", {"state": state.name});
47+
}, onError: (error) {
48+
debugPrint("BatteryService: error listening to state changes: $error");
49+
});
50+
} else if (!listenState && _stateSub != null) {
51+
_stateSub?.cancel();
52+
_stateSub = null;
53+
}
54+
}
55+
56+
@override
57+
void dispose() {
58+
debugPrint("BatteryService(${control.id}).dispose()");
59+
control.removeInvokeMethodListener(_invokeMethod);
60+
_stateSub?.cancel();
61+
_stateSub = null;
62+
super.dispose();
63+
}
64+
}

packages/flet/pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ dependencies:
2121
sdk: flutter
2222
flutter_localizations:
2323
sdk: flutter
24+
battery_plus: ^6.2.2
2425
collection: ^1.19.0
2526
device_info_plus: ^12.1.0
2627
equatable: ^2.0.3
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import flet as ft
2+
3+
4+
async def main(page: ft.Page):
5+
battery = ft.Battery()
6+
page.services.append(battery) # need to keep a reference to the service
7+
8+
info = ft.Text()
9+
state_text = ft.Text()
10+
11+
async def refresh_info(_=None):
12+
level = await battery.get_battery_level()
13+
state = await battery.get_battery_state()
14+
save_mode = await battery.is_in_battery_save_mode()
15+
info.value = (
16+
f"Level: {level}% | State: {state} | "
17+
f"Battery saver: {'on' if save_mode else 'off'}"
18+
)
19+
20+
async def on_state_change(e: ft.BatteryStateChangeEvent):
21+
state_text.value = f"State changed: {e.state}"
22+
await refresh_info()
23+
24+
battery.on_state_change = on_state_change
25+
26+
await refresh_info()
27+
28+
page.add(
29+
ft.Column(
30+
[
31+
info,
32+
ft.Row(
33+
[
34+
ft.Button("Refresh battery info", on_click=refresh_info),
35+
]
36+
),
37+
state_text,
38+
],
39+
)
40+
)
41+
42+
43+
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.Battery
3+
examples: ../../examples/controls/battery
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) }}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{ class_all_options("flet.BatteryState", separate_signature=False) }}

0 commit comments

Comments
 (0)