simplest_power_utilities 1.1.0
simplest_power_utilities: ^1.1.0 copied to clipboard
Plugin which exposes various power-related utilities for Android and iOS, including low power mode detection
simplest_power_utilities #
Power-related utilities for Android and iOS: low power mode detection, battery level, charging state, change events for all three, and a shortcut to the battery settings.
Requirements #
| Minimum version | |
|---|---|
| Android | Android 10 (API 29) |
| iOS | 15.0 |
| Dart | 3.13.5 |
| Flutter | 3.47.6 |
Basic usage #
import 'package:simplest_power_utilities/simplest_power_utilities.dart';
final plugin = SimplestPowerUtilities();
// Check current low power mode status
final isEnabled = await plugin.isLowPowerModeEnabled();
print('Low power mode enabled: $isEnabled');
Real-time updates #
// Subscribe to low power mode changes
final subscription = plugin.onLowPowerModeChanged.listen((isEnabled) {
print('Low power mode changed: $isEnabled');
});
// Don't forget to cancel the subscription when done
await subscription.cancel();
Battery level #
// Current battery level, 0-100
final level = await plugin.getBatteryLevel();
print('Battery level: $level%');
// Live updates: emits the current level right away, then on every change
final subscription = plugin.onBatteryLevelChanged.listen((level) {
print('Battery level changed: $level%');
});
await subscription.cancel();
getBatteryLevel() throws a PlatformException with code UNAVAILABLE when the platform cannot report a level. The iOS Simulator is the usual case.
Charging state #
// Current charging state
final state = await plugin.getChargingState();
print('Charging: ${state.isCharging}, status: ${state.status}, source: ${state.source}');
// Live updates: emits the current state right away, then on every change
final subscription = plugin.onChargingStateChanged.listen((state) {
print('Charging state changed: $state');
});
await subscription.cancel();
ChargingState has two parts. status is one of unknown, discharging, charging, notCharging (plugged in but not charging, Android only) or full. source is one of none, ac, usb, wireless, dock (Android 13+) or unknown. iOS never reports the power source, so a plugged-in iOS device has source == ChargingSource.unknown. isCharging and isPluggedIn cover the common checks.
Launch battery settings #
// Launch the device's battery settings screen
final success = await plugin.launchBatterySettings();
if (success) {
print('Battery settings opened successfully');
} else {
print('Failed to open battery settings');
}
Platform behaviour #
| Android | iOS | |
|---|---|---|
isLowPowerModeEnabled() |
Battery Saver (PowerManager.isPowerSaveMode) |
Low Power Mode (ProcessInfo.isLowPowerModeEnabled) |
onLowPowerModeChanged |
ACTION_POWER_SAVE_MODE_CHANGED broadcast |
NSProcessInfoPowerStateDidChange notification |
getBatteryLevel() |
BatteryManager.BATTERY_PROPERTY_CAPACITY, falling back to the sticky ACTION_BATTERY_CHANGED intent |
UIDevice.batteryLevel (enables isBatteryMonitoringEnabled) |
onBatteryLevelChanged |
ACTION_BATTERY_CHANGED broadcast, deduplicated by level |
UIDevice.batteryLevelDidChangeNotification, deduplicated by level |
getChargingState() |
EXTRA_STATUS and EXTRA_PLUGGED of the sticky ACTION_BATTERY_CHANGED intent |
UIDevice.batteryState (enables isBatteryMonitoringEnabled) |
onChargingStateChanged |
ACTION_BATTERY_CHANGED broadcast, deduplicated by state |
UIDevice.batteryStateDidChangeNotification, deduplicated by state |
launchBatterySettings() |
Battery Saver settings, falling back to the battery usage screen | The app's page in the Settings app (iOS has no public deep link to the Battery section) |
All streams are broadcast streams. onLowPowerModeChanged only emits on change, so read the current value with isLowPowerModeEnabled() first. onBatteryLevelChanged and onChargingStateChanged emit the current value as soon as you subscribe. The native listener is registered on the first subscription and released when the last one is cancelled.
Example #
The example app shows the low power mode state, the battery percentage and the charging state live, lists low power mode change events as they arrive while a subscription is active, and opens the battery settings.