Flutter

[Flutter] 화면 계속 켜두기 (절전모드 전환방지)

devyong 2024. 9. 9. 22:09

앱을 켜두고 아무런 상호작용이 없다면 디바이스 설정에 따라서 절전모드로 전환된다.

하지만 앱에 특성에 따라서 절전모드로 전환되지않고 항상 화면이 켜져있어야 하는경우들이 있다.

 

https://pub.dev/packages/wakelock_plus

 

wakelock_plus | Flutter package

Plugin that allows you to keep the device screen awake, i.e. prevent the screen from sleeping on Android, iOS, macOS, Windows, Linux, and web.

pub.dev

wakelock_plus 패키지는 매우 쉬운 사용이 가능하다.

 

 

1. 패키지설치

$ flutter pub add wakelock_plus

명령어로 설치가 가능하며 pubspec.yaml에 해당 라인이 추가되어있다.

wakelock_plus: ^1.2.8

   해당 라인이 추가되어있다. 명령어가 아닌 pubspec.yaml로  pub get으로 설치가 가능하긴하다.

 

2. 가져오기

import 'package:wakelock_plus/wakelock_plus.dart';

패키지 설치가 정상적이라면 해당 import을 문제없이 가능하다.

대부분 코드를 추가하고 자동 import를 생성할것이라, 직접 import를 작성하지는 않을 것이다.

 

 

3. 사용법

 

활성화/비활성화

wakelock 활성화 : WakelockPlus.enable();

wakelock 비활성화 : WakelockPlus.disable();

 

wakelock 상태점검

bool wakelockEnabled = await WakelockPlus.enabled;

bool형태로 return.