Member-only story

Prevent Screenshot and Video Recording in Flutter

Gulshan Yadav
2 min readMay 12, 2020

--

Introduction

In this article, you will learn how you can prevent users from taking screenshots or recording videos of the app.

To achieve this, there are two methods which I know. 1st method is useful when you want to protect any particular screen of the app. Using the 2nd method you can protect the whole app irrespective of any particular screen.

So, let’s start with the methods.

Method-1:

In this method, we’ll use the flutter_windowmanager package.

So, define this package inside pubspec.yaml file.

flutter_windowmanager: ^0.0.1+1

Now, get dependencies using

flutter pub get

You have to add a few lines of code in your StatefulWidget. You need to call a method of FlutterWindowManager using await and async. I have added that method inside secureScreen() function and then called secureScreen() inside initState().

Future<void> secureScreen() async {await FlutterWindowManager.addFlags(FlutterWindowManager.FLAG_SECURE); }@override
void initState() {
secureScreen(); super.initState();
}

Example:

Method-2:

In this method, we will write native codes in Android and iOS both differently to secure our whole app at once.

For Android:

  1. Locate your MainActivity class inside the embedded android project directory in your Flutter project.
  2. Add the following import to your main activity class:
import android.view.WindowManager.LayoutParams;

3. Add the following line to your MainActivity’s onCreate method:

getWindow().addFlags(LayoutParams.FLAG_SECURE);

--

--

Gulshan Yadav
Gulshan Yadav

Written by Gulshan Yadav

Freelancer - Flutter | Android | Blockchain | NodeJs | Technical Writer | Open Source Contributor | LinkedIn — https://www.linkedin.com/in/mrgulshanyadav

Responses (7)

Write a response