75

How to keep an application from locking the screen in flutter?

Is there a flag to turn it off an on? Does flutter SDK expose this?

Something like keepAwake(true);

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Tree
  • 29,135
  • 24
  • 78
  • 98

4 Answers4

127

Note: This package wakelock has some depedencies conflicts with other packages. Use this wakelock_plusinstead. click on Reference As support for the screen plugin that @Tree mentioned has been discontinued and there are some issues with it now, you can use wakelock.
Full disclosure: I am the author of this plugin, however, it is basically a port of the wakelock functionality from the screen plugin, with the issues fixed:

import 'package:wakelock/wakelock.dart';

// To keep the screen on:
Wakelock.enable(); // or Wakelock.toggle(on: true);

// To let the screen turn off again:
Wakelock.disable(); // or Wakelock.toggle(on: false);

Learn more.

Fahad Mustafa
  • 27
  • 1
  • 6
creativecreatorormaybenot
  • 114,516
  • 58
  • 291
  • 402
  • 1
    Where should I add Wakelock.enable() ? I want it on everypage of my application – Andreea Purta Sep 11 '20 at 20:06
  • 3
    Is this still recommended in 2021? I.e. do we really need a third party plugin for something that common?!? Doesn't Flutter provide something? – SePröbläm Feb 08 '21 at 09:07
  • 8
    @SePröbläm Hi, I am still maintaining it :) We recently added **macOS** support as well Also, Flutter is open source - they have first-party plugins, however, they do say themselves that they will abandon their first-party solutions if a better third party project exists. So why would they want to create one for something that works perfectly fine? – creativecreatorormaybenot Feb 08 '21 at 16:17
  • 1
    @creativecreatorormaybenot Good to know. Thank you very much for providing it! – SePröbläm Feb 08 '21 at 16:22
  • I assume this keeps the app running in the background and you can play, for example as well a sound like a "RINGRINGRING" - is it possible to turn on the screen as well somehow? – Toskan Feb 09 '21 at 03:50
  • @AlexSemeniuk That has nothing to do with the display. – creativecreatorormaybenot Mar 09 '21 at 14:57
61

I found plugin that does the job. https://pub.dartlang.org/packages/screen

import 'package:screen/screen.dart';

// Prevent screen from going into sleep mode:
Screen.keepOn(true);

You also need to set permission for android

<uses-permission android:name="android.permission.WAKE_LOCK" />
Tree
  • 29,135
  • 24
  • 78
  • 98
6

This package does the work https://pub.dev/packages/wakelock

It depends on Flutter Wakelock class.

Permissions The wakelock plugin does not require any permissions on any platform. This is because it only enables the screen wakelock and not any partial (CPU) wakelocks that would keep the app alive in the background.

How to Use it?

// to enable the Android and iOS wakelock
Wakelock.enable();

// to disables the wakelock again.
Wakelock.disable();



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


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


class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    Wakelock.enable(); // Here :)
    return MaterialApp(
      home:  MyHomePage(),
    );
  }
}

Note: You have to Stop and Run again

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Ramy Wahid
  • 218
  • 2
  • 6
5

As @creativecreatorormaybenot already answered, you can use wakeLock to keep the screen on. But I wanted to add where to put the Wakelock.enable();. Here a code snippet how I used it and it works fine for me:

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

class MyApp extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    Wakelock.enable();
    return MaterialApp(
      debugShowCheckedModeBanner: false,
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: MainScreen()
    );
  }
}

I hope it will fix your problem. Here is the link to the package: https://pub.dev/packages/wakelock

guerdaa
  • 157
  • 1
  • 7