Tree
Tree

Reputation: 31391

How to keep application awake in flutter?

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

Upvotes: 83

Views: 61511

Answers (5)

swargam vinay sunny
swargam vinay sunny

Reputation: 1

if weaklock does not work please add weaklock_plus version at starting of dependencies as first line of dependencie and flutter clean flutter pub get and flutter build apk

Upvotes: 0

creativecreatorormaybenot
creativecreatorormaybenot

Reputation: 126854

wakelock_plus

You can prevent the device from sleeping / keep the screen turned on by using the wakelock_plus plugin.
It is maintained by the Flutter Community and originally written by me. The screen plugin mentioned in another answer is no langer being maintained.

Usage example

import 'package:wakelock_plus/wakelock_plus.dart';

// To keep the screen on:
WakelockPlus.enable(); // or WakelockPlus.toggle(on: false);

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

Learn more.

Installing

You can use this plugin if you depend on it.

Run this command:

flutter pub add wakelock_plus

This will add a line like this to your package's pubspec.yaml (and run an implicit flutter pub get):

dependencies:
  wakelock_plus: ^1.2.8

Upvotes: 138

Ramy Wahid
Ramy Wahid

Reputation: 443

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

Upvotes: 6

guerdaa
guerdaa

Reputation: 216

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

Upvotes: 5

Tree
Tree

Reputation: 31391

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" />

Upvotes: 63

Related Questions