How to Disable Screenshot Capture For App In Flutter

How to Disable Screenshot Capture For App In Flutter?

When designing and developing a flutter application sometimes applications may some confidential details so in order to protect data we need to Disable Screenshot Capture For App In Flutter?

Discover how to enhance the privacy and security of your Flutter app by disabling screenshot capture in this insightful blog post. Gain a comprehensive understanding of the techniques and code snippets that allow you to prevent users from taking screenshots within your app. Learn how to implement this functionality seamlessly, safeguarding sensitive information and maintaining confidentiality. Whether you’re building a messaging app, a banking app, or any other app that requires heightened security, this tutorial will equip you with the knowledge and tools to disable screenshot capture effectively. Elevate the privacy and protection of your Flutter app by implementing this essential feature today.

How to Disable Screenshot Capture For App In Flutter?

  • Locate your MainActivity class inside the embedded android project dir in your Flutter Project
  • Add the following import to your main activity class:
    import android.view.WindowManager.LayoutParams;
  • Add the following line to your MainActivity’s onCreate method:
    getWindow().addFlags(LayoutParams.FLAG_SECURE);
  • What worked for me was writing the below code in MainActivity.java file.
    @Override
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);
      }

    and importing these packages!

    import android.view.WindowManager; 
    import android.view.WindowManager.LayoutParams;
    import android.os.Bundle; // required for onCreate parameter
  • This works for iOS. In your Runner > AppDelegate.m
    @implementation AppDelegate
    
    - (BOOL)application:(UIApplication *)application
        didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
      [GeneratedPluginRegistrant registerWithRegistry:self];
      // Override point for customization after application launch.
      return [super application:application didFinishLaunchingWithOptions:launchOptions];
    }
    
    - (void)applicationWillResignActive:(UIApplication *)application{
        self.window.hidden = YES;
    }
    
    - (void)applicationDidBecomeActive:(UIApplication *)application{
        self.window.hidden = NO;
    }
    
    @end
  • Screenshots can be prevented very easily by following below two steps. I am using VS code.
  • Step 1 Open the file “mainActivity.kt” using the path android\app\src\main\kotlin\com\example\auth_email\MainActivity.kt
  • Step 2 Add the two lines
    (a) import android.view.WindowManager.LayoutParams; 
    
    (b) getWindow().addFlags(LayoutParams.FLAG_SECURE); in MainActivity: FlutterActivity() section
  • Restart the app

If you are using kotlin open MainActivity.kt

Add below code at end of imports

import android.view.WindowManager.LayoutParams

Add below code at end of super.onCreate(savedInstanceState)

window.addFlags(LayoutParams.FLAG_SECURE)

It’s done.

You can also use a flutter package called flutter_windowmanager

First import its latest version in pubspec.yaml file of your Flutter project and run pub get. Then add the below code inside the widget’s initState() method for which you want to disable screenshot and screen recording.

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

@override
void initState() {
   secureScreen();
   super.initState();
}

If you want to make your whole app screenshot disable just call securescreen() method defined above inside your main() function in main.dart file.

Conclusion:

Thanks for being with us on a flutter journey!!!

In this article, we have been through how to how to disable screenshot capture for app in flutter ??

Do let us know if you need any assistance with flutter development.?? We would love to assist you.

FlutterAgency.com is our portal Platform dedicated to Flutter Technology and Flutter Developers. The portal is full of cool resources from Flutter like Flutter Widget GuideFlutter ProjectsCode libs and etc.

FlutterAgency.com is one of the most popular online portals dedicated to Flutter Technology and daily thousands of unique visitors come to this portal to enhance their knowledge of Flutter.

Abhishek Dhanani

Written by Abhishek Dhanani

Abhishek Dhanani, a skilled software developer with 3+ years of experience, masters Dart, JavaScript, TypeScript, and frameworks like Flutter and NodeJS. Proficient in MySQL, Firebase, and cloud platforms AWS and GCP, he delivers innovative digital solutions.

Leave a comment

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


Discuss Your Project

Connect with Flutter Agency's proficient skilled team for your app development projects across different technologies. We'd love to hear from you! Fill out the form below to discuss your project.

Have Project For Us

Get in Touch

"*" indicates required fields

ready to get started?

Fill out the form below and we will be in touch soon!

"*" indicates required fields