Flutter - Disable Screenshot and Video Capture

This tutorial shows you how to make users unable to take screenshot and video recording in a Flutter application.

Sometimes you may want to prevent users from capturing anything from your application. That means you have to make screen capturing or video recording not allowed. If your application uses Flutter, it can be done easily by adding a few lines of code. There is also a package that makes it easier to disable screen capture. In this tutorial, I am going to show you examples of how to disable screenshot and video recording in two ways: modifying native code and using a package .

Using Native Code

For Android, you need to add the following in MainActivity.java file, inside onCreate method.

  getWindow().addFlags(LayoutParams.FLAG_SECURE);

Below is the example of MainActivity.java after added with the above line of code. By adding FLAG_SECURE flag, it prevents the users from capturing the application including the app preview when switching between apps.

  package com.woolha.flutterapp;
  
  import android.os.Bundle;
  import android.view.WindowManager.LayoutParams;
  import io.flutter.app.FlutterActivity;
  import io.flutter.plugins.GeneratedPluginRegistrant;
  
  public class MainActivity extends FlutterActivity {
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().addFlags(LayoutParams.FLAG_SECURE); GeneratedPluginRegistrant.registerWith(this); } }

 

For iOS, you need to modify AppDelegate.m. First, you need to handle when the application is about to move into the background by adding the following code.

  - (void)applicationWillResignActive:(UIApplication *)application{
      self.window.hidden = YES;
  }

When the application becomes active again, add the following to unhide the application's key window:

  - (void)applicationDidBecomeActive:(UIApplication *)application{
      self.window.hidden = NO;
  }

Those methods must be added inside AppDelegate implementation block in AppDelegate.m file.

 

Using flutter_windowmanager

There is a Flutter package named flutter_windowmanager that makes it easier for us to disable screen capture on certain widgets. To use that package, first add it in the dependencies section of pubspec.yaml.

  flutter_windowmanager: ^0.0.2

After that, you need to run packages get (flutter pub get). In order to use that package, add import 'package:flutter_windowmanager/flutter_windowmanager.dart'; in your file. That package allows you to add FLAG_SECURE flag which prevents screenshot and video recording in Android and iOS. Below is the code you need to add.

  await FlutterWindowManager.addFlags(FlutterWindowManager.FLAG_SECURE);

Usually, it can be called inside initState(). Here's the example.

  import 'package:flutter/material.dart';
  import 'package:flutter_windowmanager/flutter_windowmanager.dart';
  
  void main() => runApp(MyApp());
  
  class MyApp extends StatelessWidget {
  
    @override
    Widget build(BuildContext context) {
      return MaterialApp(
        title: 'Woolha.com Flutter Tutorial',
        home: _DisableCaptureExample(),
      );
    }
  }
  
  class _DisableCaptureExample extends StatefulWidget {
    _DisableCaptureExampleState createState() => _DisableCaptureExampleState();
  }
  
  class _DisableCaptureExampleState extends State<_DisableCaptureExample> with TickerProviderStateMixin {
  
    initState() {
      super.initState();
      disableCapture();
    }
  
    Future<void> disableCapture() async {
      await FlutterWindowManager.addFlags(FlutterWindowManager.FLAG_SECURE);
    }
  
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        appBar: AppBar(
          title: Text('Woolha.com Flutter Tutorial'),
        ),
        body: Center(
          child: Text('Woolha.com Disable Capture'),
        ),
      );
    }
  }