How to hide the app content with splash screen when app is in backgorund/inactive in flutter with package or method channel

73 views Asked by At

Hey guys i am working on an app where the client wants to show the splash screen when app is in background mode not a black screen using flag secure, anyone who can help me with it for android and ios suggest a package which can help me understand it or help me with method channel

Here is the code for both

Android

class MainActivity: FlutterFragmentActivity() {

    private val CHANNEL = "security"

    override fun onCreate(savedInstanceState: Bundle?) {
        if (intent.getIntExtra("org.chromium.chrome.extra.TASK_ID", -1) == this.taskId) {
            this.finish()
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        }
        super.onCreate(savedInstanceState)
    }

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

        
        setupMethodChannel(flutterEngine)
    }



        private fun setupMethodChannel(flutterEngine: FlutterEngine) {
        MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler { call, result ->
            when (call.method) {
                "enableAppSecurity" -> {
                    enableAppSecurity()
                    result.success(null)
                }
                "disableAppSecurity" -> {
                    disableAppSecurity()
                    result.success(null)
                }
                else -> result.notImplemented()
            }
        }
    }

    override fun onWindowFocusChanged(hasFocus: Boolean) {
        super.onWindowFocusChanged(hasFocus)
        toggleAppSecurity(hasFocus)
    }

    override fun onPause() {
        super.onPause()
        enableAppSecurity()
    }

    override fun onResume() {
        super.onResume()
        disableAppSecurity()
    }

    private fun toggleAppSecurity(hasFocus: Boolean) {
        if (hasFocus) {
            disableAppSecurity()
        } else {
            enableAppSecurity()
        }
    }

    private fun enableAppSecurity() {
        window.setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE)
    }

    private fun disableAppSecurity() {
        window.clearFlags(WindowManager.LayoutParams.FLAG_SECURE)
    }
}

IOS

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {

   private var flutterViewController: FlutterViewController!
  private var securityChannel: FlutterMethodChannel!
  private var blurEffectView: UIVisualEffectView?
  private var isInBackground: Bool = false // Track whether app is in background


  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    GeneratedPluginRegistrant.register(with: self)
    setupFlutterCommunication()
//      FirebaseApp.configure()
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }

  private func setupFlutterCommunication() {
    flutterViewController = window?.rootViewController as? FlutterViewController
    securityChannel = FlutterMethodChannel(
      name: "security",
      binaryMessenger: flutterViewController.binaryMessenger
    )

    securityChannel.setMethodCallHandler(handle)
  }

  override func applicationDidEnterBackground(_ application: UIApplication) {
    isInBackground = true // App entered background
    enableAppSecurity()
  }

  override func applicationDidBecomeActive(_ application: UIApplication) {
    // Check if the app was in background before becoming active
    if isInBackground {
      disableAppSecurity()
      isInBackground = false
    }
  }

  private func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
    switch call.method {
    case "enableAppSecurity":
      result(nil)
    case "disableAppSecurity":
      result(nil)
    default:
      result(FlutterMethodNotImplemented)
    }
  }

  private func enableAppSecurity() {
    let blurEffect = UIBlurEffect(style: .light)
    blurEffectView = UIVisualEffectView(effect: blurEffect)
    blurEffectView?.frame = window!.frame
    window?.addSubview(blurEffectView!)
  }

  private func disableAppSecurity() {
    blurEffectView?.removeFromSuperview()
  }
}
0

There are 0 answers