Open In App

Flutter - Battery Level and State

Last Updated : 02 Aug, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will see how to fetch the Battery level and its state using Flutter. To fetch the Battery level(percentage) and its state, we use a package called the battery plus package.

Important terms: 

  • Battery level - we see how much battery is remaining in the mobile.
  • Battery State - we see whether the state of the battery is charging, discharging, or it's full.

Step by Step Implementation

Step 1: Create a new Flutter Application

Create a new Flutter application using the command Prompt. To create a new app, write the following command and run it.

flutter create app_name

To know more about it refer this article: Creating a Simple Application in Flutter

Step 2: Adding the Dependency

To add the dependency to the pubspec.yaml file, add battery_plus as a dependency in the dependencies part of the pubspec.yaml file, as shown below:

dependencies:
flutter:
sdk: flutter
cupertino_icons: ^1.0.2
battery_plus: ^6.2.2

Now, run the below command in the terminal.

flutter pub get

Or

Run the below command in the terminal.

flutter pub add battery_plus

Step 3: Import dependencies

To use libraries, import all of them in the respective .dart file.

Dart
import 'dart:async';

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

Complete Source Code

main.dart:

Dart
import 'dart:async';

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

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({
    Key? key,
  }) : super(key: key);

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  var battery = Battery();
  int percentage = 0;
  late Timer timer;

  // created a Batterystate of enum type
  BatteryState batteryState = BatteryState.full;
  late StreamSubscription streamSubscription;
                                                                             

  @override
  void initState() {
    super.initState();
    // calling the method to get battery state
    getBatteryState();

    // calling the method to get battery percentage
    Timer.periodic(const Duration(seconds: 5), (timer) {
      getBatteryPerentage();
                                                        
    });
    
                                                       
  }
  // method created to display battery percent
  void getBatteryPerentage() async {
    final level = await battery.batteryLevel;
    percentage = level;

    setState(() {});
  }

  // method to know the state of the battery
  void getBatteryState() {
    streamSubscription = battery.onBatteryStateChanged.listen((state) {
      batteryState = state;

      setState(() {});
    });
  }

// Custom widget to add different states of battery
// ignore: non_constant_identifier_names
  Widget BatteryBuild(BatteryState state) {
    switch (state) {

      // first case is for battery full state 
      // then it will show green in color
      case BatteryState.full:
        // ignore: sized_box_for_whitespace
        return Container(
          width: 200,
          height: 200,
          
          // ignore: prefer_const_constructors
          child: (Icon(
            Icons.battery_full,
            size: 200,
            color: Colors.green,
          )),
        );

       // Second case is when battery is charging
       // then it will show blue in color
      case BatteryState.charging:
        
        // ignore: sized_box_for_whitespace
        return Container(
          width: 200,
          height: 200,
          
          // ignore: prefer_const_constructors
          child: (Icon(
            Icons.battery_charging_full,
            size: 200,
            color: Colors.blue,
          )),
        );

        // third case is when the battery is
        // discharged then it will show red in color
      case BatteryState.discharging:
      default:
        
        // ignore: sized_box_for_whitespace
        return Container(
          width: 200,
          height: 200,
          
          // ignore: prefer_const_constructors
          child: (Icon(
            Icons.battery_alert,
            size: 200,
            color: Colors.red,
          )),
        );
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.green,
          title: const Text('GeeksforGeeks',
                            style: TextStyle(color: Colors.white),),
          centerTitle: true,
      ),
      body: Center(
        child: Column(
          
          mainAxisAlignment: MainAxisAlignment.center,
          children:[
            
            // calling the custom widget
            BatteryBuild(batteryState),
            
            // Displaying battery percentage
            Text('Battery Percentage: $percentage',
                 style: const TextStyle(fontSize: 24),)
            ],
        ),
      ),
    );
  }
}

Output:

Battery_Level_and_State

Explanation - 

  • We have created a custom widget where we coded that if the mobile battery is charging, then show us the battery in blue color. So currently my phone is not fully charged, that's why we saw the battery in blue color.
  • If our battery had been 100% then the color would have changed to green.
  • If our battery had been 0% then it would have been in red color. For more clarification, see the image below.

Similar Reads