![]() |
VOOZH | about |
The wakelock_plus package in Flutter is used to keep the screen awake. It can be adjusted according to the requirements (how long to stay awake). These are straightforward things that enhance the quality of the app and increase the user-friendliness of the same.
In this article, we will explore the process of keeping the mobile screen awake while not using the application. To do so, we will build a simple application with two buttons, namely:
Create a new Flutter application using the command Prompt. To create a new app, write the below command and run it.
flutter create app_nameTo know more about it refer this article: Creating a Simple Application in Flutter
To add the dependency to the pubspec.yaml file, add wakelock_plus as a dependency in the dependencies part of the pubspec.yaml file, as shown below:
Now run the below command in the terminal.
flutter pub getUse the below line of code in the main.dart file to import the wakelock_plus dependency.
import 'package:wakelock_plus/wakelock_plus.dart';The StatefulWidget can be used to give a simple structure to the application as shown below:
One of the ways to add buttons in an application is to use the inbuilt OutlineButton widget in flutter. These are simple to implement and as discussed earlier, we will add two buttons to the body of the application namely: enable wakelock and disable wakelock as shown below:
To know more about ElevatedButton in Flutter refer this article: Flutter – ElevatedButton Widget.
main.dart:
Output:
Since the output is not possible to show in any form unless tried by yourself, use the above source code and try it for yourself. The above output shows only the basic UI of the application we have built.