flutter_kalapa_bsdk 1.0.14
flutter_kalapa_bsdk: ^1.0.14 copied to clipboard
A Flutter plugin designed to capture a client’s digital footprint from both iOS and Android devices and upload it to the Kalapa service for future processing of scorecards and fragments. It provides a [...]
example/lib/main.dart
import 'dart:math';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:flutter_kalapa_bsdk/kalapa_behavior_score_flutter.dart';
import 'package:flutter_kalapa_bsdk/kalapa_behavior_score_flutter_platform_interface.dart';
import 'package:flutter_kalapa_bsdk/kalapa_behavior_score_flutter.dart';
void main() {
runApp(
const KalapaBehaviorWidget(
key: Key("root_layout"),
startTracking: true,
continuousTracking: true,
child: MyApp(),
),
);
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
static Environment appEnv = Environment.prod;
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _platformVersion = 'Unknown';
final klpBehaviorScore = KalapaBehaviorScoreFlutter();
final TextEditingController _emailController = TextEditingController();
final TextEditingController _passwordController = TextEditingController();
@override
void initState() {
super.initState();
initPlatformState();
BehaviorModule.startTracking(continuousTracking: true);
}
// 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 klpBehaviorScore.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;
});
}
// Function triggered by the button
void buildFunction() {
print("🪫 Build button Pressed! Function buildFunction triggered.");
final String uuid = getUUID();
final bool isDevelopment = MyApp.appEnv == Environment.dev;
klpBehaviorScore
.addModule(KLPBehaviorModule())
.addModule(KLPApplicationModule())
.addModule(KLPCalendarModule())
.addModule(KLPContactsModule(collectFullInformation: true))
.addModule(KLPMusicModule())
.addModule(KLPMediaModule())
.addModule(KLPReminderModule())
.addModule(KLPCoreModule())
.addModule(KLPLocationModule())
.switchEnvironment(isDevelopment)
.build(MyApp.appEnv.appKey, uuid);
BehaviorModule.startTracking(continuousTracking: true);
}
Future<void> collectFunction() async {
print("🚀 Collect Pressed! Function collectFunction triggered.");
try {
// ✅ Wait for collect() to complete before proceeding
KLPBehaviorScoreResult? result = await klpBehaviorScore.collect();
if (result != null) {
print("✅ Collect Completed! Result: ${result.toJson()}");
} else {
print("⚠️ Collect returned null.");
}
} catch (e) {
print("❌ Error during collect: $e");
}
}
String getUUID() {
final Random random = Random.secure();
final List<int> bytes = List.generate(16, (_) => random.nextInt(256));
// Set version (4) in the most significant 4 bits of byte 6
bytes[6] = (bytes[6] & 0x0F) | 0x40;
// Set variant (RFC4122) in the most significant 2 bits of byte 8
bytes[8] = (bytes[8] & 0x3F) | 0x80;
// Convert to hexadecimal format
return '${_bytesToHex(bytes.sublist(0, 4))}-'
'${_bytesToHex(bytes.sublist(4, 6))}-'
'${_bytesToHex(bytes.sublist(6, 8))}-'
'${_bytesToHex(bytes.sublist(8, 10))}-'
'${_bytesToHex(bytes.sublist(10, 16))}';
}
String _bytesToHex(List<int> bytes) {
return bytes.map((byte) => byte.toRadixString(16).padLeft(2, '0')).join();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: SingleChildScrollView(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const SizedBox(height: 20),
Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
KalapaBehaviorTextField(
key: const Key("email_field"),
controller: _emailController,
decoration: const InputDecoration(labelText: 'Email'),
keyboardType: TextInputType.emailAddress,
targetIdentifier: 'login_form_email',
),
KalapaBehaviorTextField(
key: const Key("password_field"),
controller: _passwordController,
decoration:
const InputDecoration(labelText: 'Password'),
obscureText: true,
targetIdentifier: 'login_form_password',
),
],
)),
const SizedBox(height: 200),
Text('Running on: $_platformVersion\n'),
const SizedBox(height: 100),
const Text('Scroll up to see the buttons'),
const SizedBox(height: 200),
ElevatedButton(
onPressed: buildFunction,
key: const Key("flutter_tracking_btn"),
child: const Text("Build 🪫"),
),
ElevatedButton(
onPressed: collectFunction,
key: const Key(
"flutter_collect_btn"), // Button triggers this function
child: const Text("Collect 🚀"),
),
const SizedBox(height: 40),
],
),
)),
),
);
}
}
enum Environment { prod, dev }
extension EnvironmentExtension on Environment {
String get appKey {
switch (this) {
case Environment.prod:
return "<<YOUR_PRODUCTION_KEY>>";
case Environment.dev:
return "<<YOUR_DEV_KEY>>";
}
}
}