How to Disable Screenshot Capture For App In Flutter ??

· 3 min read
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 ??

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.

Leave a Reply