How to Keep App Screen Awake in Flutter App

In this example, we are going to show you how to keep the application or screen awake by Flutter app so that to prevent the screen from locking. We have used Wake Lock feature of Mobile. See the example below:

See this also: How to Change the Screen Brightness with Flutter App

First of all, add wakelock Flutter package in your project by adding the following lines on pubspec.yaml file.

dependencies:
  flutter:
    sdk: flutter
  wakelock: ^0.5.6

You can achieve this with screen Flutter package as well, but we choose wakelock because it is null safety and it does not require any permission declaration.

Import package in your script:

import 'package:wakelock/wakelock.dart';

Wakelock.enable();

Wakelock.disable();

bool ison = await Wakelock.enabled;
if(ison){
    print("Screen is on stay awake mode");
}else{
    print("Screen is not on stay awake mode.");
}

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

void main() {
  runApp( MaterialApp(
       home: Home()
  ));
}

class Home extends  StatefulWidget {
  @override
  State<Home> createState() => _HomeState();
}

class _HomeState extends State<Home> {

  Future<bool> checkAwake() async {
    bool ison = await Wakelock.enabled;
    return ison;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
         appBar: AppBar(
            title: Text("Keep Screen Awake on Flutter App"),
            backgroundColor: Colors.redAccent,
         ),
          body: Container(
             alignment: Alignment.center,
             padding: EdgeInsets.all(20),
             child: Column(
               children:[

                  FutureBuilder<bool>(
                      future: checkAwake(),
                      builder: (BuildContext context, AsyncSnapshot<bool> snapshot) {
                           if(snapshot.hasData){
                               if(snapshot.data == true){
                                   return Text("Screen is on stay awake mode.");
                               }else{
                                  return Text("Screen is not on stay awake mode.");
                               }
                           }else{
                              return Text("Error while reading awake state.");
                           }
                      }
                  ),

                  ElevatedButton(
                    onPressed: (){
                        Wakelock.enable();
                        setState(() {});
                    }, 
                    child: Text("Enable Stay Awake")
                  ),

                  ElevatedButton(
                    style: ElevatedButton.styleFrom(primary: Colors.redAccent),
                    onPressed: (){
                       Wakelock.disable();
                       setState(() {});
                    }, 
                    child: Text("Disable Stay Awake")
                  )
               ]
             ),
          )
      );
  }
} 

In this way, you can enable or disable stay awake screen mode on Flutter App.

No any Comments on this Article


Please Wait...