android_battery_level_plus 0.0.2 copy "android_battery_level_plus: ^0.0.2" to clipboard
android_battery_level_plus: ^0.0.2 copied to clipboard

PlatformAndroid

A Flutter plugin to access the device's current battery level on Android.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:android_battery_level_plus/battery_plugin.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 _batteryLevel = 'Unknown';
  final _batteryPlugin = BatteryPlugin();

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  Future<void> initPlatformState() async {
    String batteryLevel;
    try {
      final level = await _batteryPlugin.getBatteryLevel();
      batteryLevel = level != null ? level.toString() : 'Unknown';
    } on PlatformException {
      batteryLevel = 'Failed to get battery level.';
    }

    if (!mounted) return;

    setState(() {
      _batteryLevel = batteryLevel;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: const Text('Battery Plugin')),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text(
                'Battery Level: $_batteryLevel%',
                style: const TextStyle(fontSize: 24),
              ),
              const SizedBox(height: 20),
              ElevatedButton(
                onPressed: initPlatformState,
                child: const Text('Refresh Battery Level'),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
6
likes
160
points
133
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

A Flutter plugin to access the device's current battery level on Android.

Repository (GitHub)

License

BSD-3-Clause (license)

Dependencies

flutter, plugin_platform_interface

More

Packages that depend on android_battery_level_plus

Packages that implement android_battery_level_plus