Flutter安全存储插件secrethero_flutter的使用
Flutter安全存储插件secrethero_flutter的使用
简介
Secret Hero是一个用于在应用程序中添加游戏功能的SDK。该SDK使开发者能够集成引人入胜的游戏体验,包括排行榜跟踪和优惠券奖励,从而增强用户在应用中的参与度和留存率。
安装
要将SecretHero库集成到你的项目中,请添加以下依赖项:
$ flutter pub add secrethero_flutter
这将在你的包的pubspec.yaml
文件中添加如下一行(并运行隐式的flutter pub get
):
dependencies:
secrethero_flutter: "latest_version"
设置
初始化Secret Hero
SecretHero
SDK中的初始化函数负责使用特定的配置设置和监听器来初始化SDK。SecretHeroConfig
类负责配置初始化SecretHero
SDK。
SecretHero().initialize(secretHeroConfig, secretHeroListener);
SecretHeroConfig(
token: "your_token",
userId: "user_id",
displayName: "display_name",
theme: SecretHeroTheme(
primaryColor: "#191D21",
secondaryColor: "#FFFFFF",
highlightColor: "#7B61FF",
buttonColor: "#92FFD7",
)
);
token
: 用于认证和与SecretHero SDK交互的令牌。userId
: 与SecretHero会话关联的用户ID。displayName
: 用户在SecretHero环境中的显示名称。theme
:SecretHeroTheme
类封装了各种视觉属性,允许自定义平台的外观。
SecretHeroEventListener
代理作为SecretHero SDK中的回调机制,提供了处理SDK生命周期和操作期间的各种事件和响应的方法。
final secretHeroListener = SecretHeroEventListener(
onInitialized: () {
print("onInitialized");
},
onInitializeFailed: (errorMessage) {
print("onInitializeFailed $errorMessage");
},
onOpen: () {
print("onOpen");
},
onClose: () {
print("onClose");
},
onEvent: (event) {
print("onEvent $event");
}
);
onInitialized()
: 当SecretHero SDK成功初始化时调用。onInitializeFailed(errorMessage: String)
: 在SecretHero SDK初始化失败时触发。onClose()
: 当SecretHero会话关闭或终止时调用。onOpen()
: 当SecretHero会话打开或激活时执行。onEvent()
: 当SecretHero事件累积时执行。
release()
函数在SecretHero中用于释放和重置与SecretHero相关的配置和监听器。需要在onDestroy
时调用。
SecretHero().release();
示例代码
import 'package:flutter/material.dart';
import 'package:secrethero_flutter/secrethero_flutter.dart';
import 'package:secrethero_flutter/secrethero_flutter_method_channel.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final secretHeroConfig = SecretHeroConfig(
token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJjbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJjbGllbnRJRCI6IjEiLCJpYXQiOjE2OTgwNzA3Mjl9.Tf_QBI0PM8EHVI0xqmFdqkjtcI5-q2ciyGhnASBhfY8",
userId: "99999999",
displayName: "TestFlutterUser",
iconImage: "assets/images/SDKIcon.jpg",
country: "tr",
language: "tr",
isLayoutLTR: true,
theme: SecretHeroTheme(
primaryColor: "#191D21",
secondaryColor: "#FFFFFF",
highlightColor: "#7B61FF",
buttonColor: "#92FFD7",
)
);
final secretHeroListener = SecretHeroEventListener(
onInitialized: () {
print("onInitialized");
},
onInitializeFailed: (errorMessage) {
print("onInitializeFailed $errorMessage");
},
onOpen: () {
print("onOpen");
},
onClose: () {
print("onClose");
},
onEvent: (event, extras) {
print("onEvent $event $extras");
}
);
@override
void initState() {
super.initState();
SecretHero().initialize(secretHeroConfig, secretHeroListener);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Column(
children: [
TextButton(
style: ButtonStyle(
foregroundColor: MaterialStateProperty.all<Color>(Colors.blue),
),
onPressed: () {
SecretHero().initialize(secretHeroConfig, secretHeroListener);
},
child: const Text('SecretHero - initialize'),
),
TextButton(
style: ButtonStyle(
foregroundColor: MaterialStateProperty.all<Color>(Colors.blue),
),
onPressed: () {
SecretHero().start();
},
child: const Text('SecretHero - start'),
),
TextButton(
style: ButtonStyle(
foregroundColor: MaterialStateProperty.all<Color>(Colors.blue),
),
onPressed: () {
SecretHero().release();
},
child: const Text('SecretHero - release'),
)
],
),
),
);
}
}
更多关于Flutter安全存储插件secrethero_flutter的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复