Flutter安全屏幕插件tbib_secure_screen的使用

Flutter安全屏幕插件tbib_secure_screen的使用

插件介绍

这个插件用于在保存截图或录制屏幕时,屏幕会显示空白。

开始使用

  • Android配置:无需进行任何操作。
  • iOS配置:前往 AppDelegate.swift 文件。
import UIKit
import Flutter
import tbib_secure_screen

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
      TbibSecureScreenPlugin.register(with: self.registrar(forPlugin: "tbib_secure_screen")!)
      TbibSecureScreenPlugin.shared?.initSecure()
      GeneratedPluginRegistrant.register(with: self)
      return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }

  override func applicationWillResignActive(
    _ application: UIApplication
  ) {
     TbibSecureScreenPlugin.shared?.blurScreen()
  }
  override func applicationDidBecomeActive(
    _ application: UIApplication
  ) {
     TbibSecureScreenPlugin.shared?.removeBlurScreen()
  }
}

注意事项

在 iOS 上,屏幕将在应用程序中的任何地方变为空白,而不是特定的屏幕,这意味着它将自动激活。

在 Android 上,工作在特定的屏幕上。

在 Flutter (Android) 中激活插件

你需要调用以下代码来激活插件:

TBIBSecureScreen().setSecureScreen();

在 Flutter (Android) 中禁用插件

你需要调用以下代码来禁用插件:

TBIBSecureScreen().setUnsecureScreen();

示例代码

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:tbib_secure_screen/tbib_secure_screen.dart';

void main() {
  TBIBSecureScreen().setSecureScreen();
  runApp(const MyApp());
}

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

  [@override](/user/override)
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';
  final _tbibSecureScreenPlugin = TBIBSecureScreen();

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Text('Running on: $_platformVersion\n'),
        ),
      ),
    );
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String platformVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.
    // We also handle the message potentially returning null.
    try {
      platformVersion = await _tbibSecureScreenPlugin.getPlatformVersion() ?? 'Unknown platform version';
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
    });
  }

  [@override](/user/override)
  void initState() {
    super.initState();
    initPlatformState();
  }
}

更多关于Flutter安全屏幕插件tbib_secure_screen的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter安全屏幕插件tbib_secure_screen的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,以下是如何在Flutter项目中使用tbib_secure_screen插件的一个示例代码案例。这个插件用于实现安全屏幕功能,通常用于需要防止屏幕截图或屏幕录制的情况,例如显示敏感信息时。

首先,你需要在你的pubspec.yaml文件中添加tbib_secure_screen依赖:

dependencies:
  flutter:
    sdk: flutter
  tbib_secure_screen: ^最新版本号  # 替换为当前最新版本号

然后运行flutter pub get来安装依赖。

接下来,你可以在你的Flutter应用中使用这个插件。以下是一个简单的示例,展示如何在一个页面上启用安全屏幕功能:

import 'package:flutter/material.dart';
import 'package:tbib_secure_screen/tbib_secure_screen.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Secure Screen Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: SecureScreenDemo(),
    );
  }
}

class SecureScreenDemo extends StatefulWidget {
  @override
  _SecureScreenDemoState createState() => _SecureScreenDemoState();
}

class _SecureScreenDemoState extends State<SecureScreenDemo> {
  bool _isSecureScreenEnabled = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Secure Screen Demo'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'Secure Screen is $_isSecureScreenEnabled',
              style: TextStyle(fontSize: 20),
            ),
            SizedBox(height: 20),
            ElevatedButton(
              onPressed: () async {
                setState(() {
                  _isSecureScreenEnabled = !_isSecureScreenEnabled;
                });

                if (_isSecureScreenEnabled) {
                  await TbibSecureScreen.enableSecureScreen();
                } else {
                  await TbibSecureScreen.disableSecureScreen();
                }
              },
              child: Text(_isSecureScreenEnabled ? 'Disable Secure Screen' : 'Enable Secure Screen'),
            ),
          ],
        ),
      ),
    );
  }

  @override
  void dispose() {
    // Ensure to disable the secure screen when the widget is disposed
    if (_isSecureScreenEnabled) {
      TbibSecureScreen.disableSecureScreen().then((_) => super.dispose());
    } else {
      super.dispose();
    }
  }
}

在这个示例中,我们创建了一个简单的Flutter应用,其中包含一个按钮用于启用或禁用安全屏幕。当点击按钮时,应用会调用TbibSecureScreen.enableSecureScreen()TbibSecureScreen.disableSecureScreen()方法来启用或禁用安全屏幕功能。

请注意,这个插件的具体实现可能依赖于底层操作系统提供的API,因此在不同平台(如Android和iOS)上的行为可能会有所不同。务必参考插件的官方文档和示例代码,以确保在不同平台上都能正确实现安全屏幕功能。

此外,由于安全性的考虑,某些操作可能需要额外的权限或配置,例如防止屏幕截图或录制可能需要在Android的AndroidManifest.xml或iOS的Info.plist文件中进行配置。务必遵循插件文档中的指导进行这些配置。

回到顶部