Implementing Screenshot Prevention in Flutter using screen_protector Package

Spread the love

Implementing Screenshot Prevention in Flutter using screen protector Package
Implementing Screenshot Prevention in Flutter using screen protector Package
Implementing Screenshot Prevention in Flutter using screen protector Packag
Implementing Screenshot Prevention in Flutter using screen protector Package

Introduction

The screen_protector package allows you to prevent screenshots in your Flutter app, enhancing the security of sensitive information. This guide will show you how to implement screenshot prevention using this package.

 

Content

1.Add the screen_protector dependency:

Open your pubspec.yaml file and add the screen_protector dependency.

Run flutter pub get to install the package.

 

2.Import the package:

Import the screen_protector package in your Dart file.


import 'package:screen_protector/screen_protector.dart';

3.Initialize the ScreenProtector:

Initialize the ScreenProtector in your app’s main function or at the beginning of your app.


void main() {
  ScreenProtector.enable();
  runApp(MyApp());
}

This will enable the screenshot prevention for your app.

 

4.Disable screenshot prevention (optional):

If you need to allow screenshots at a certain point in your app, you can disable the screenshot prevention.


ScreenProtector.disable();

Call this method to allow screenshots.

 

Sample Code:


import 'package:flutter/material.dart';
import 'package:screen_protector/screen_protector.dart';

class PreventScreenshotPage extends StatefulWidget {
  const PreventScreenshotPage({Key? key}) : super(key: key);

  @override
  _PreventScreenshotPageState createState() => _PreventScreenshotPageState();
}

class _PreventScreenshotPageState extends State<PreventScreenshotPage> {
  @override
  void initState() {
    // For iOS only.
    _addListenerPreventScreenshot();

    // For iOS and Android
    _preventScreenshotOn();
    _checkScreenRecording();
    super.initState();
  }

  @override
  void dispose() {
    // For iOS only.
    _removeListenerPreventScreenshot();

    // For iOS and Android
    _preventScreenshotOff();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Color.fromARGB(255, 19, 222, 230),
        centerTitle: true,
        title: const Text('Prevent from Screenshot'),
      ),
      body: const Center(
        child: Text(
          'Preventing from Screenshot and Screen Recording...',
          style: TextStyle(color: Colors.black54, fontSize: 20, fontWeight: FontWeight.bold),
          textAlign: TextAlign.center,
        ),
      ),
    );
  }

  void _checkScreenRecording() async {
    final isRecording = await ScreenProtector.isRecording();

    if (isRecording) {
      ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
        content: Text('Screen Recording...'),
      ));
    }
  }

  void _preventScreenshotOn() async => await ScreenProtector.preventScreenshotOn();

  void _preventScreenshotOff() async => await ScreenProtector.preventScreenshotOff();

  void _addListenerPreventScreenshot() async {
    ScreenProtector.addListener(() {
      // Screenshot
      debugPrint('Screenshot:');
      ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
        content: Text('Screenshot!'),
      ));
    }, (isCaptured) {
      // Screen Record
      debugPrint('Screen Record:');
      ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
        content: Text('Screen Record!'),
      ));
    });
  }

  void _removeListenerPreventScreenshot() async {
    ScreenProtector.removeListener();
  }
}

Output:

Implementing Screenshot Prevention in Flutter using screen protector Package
Implementing Screenshot Prevention in Flutter using screen protector Package
Implementing Screenshot Prevention in Flutter using screen protector Packag
Implementing Screenshot Prevention in Flutter using screen protector Package

Conclusion

By following these steps, you can prevent screenshots in your Flutter app using the screen_protector package. This can be useful for protecting sensitive information or preventing users from capturing content in your app.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *