Open In App

Flutter – Keep Application Awake with Wakelock

Last Updated : 26 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article we will learn how to prevent device from getting into sleep mode. If you are adding some features like video calling app, video creating app, or many more you need to awake the screen when the app is open. So we can do the same thing in flutter with just a few steps.

Step By Step Implementation

Let’s start the implementation for awaking the device on just 1 button click

Step 1: Create flutter app 

Create a simple flutter app using command

Dart




flutter create .


Step 2: Add the wakelock_plus package

Add the wake lock plus package in the project either using command or manually.

Dart




dependencies:
  flutter:
    sdk: flutter
  wakelock_plus: ^1.1.3


Step 3: Code for enabling  and disabling preventing for sleeping mode 

For your understanding we will add 2 buttons in code. First one for enabling the sleep mode as off or second to disable the same.

Dart




Padding(
        padding: const EdgeInsets.all(8.0),
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              ElevatedButton(
                  onPressed: () {
                    // To enable the awake mode
                    WakelockPlus.enable();
                  },
                  child: const Text("Wakelock Enable")),
              const Text(
                "After Tapping previous button this device will not go in sleep mode either there is some activity or not",
                style: TextStyle(fontSize: 22),
              ),
              ElevatedButton(
                  onPressed: () {
                    // To disble the sleep mode
                    WakelockPlus.disable();
                  },
                  child: const Text("Wakelock Disable")),
              const Text(
                "After Tapping previous button this device will go in sleep mode if there is no activity for particular time which is set in your settings",
                style: TextStyle(fontSize: 22),
              )
            ],
          ),
        ),
      ),


If you want to awake the app every time user uses your app then you can do  by adding this code in main function

Dart




// This code will be helpful when you want to 
// awake the device when this application is running by users
  
void main() {
  // This function is used so that every line of 
  // code is complete before executing of runApp function
  WidgetsFlutterBinding.ensureInitialized();
  // Prevent Device from Sleep mode
  WakelockPlus.enable();
  runApp(const MyApp());
}


Note: Avoid using this code  in main function because it may consume lots of battery which user didn;t want.So make sure to use this command for very crucial apps only or only use when it required

Here you go. You have successfully built the application where you can control either to awake the device or not.

Output:

Image:

WhatsApp-Image-2023-10-29-at-122655-PMVideo:

Normal Sleep Time for device is set to be 15 seconds

When Wakelock button is enabled

When We have disable wakelock

Some Common Use Cases

Media players, navigation apps, or apps that require constant monitoring

How to test whether wake lock is enable or not?

  1. Set your display time as minimum time in your device setting.
  2. Now open the Flutter App and click the enable button and wait for minimum time that you have set in your device settings.
  3. Please make sure not to do any activity in your app during this testing like touching ,clicking.
  4. After the minimum time completes if the device is open or awake it means you have successfully enable this feature


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads