How to prevent screenshots in particular sections of my flutter app?

1,269

You'll have to use a MethodChannel to do that and call the method enabling/disabling screenshots at will.

You can check here for how to do it. The examples calls a method to fetch the battery level, just replace it with your Kotlin method to enable/disable screenshot.

You can then do something like this to pass the argument to decide if it's enable or disable:

final platform = MethodChannel("your_channel_name");
platform.invokeMethod('setScreenshotEnableStatus',{'enabled': true})

and, from the MethodCall object that the MethodCallHandler receives, you can recover that argument this way:

call.argument('enabled')

Another way of doing it is create two native methods, one for enable and one for disable, and call them accordingly.

Another resource for native integration might be this, I've quickly skimmed the article and it seems ok.

Share:
1,269
Deepak sharma
Author by

Deepak sharma

Updated on December 20, 2022

Comments

  • Deepak sharma
    Deepak sharma over 1 year

    With the help of this thread I am able to prevent screenshots in my app but How can I use it only in a particular section of my app. It makes my whole app unshareable.

    I have a section that shows notes to my premium members. I don't want the user to take screenshots of it.

    My app has MainActivity in kotlin so when I copied the java code from thread mentioned my IDE converted it to Kotlin.

    import android.os.Bundle
    import android.view.WindowManager.LayoutParams
    import androidx.annotation.NonNull;
    import io.flutter.embedding.android.FlutterActivity
    import io.flutter.embedding.engine.FlutterEngine
    import io.flutter.plugins.GeneratedPluginRegistrant
    
    class MainActivity: FlutterActivity() {
        override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
            GeneratedPluginRegistrant.registerWith(flutterEngine);
        }
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            window.setFlags(LayoutParams.FLAG_SECURE, LayoutParams.FLAG_SECURE)
        }
    }