get_storage_battery 1.1.0 copy "get_storage_battery: ^1.1.0" to clipboard
get_storage_battery: ^1.1.0 copied to clipboard

A Flutter plugin to monitor and manage battery usage and storage information on Android and iOS devices.

example/lib/main.dart

import 'package:flutter/material.dart';
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();
  }

  Future<void> initPlatformState() async {
    try {
      int batteryLevel = await _getStorageBatteryPlugin.getBatteryPercentage();
      Map<String, int> storageInfo =
          await _getStorageBatteryPlugin.getStorageInfo();

      setState(() {
        _batteryPercentage = '$batteryLevel%';
        _totalStorage = '${storageInfo['total']! / (1024 * 1024 * 1024)} GB';
        _usedStorage = '${storageInfo['used']! / (1024 * 1024 * 1024)} GB';
        _freeStorage = '${storageInfo['free']! / (1024 * 1024 * 1024)} GB';
      });
    } 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'),
            ],
          ),
        ),
      ),
    );
  }
}
2
likes
130
points
23
downloads

Publisher

unverified uploader

Weekly Downloads

A Flutter plugin to monitor and manage battery usage and storage information on Android and iOS devices.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

flutter, plugin_platform_interface

More

Packages that depend on get_storage_battery