get_storage_battery 1.0.1 get_storage_battery: ^1.0.1 copied to clipboard
A Flutter plugin to monitor and manage battery usage and storage information on Android and iOS devices.
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:get_storage_battery/get_storage_battery.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _batteryPercentage = 'Unknown';
String _totalStorage = 'Unknown';
String _usedStorage = 'Unknown';
String _freeStorage = 'Unknown';
final _getStorageBatteryPlugin = GetStorageBattery();
@override
void initState() {
super.initState();
initPlatformState();
}
// Initialize the platform state to get battery percentage and storage information.
Future<void> initPlatformState() async {
try {
// Fetch the battery percentage
int batteryLevel = await _getStorageBatteryPlugin.getBatteryPercentage();
setState(() {
_batteryPercentage = '$batteryLevel%';
});
// Fetch the storage information
Map<String, int> storageInfo =
await _getStorageBatteryPlugin.getStorageInfo();
setState(() {
_totalStorage = '${storageInfo['total']} MB';
_usedStorage = '${storageInfo['used']} MB';
_freeStorage = '${storageInfo['free']} MB';
});
} on PlatformException catch (e) {
setState(() {
_batteryPercentage = 'Failed to get battery percentage: ${e.message}';
_totalStorage = 'Failed to get storage info';
_usedStorage = 'Failed to get storage info';
_freeStorage = 'Failed to get storage info';
});
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Storage and Battery Info'),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Battery Percentage: $_batteryPercentage'),
SizedBox(height: 8),
Text('Total Storage: $_totalStorage'),
SizedBox(height: 8),
Text('Used Storage: $_usedStorage'),
SizedBox(height: 8),
Text('Free Storage: $_freeStorage'),
],
),
),
),
);
}
}