screen_capture_event 1.3.0
screen_capture_event: ^1.3.0 copied to clipboard
Catch screenshot and screen recording events on Android and iOS, using the system callbacks on Android 14 and 15 and later.
import 'package:flutter/material.dart';
import 'package:screen_capture_event/screen_capture_event.dart';
void main() {
runApp(const MaterialApp(
home: ExampleScreenListener(),
));
}
class ExampleScreenListener extends StatefulWidget {
const ExampleScreenListener({super.key});
@override
State<ExampleScreenListener> createState() => _ExampleScreenListenerState();
}
class _ExampleScreenListenerState extends State<ExampleScreenListener> {
final ScreenCaptureEvent screenListener = ScreenCaptureEvent();
final List<String> events = [];
String text = "Do Screenshot or Screen Record";
bool preventScreenshot = false;
void log(String event) {
final time = TimeOfDay.now().format(context);
setState(() {
text = event;
events.insert(0, "$time $event");
});
}
@override
void initState() {
screenListener.addScreenRecordListener((recorded) {
log(recorded ? "Start Recording" : "Stop Recording");
});
screenListener.addScreenShotListener((filePath) {
log(filePath.isEmpty
? "Screenshot taken"
: "Screenshot stored on : $filePath");
});
screenListener.watch();
super.initState();
}
@override
void dispose() {
screenListener.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text("Example Screen Capture Event")),
body: ListView(
padding: const EdgeInsets.all(30),
children: [
Text(text, textAlign: TextAlign.center),
const SizedBox(height: 10),
///Prevent Button
TextButton(
style: TextButton.styleFrom(
backgroundColor: Theme.of(context).primaryColor),
onPressed: () {
setState(() {
preventScreenshot = !preventScreenshot;
});
screenListener.preventAndroidScreenShot(preventScreenshot);
},
child: Text(
preventScreenshot
? "Allow Screenshot (ANDROID)"
: "Prevent Screenshot (ANDROID)",
style: const TextStyle(color: Colors.white),
),
),
///Check Record Button
TextButton(
style: TextButton.styleFrom(
backgroundColor: Theme.of(context).primaryColor),
onPressed: () {
screenListener.isRecording().then((value) {
log(value ? "You're Still Recording" : "You're not recording");
});
},
child: const Text(
"Check Record Status",
style: TextStyle(color: Colors.white),
),
),
const SizedBox(height: 20),
Text("Events (${events.length})"),
for (final event in events) Text(event),
],
),
);
}
}