A Flutter plugin to detect when screenshots are taken on Android devices and iOS Devices. This plugin provides a real-time event-based system that works for Android versions below and above 14 (Android UPSIDE_DOWN_CAKE
). It monitors screenshots using content observers for older Android versions and a dedicated screen capture callback for Android 14+.
- Detect screenshots on Android devices using two approaches:
- Content Observer for Android versions < 14.
- Screen Capture Callback for Android 14+.
- Detect screenshots on iOS.
- Supports real-time screenshot detection using an EventChannel.
- Simple API to listen to screenshot capture events.
Add this plugin to your pubspec.yaml
file:
dependencies:
flutter_screenshot_detect: ^0.1.6
Run the following command to install the package:
flutter pub get
- Import the plugin in your Dart code:
import 'package:flutter_screenshot_detect/flutter_screenshot_detect.dart';
- Initialize the plugin and listen for screenshot events:
import 'package:flutter/material.dart';
import 'package:flutter_screenshot_detect/flutter_screenshot_detect.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _status = 'Waiting for screenshot...';
final FlutterScreenshotDetect _detector = FlutterScreenshotDetect();
@override
void initState() {
super.initState();
// Listen for screenshot detection events
_detector.startListening((event) {
print('Screenshot Taken');
});
}
@override
void dispose() {
_detector.stopListening();
super.dispose();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Screenshot Detector Example'),
),
body: Center(
child: Text(_status),
),
),
);
}
}
Starts listening for screenshot capture events.
startListening()
This method returns a stream of events whenever a screenshot is detected. The event contains the following information:
method
: Indicates how the screenshot was detected (content_observer
orscreen_capture_callback
).timestamp
: The timestamp when the screenshot was detected.path
: The file path of the screenshot (only available forcontent_observer
).
Stops listening for screenshot events.
void stopListening()
The plugin utilizes the Activity.ScreenCaptureCallback
API, introduced in Android 14 (UPSIDE_DOWN_CAKE
), to detect screenshots.
For Android versions lower than 14, the plugin uses the traditional content observer to monitor changes in the MediaStore
for screenshot files.
To detect screenshots on Android, no special permissions are required for Android 14+. However, for Android versions below 14, make sure the app has permission to read the media files. Add the following permissions in your AndroidManifest.xml
if targeting Android versions below 14:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
You can find a full working example in the example/
directory of the repository.
Feel free to fork this repository and submit pull requests. Please follow best coding practices and include tests for any new features or fixes.
This plugin is licensed under the MIT License. See the LICENSE file for more details.