battery_flutter 0.0.1
battery_flutter: ^0.0.1 copied to clipboard
A plugin get device battery
battery_flutter #
A Flutter plugin to retrieve the current battery level of the device on both Android and iOS.
๐ Features #
- Get battery level as a percentage
- Supports both Android and iOS
- Simple API using
MethodChannel
๐ Getting Started #
1. Add dependency #
In your pubspec.yaml
:
dependencies:
battery_flutter: ^0.0.1
Then run:
flutter pub get
2. Import the plugin #
import 'package:battery_flutter/battery_flutter.dart';
3. Usage Example #
final battery = BatteryFlutter();
int? batteryLevel = await battery.getBattery();
print('Battery level: $batteryLevel%');
๐ฑ Example App #
A complete example is available in the example/
directory.
Run:
cd example
flutter run
๐งช Example UI Widget #
import 'package:flutter/material.dart';
import 'package:battery_flutter/battery_flutter.dart';
class BatteryWidget extends StatefulWidget {
@override
_BatteryWidgetState createState() => _BatteryWidgetState();
}
class _BatteryWidgetState extends State<BatteryWidget> {
int? _batteryLevel;
@override
void initState() {
super.initState();
_loadBatteryLevel();
}
Future<void> _loadBatteryLevel() async {
final battery = BatteryFlutter();
final level = await battery.getBattery();
setState(() {
_batteryLevel = level;
});
}
@override
Widget build(BuildContext context) {
return Center(
child: Text('Battery Level: ${_batteryLevel ?? "Unknown"}%'),
);
}
}
๐ฆ Platform Support #
Platform | Supported |
---|---|
Android | โ Yes |
iOS | โ Yes |
Web | โ No |
Windows | โ No |
macOS | โ No |
Linux | โ No |
๐ License #
This project is licensed under the MIT License.