πŸͺ΄ Screen Lock Detector πŸͺ΄

A Flutter plugin that enables your app to detect screen lock and unlock events across Android and iOS platforms.

The plugin provides native event callbacks for both Android and iOS, making it easy to respond to lock screen changes β€” such as pausing sensitive activities, logging out users, or triggering background operations.

buy me a coffee Ko-fi
Pub Package Pub Likes Pub Score Pub Monthly Downloads Star on Github Forks on Github Contributors Build Status Code size Platform

screen lock detector preview

How it's work?

For iOS, this plugin just listen to UIApplication notification center API:

  • protectedDataWillBecomeUnavailableNotification to detect is locked
  • protectedDataDidBecomeAvailableNotification to detect is unlocked

For Android, this plugin uses KeyguardManager and PowerManager API to check if device is secured lock or display is off combine with listener on Intent actions:

  • ACTION_SCREEN_OFF to detect is locked
  • ACTION_USER_PRESENT to detect is unlocked

Features

  • βœ… Detect when the screen is locked.
  • βœ… Detect when the screen is unlocked.
  • βœ… Supports both event streams and manual status checks.
  • βœ… Works on Android and iOS.

Note This plugin does not work on simulators or emulators because it relies on native system events that are not available in these virtual environments. Please test on a physical device.

Installation

Add screen_lock_detector as a dependency in your pubspec.yaml file:

flutter pub add screen_lock_detector

Usage

Import Plugin

import 'package:screen_lock_detector/screen_lock_detector.dart';

Listen to Event Stream

ScreenLockDetector.statusStream.listen((status) {
    print("Screen status: $status");
    if (status == ScreenStatus.locked) {
    // Do something here when screen is locked
    } else if (status == ScreenStatus.unlocked) {
    // Do something here when screen is unlocked
    }
});

Or Manual Status Check

final status = await ScreenLockDetector.checkScreenStatus();
print("Screen status: $status");

See the example for runnable examples of various usages.

Bugs or Requests

If you encounter any problems feel free to open an issue. If you feel the library is missing a feature, please raise a ticket on GitHub and I'll look into it. Pull request are also welcome.

See Contributing.md.

Support

Don't forget to give it a like πŸ‘ or a star ⭐

Activities

Alt