Skip to content

Latest commit

 

History

History
48 lines (41 loc) · 1.38 KB

useBattery.md

File metadata and controls

48 lines (41 loc) · 1.38 KB

useBattery

Tracks battery status.

Installation

Depends on battery_plus.

dependencies:
  flutter_use_battery: ^0.0.2

Usage

class Sample extends HookWidget {
  @override
  Widget build(BuildContext context) {
    final battery = useBattery();
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            const Text("-- Battery --"),
            Text("fetched: ${battery.fetched}"),
            Text("batteryState: ${battery.batteryState}"),
            Text("level: ${battery.batteryLevel}"),
            Text("isInBatterySaveMode: ${battery.isInBatterySaveMode}"),
          ],
        ),
      ),
    );
  }
}

Reference

  • fetched: bool - whether battery state is fetched;
  • batteryLevel: int - representing the system's battery charge level to a value between 0 and 100.
  • isInBatterySaveMode: bool - check if device is on battery save mode.
  • batteryState: BatteryState - battery state changes.
    • full - The battery is completely full of energy.
    • charging -The battery is currently storing energy.
    • discharging - The battery is currently losing energy.
    • unknown - The state of the battery is unknown.