Screen Awake in Flutter – Wakelock in Flutter package prevents mobile device screens from turning off. To conserve battery life, mobile devices enter power-saving mode and switch off the screen when not in use.
The wakelock
package simplifies Flutter app wake lock acquisition and release. It supports all Platforms and lets you set a timeout or release the wake lock when the app goes into the background.
Flutter wakelock
package keeps the screen stay awake. This cross-platform package prevent the screen from sleeping while your app is running.
Screen Awake Using Wakelock:
- Add the
wakelock
package to your project by adding this line to pubspec.yaml or run command flutter pub add wakelock:
dependencies: wakelock: ^0.6.2
- Import the
wake_lock
package in your Dart file:
import 'package:wakelock/wakelock.dart';
The enable() method of the Wakelock class can be used to maintain screen brightness. To activate the Wakelock in Flutter whenever a specific page loads, for example:
@override void initState() { super.initState(); Wakelock.enable(); } @override void dispose() { Wakelock.disable(); super.dispose(); }
After the widget is added, the initState()
method calls enable(
). When the widget is removed from the widget tree, dispose()
calls disable()
. This disables the wakelock
when the page is not visible and enables it when it is.
Complete Flutter wakelock example:
import 'package:flutter/material.dart'; import 'package:wakelock/wakelock.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Screen Awake Demo', home: MyHomePage(), ); } } class MyHomePage extends StatefulWidget { @override _MyHomePageState createState() => _MyHomePageState(); } class _MyHomePageState extends State<MyHomePage> { bool _isWakelockEnabled = false; @override void initState() { super.initState(); Wakelock.enable(); } @override void dispose() { Wakelock.disable(); super.dispose(); } void _toggleWakelock() { setState(() { _isWakelockEnabled = !_isWakelockEnabled; if (_isWakelockEnabled) { Wakelock.enable(); // wakelock.toggle enable print('Wakelock Enable $_isWakelockEnabled'); } else { Wakelock.disable(); print('Wakelock Enable $_isWakelockEnabled'); } }); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Row(children: [ Image.asset( 'assets/logo.png', height: 30, ), Text('flutterflux.com') ]), ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Text( 'Press the button below to enable/disable wakelock:', ), SizedBox(height: 20), ElevatedButton( onPressed: _toggleWakelock, child: Text( _isWakelockEnabled ? 'Disable Wakelock' : 'Enable Wakelock', ), ), ], ), ), ); } }
We import the wakelock
package and define a MyHomePage
widget with a Wakelock in Flutter button. To track wakelock state, we define a boolean _isWakelockEnabled
variable in the _MyHomePageState
class. By default, initState()
disables the wakelock. To prevent the widget from running in the background, we disable the wakelock in dispose()
.
Pressing the button invokes _toggleWakelock()
. Toggle the _isWakelockEnabled
variable to enable or disable the Wakelock in Flutter.
In the construct() method, we define the home page UI, which is a column containing text and a button. When pressed, the button invokes the _toggleWakelock()
method to enable or disable the wakelock based on its wording.
Output
Wakelock package Features
Flutter apps can enable or disable the wakelock using the wakelock
package’s easy API. Key features:
- Cross-platform support: The
wakelock
package works on Android and iOS devices, making screen awake wakelock functionality in your Flutter app straightforward. - Easy API: The package provides a simple and clear API to enable or disable the Wakelock in Flutter with a few lines of code.
- Battery-friendly: To save battery, the
wakelock
package disables it while the app is in the background or the screen is off. - Fine-grained control: The software lets you enable and disable the
wakelock
to save battery.
Conclusion
The wakelock
package in Flutter sdk makes it easy to keep your app’s screen awake and prevent the device from sleeping. Playing games or giving presentations can benefit from this.
The package’s API lets you enable or disable the Wakelock in Flutter with a few lines of code on all Platforms. It disables the wakelock when the app is in the background or the screen is off to save battery.