Download StackFood Multi Restaurant, Food Delivery App with Laravel Admin and Restaurant Panel Buy Full App on $79
Download FlutKit - Flutter UI Kit Most beautiful Flutter UI Kit with 20+ Full App, 450+ Screens. Buy Full Kit on $39
Download Fluxstore Multi Vendor WooCommerce/Dokan Plugin compatible WordPress eCommerce Flutter App. Buy Full App on $79

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';

Enable Stay Awake Mode:

Wakelock.enable();

Disable Stay Awake Mode:

Wakelock.disable();

How to Check if Screen is currently on Stay Awake Mode:

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

Full Flutter Code Example:

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")
                  )
               ]
             ),
          )
      );
  }
} 

Output Screenshot:

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

No any Comments on this Article


Please Wait...