Skip to content

Commit

Permalink
docs(battery_plus): Improve documentation on battery states (#2402)
Browse files Browse the repository at this point in the history
  • Loading branch information
vbuberen committed Dec 1, 2023
1 parent 42ef02b commit baeb886
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
19 changes: 16 additions & 3 deletions docs/battery_plus/usage.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ The available states are:
- **Full**: the battery is at 100%, full of energy.
- **Charging**: the battery is being charged.
- **Discharging**: the battery is losing energy.
- **Connected, but not charging**: the battery is connected to power source, but not charging. This state is only available on Android, MacOS and Linux platforms.
- **Unknown**: unable to read the battery state or the device has no battery.

To get these information in realtime, you can subscribe to the stream from the `Battery` API.
To get this information in realtime, you can subscribe to the stream from the `Battery` API.

```dart
// The enum that holds different states of a battery
Expand All @@ -35,7 +36,7 @@ void initState() {
// Initialize the subscription by listening to onBatteryStateChanged Stream
_batteryStateSubscription =
_battery.onBatteryStateChanged.listen((BatteryState state) {
// Each time the battery state changes, the local state of the widget is updated
setState(() {
_batteryState = state;
Expand All @@ -56,4 +57,16 @@ Using the same `Battery` instance, it's possible to read the current percentage

```dart
final batteryLevel = await _battery.batteryLevel;
```
```

### Check if battery save mode is on

:::note
This check is currently available on Android, iOS and Windows platforms only
:::

The is also a possibility to check if device has battery save mode enabled

```dart
final isInSaveMode = await _battery.isInBatterySaveMode;
```
4 changes: 4 additions & 0 deletions packages/battery_plus/battery_plus/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ print(await battery.batteryLevel);
battery.onBatteryStateChanged.listen((BatteryState state) {
// Do something with new state
});
// Check if device in battery save mode
// Currently available on Android, iOS and Windows platforms only
print(await battery.isInBatterySaveMode);
```

## Learn more
Expand Down

0 comments on commit baeb886

Please sign in to comment.