Secure Your Flutter App from Screenshots & Screen Recording on Android

By VisioFolio Admin
June 25, 2025
 Secure Your Flutter App from Screenshots & Screen Recording on Android

πŸ“΅ Secure Your Flutter App from Screenshots & Screen Recording on Android

In today’s world, protecting sensitive data inside your mobile app is more important than ever. Whether you're displaying confidential documents, private messages, or financial information, preventing users from taking screenshots or recording the screen can be crucial.

In this tutorial, we’ll show you how to implement secure screen protection in Flutter using native Android functionality with just a few lines of code.

πŸ”’ This guide uses Android’s FLAG_SECURE API to prevent screenshots and screen recording β€” all integrated seamlessly into your Flutter project.


πŸš€ What You'll Build

A simple Flutter app with a toggle switch that enables or disables secure screen mode at runtime.

πŸ” Features:

  • πŸ›‘οΈ Prevent screenshots and screen recording on Android
  • πŸ“± Toggle secure screen mode dynamically
  • πŸ”„ Clean, reusable code using MethodChannel

πŸ›  Prerequisites

  • Flutter SDK installed
  • Android device/emulator (iOS doesn't support FLAG_SECURE)
  • Basic understanding of platform channels in Flutter

🧱 Project Structure Overview

lib/ β”œβ”€β”€ controller/ β”‚ └── screen_security.dart # Dart bridge for secure screen control └── home_screen.dart # UI with toggle switch for secure mode

android/ └── app/src/main/kotlin/com/example/secure_screen/ └── MainActivity.kt # Native Android implementation


✏️ Step 1: Add Native Android Code

Update your MainActivity.kt to handle secure mode toggle using a method channel:

// android/app/src/main/kotlin/com/example/secure_screen/MainActivity.kt

package com.example.secure_screen

import android.view.WindowManager
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel

class MainActivity : FlutterActivity() {
    private val CHANNEL = "screen_security"

    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)

        MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler {
            call, result ->
            when (call.method) {
                "enableSecureMode" -> {
                    window.setFlags(
                        WindowManager.LayoutParams.FLAG_SECURE,
                        WindowManager.LayoutParams.FLAG_SECURE
                    )
                    result.success(null)
                }
                "disableSecureMode" -> {
                    window.clearFlags(WindowManager.LayoutParams.FLAG_SECURE)
                    result.success(null)
                }
                else -> result.notImplemented()
            }
        }
    }
}

🧩 Step 2: Create the Flutter Bridge Now let’s connect Flutter to the native code.

// lib/controller/screen_security.dart
import 'package:flutter/services.dart';

class ScreenSecurity {
  static const _channel = MethodChannel('screen_security');

  static Future<void> enableSecureMode() async {
    await _channel.invokeMethod('enableSecureMode');
  }

  static Future<void> disableSecureMode() async {
    await _channel.invokeMethod('disableSecureMode');
  }
}

This ScreenSecurity class provides easy-to-use Dart methods for toggling secure mode.

πŸ§ͺ Step 3: Build the UI with Toggle Switch Let’s wire it all up with a SwitchListTile in your home screen:

// lib/home_screen.dart

import 'package:flutter/material.dart';
import 'controller/screen_security.dart';

class HomeScreen extends StatefulWidget {
  const HomeScreen({super.key});

  @override
  State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  bool isSecureModeEnabled = false;

  @override
  void dispose() {
    ScreenSecurity.disableSecureMode(); // Clean up
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Secure Screen Demo')),
      body: Center(
        child: SwitchListTile(
          value: isSecureModeEnabled,
          onChanged: (v) {
            setState(() {
              isSecureModeEnabled = v;
              if (v) {
                ScreenSecurity.enableSecureMode();
              } else {
                ScreenSecurity.disableSecureMode();
              }
            });
          },
          title: const Text('Enable Secure Mode'),
        ),
      ),
    );
  }
}

πŸ“± Platform Support

PlatformStatus
βœ… AndroidSupported via FLAG_SECURE
❌ iOSNot Supported (iOS doesn’t allow controlling screenshots programmatically)

βœ… Best Practices Use secure mode when displaying sensitive content like:

  • Documents

  • Private messages

  • Financial or medical data

Always disable secure mode when it's no longer needed, such as in dispose().

πŸ™Œ Credits Built with ❀️ using Flutter + native Android integration.

πŸ“¦ Source Code: GitHub Repository

AI Powered Summary

Too busy to read the full article? Get a quick AI-generated summary below.