import 'dart:async'; import 'package:flutter/services.dart'; import 'package:screen_protector/extension/color_extension.dart'; class ScreenProtector { static const MethodChannel _channel = MethodChannel('screen_protector'); static void Function()? _onScreenshotListener; static void Function(bool)? _onScreenRecordListener; /// Add callback actions when screenshot or screen record events received, /// Supported for iOS only, do nothing when run on Android. static void addListener( void Function()? screenshotListener, void Function(bool)? screenRecordListener, ) async { _onScreenshotListener = screenshotListener; _onScreenRecordListener = screenRecordListener; _channel.setMethodCallHandler(_methodCallHandler); await _channel.invokeMethod('addListener'); } /// Remove listeners static void _removeListener() { _onScreenshotListener = null; _onScreenRecordListener = null; } /// Remove observers /// Supported for iOS only, do nothing when run on Android. static void removeListener() async { _removeListener(); await _channel.invokeMethod('removeListener'); } static Future _methodCallHandler(MethodCall call) async { if (call.method == 'onScreenshot') { if (null != _onScreenshotListener) { _onScreenshotListener!(); } } else if (call.method == 'onScreenRecord') { dynamic isCaptured = call.arguments; if (null != _onScreenRecordListener && isCaptured != null && isCaptured is bool) { _onScreenRecordListener!(isCaptured); } } } /// Supported for Android only, do nothing when run on iOS. static Future protectDataLeakageOn() async { return await _channel.invokeMethod('protectDataLeakageOn'); } /// Supported for Android and iOS. static Future protectDataLeakageOff() async { return await _channel.invokeMethod('protectDataLeakageOff'); } /// Supported for iOS only, do nothing when run on Android. static Future protectDataLeakageWithBlur() async { return await _channel.invokeMethod('protectDataLeakageWithBlur'); } /// Supported for iOS only, do nothing when run on Android. static Future protectDataLeakageWithBlurOff() async { return await _channel.invokeMethod('protectDataLeakageWithBlurOff'); } /// Supported for iOS only, do nothing when run on Android. static Future protectDataLeakageWithImage(String imageName) async { return await _channel.invokeMethod('protectDataLeakageWithImage', { 'name': imageName, }); } /// Supported for iOS only, do nothing when run on Android. static Future protectDataLeakageWithImageOff() async { return await _channel.invokeMethod('protectDataLeakageWithImageOff'); } /// Supported for iOS only, do nothing when run on Android. static Future protectDataLeakageWithColor(Color color) async { return await _channel.invokeMethod('protectDataLeakageWithColor', { 'hexColor': color.toHex(), }); } /// Supported for iOS only, do nothing when run on Android. static Future protectDataLeakageWithColorOff() async { return await _channel.invokeMethod('protectDataLeakageWithColorOff'); } /// Supported for Android and iOS. static Future preventScreenshotOn() async { return await _channel.invokeMethod('preventScreenshotOn'); } /// Supported for Android and iOS. static Future preventScreenshotOff() async { return await _channel.invokeMethod('preventScreenshotOff'); } /// Supported for iOS only, do nothing when run on Android. static Future isRecording() async { return await _channel.invokeMethod('isRecording'); } }