Search code examples
flutter

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


Solution

  • 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