flutter_kalapa_bsdk 1.0.6 copy "flutter_kalapa_bsdk: ^1.0.6" to clipboard
flutter_kalapa_bsdk: ^1.0.6 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';

void main() {
  runApp(const 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();

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  // 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(KLPApplicationModule()).addModule(KLPBehaviorModule()).addModule(KLPCalendarModule()).addModule(KLPContactsModule(collectFullInformation: true)).addModule(KLPMusicModule()).addModule(KLPMediaModule()).addModule(KLPReminderModule()).addModule(KLPCoreModule()).addModule(KLPLocationModule()).switchEnvironment(isDevelopment).build(MyApp.appEnv.appKey, uuid);

    klpBehaviorScore.startTracking(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");
    }

    // ✅ Stop tracking only after collect() is done
    klpBehaviorScore.stopTracking();
  }

  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: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text('Running on: $_platformVersion\n'),
              const SizedBox(height: 20), // Spacing between text and button
              ElevatedButton(
                onPressed: buildFunction,
                key: Key("flutter_tracking_btn"),
                child: const Text("Build 🪫"),
              ),
              ElevatedButton(
                onPressed: collectFunction,
                key: Key("flutter_collect_btn"), // Button triggers this function
                child: const Text("Collect 🚀"),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

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>>";
    }
  }
}
2
likes
0
points
33
downloads

Publisher

unverified uploader

Weekly Downloads

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 seamless API for integrating digital footprint collection into Flutter applications, ensuring cross-platform compatibility.

Homepage

License

unknown (license)

Dependencies

flutter, plugin_platform_interface

More

Packages that depend on flutter_kalapa_bsdk

Packages that implement flutter_kalapa_bsdk