Flutter安全应用保护插件secure_application的使用
Flutter安全应用保护插件secure_application的使用
Flutter插件secure_application
(之前名为secure_window)允许开发者保护应用程序内容,防止在特定情况下被查看。该插件支持iOS、Android、Web和Windows平台,并提供了多种方式来确保应用的安全性。
使用方法
安装
在项目的pubspec.yaml
文件中添加secure_application
作为依赖项:
dependencies:
secure_application: ^latest_version
导入
在Dart代码中导入secure_application
包:
import 'package:secure_application/secure_application.dart';
示例代码
以下是一个完整的示例demo,展示了如何使用secure_application
来保护应用内容:
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:secure_application/secure_application.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
bool failedAuth = false;
double blurr = 20;
double opacity = 0.6;
StreamSubscription<bool>? subLock;
List<String> history = [];
@override
void initState() {
super.initState();
}
@override
void dispose() {
subLock?.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
var width = MediaQuery.of(context).size.width * 0.8;
return MaterialApp(
home: SecureApplication(
nativeRemoveDelay: 1000,
onNeedUnlock: (secure) async {
print('need unlock maybe use biometric to confirm and then sercure.unlock() or you can use the lockedBuilder');
// 可以在这里实现用户认证逻辑,例如使用生物识别或其他方式
// 如果认证成功,调用 secure.unlock()
// 如果认证失败,返回 SecureApplicationAuthenticationStatus.FAILED
return null;
},
onAuthenticationFailed: () async {
setState(() {
failedAuth = true;
});
print('auth failed');
},
onAuthenticationSucceed: () async {
setState(() {
failedAuth = false;
});
print('auth success');
},
child: Builder(builder: (context) {
if (subLock == null)
subLock = SecureApplicationProvider.of(context, listen: false)
?.lockEvents
.listen((s) => history.add(
'${DateTime.now().toIso8601String()} - ${s ? 'locked' : 'unlocked'}'));
return SecureGate(
blurr: blurr,
opacity: opacity,
lockedBuilder: (context, secureNotifier) => Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ElevatedButton(
child: Text('UNLOCK'),
onPressed: () => secureNotifier?.authSuccess(unlock: true),
),
ElevatedButton(
child: Text('FAIL AUTHENTICATION'),
onPressed: () => secureNotifier?.authFailed(unlock: true),
),
],
)),
child: Scaffold(
appBar: AppBar(
title: const Text('Secure Window Example'),
),
body: Center(
child: Builder(builder: (context) {
var valueNotifier = SecureApplicationProvider.of(context);
if (valueNotifier == null)
throw new Exception(
'Unable to find secure application context');
return ListView(
children: <Widget>[
Text('This is secure content'),
ValueListenableBuilder<SecureApplicationState>(
valueListenable: valueNotifier,
builder: (context, state, _) => state.secured
? Column(
children: <Widget>[
ElevatedButton(
onPressed: () => valueNotifier.open(),
child: Text('Open app'),
),
state.paused
? ElevatedButton(
onPressed: () =>
valueNotifier.unpause(),
child: Text('resume security'),
)
: ElevatedButton(
onPressed: () =>
valueNotifier.pause(),
child: Text('pause security'),
),
],
)
: ElevatedButton(
onPressed: () => valueNotifier.secure(),
child: Text('Secure app'),
),
),
failedAuth
? Text(
'Auth failed we cleaned sensitive data',
style: TextStyle(color: Colors.red),
)
: Text(
'Auth success',
style: TextStyle(color: Colors.green),
),
FlutterLogo(
size: width,
),
StreamBuilder(
stream: valueNotifier.authenticationEvents,
builder: (context, snapshot) =>
Text('Last auth status is: ${snapshot.data}'),
),
ElevatedButton(
onPressed: () => valueNotifier.lock(),
child: Text('manually lock'),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
children: <Widget>[
Text('Blurr:'),
Expanded(
child: Slider(
value: blurr,
min: 0,
max: 100,
onChanged: (v) => setState(() => blurr = v)),
),
Text(blurr.floor().toString()),
],
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
children: <Widget>[
Text('opacity:'),
Expanded(
child: Slider(
value: opacity,
min: 0,
max: 1,
onChanged: (v) =>
setState(() => opacity = v)),
),
Text((opacity * 100).floor().toString() + "%"),
],
),
),
...history.map<Widget>((h) => Text(h)).toList(),
],
);
}),
),
),
);
}),
),
);
}
}
关键点说明
- SecureApplication:这是必需的顶级组件,用于管理整个应用的安全状态。
- SecureGate:用于包裹需要保护的内容,当应用锁定时,这些内容会被模糊处理。
- 认证逻辑:在
onNeedUnlock
回调中实现用户认证逻辑,可以使用生物识别等方式。 - 事件流:通过监听
authenticationEvents
和lockEvents
来响应认证和锁定事件。 - UI更新:根据认证结果更新UI,显示不同的提示信息。
通过以上步骤,你可以有效地保护你的Flutter应用内容,确保其在特定情况下不会被未经授权的用户查看。
更多关于Flutter安全应用保护插件secure_application的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复