3

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)
    }
}

Nensi Kasundra
  • 1,980
  • 6
  • 21
  • 34
Deepak sharma
  • 71
  • 1
  • 9

2 Answers2

1

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.

magicleon94
  • 4,887
  • 2
  • 24
  • 53
0

You can use the Flutter Package flutter_windowmanager which was made for this purpose.

While Android natively supports a range of window modes, there was no good way to set these dynamically within a running Flutter application - instead requiring that these flags are set within the native MainActivity of the Flutter application itself.

In our App, we only wished to disable screenshots for specific screens, rather than across the entire application lifecycle. This can now be accomplished by simply calling:

await FlutterWindowManager.addFlags(FlutterWindowManager.FLAG_SECURE);

for the relevant screen.

This can further be toggled for a specific screen by either using a RouteAware mixin, or through direct toggling in initState() and dispose() methods in the case of stateful widgets.

Abdul Malik
  • 1,158
  • 6
  • 15