ironchip_lbfraud 2.0.13 ironchip_lbfraud: ^2.0.13 copied to clipboard
Flutter plugin designed for Location Fraud Detection, powered by the analysis of environmental signals to detect and prevent fraudulent activities effectively.
import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:math';
import 'package:ironchip_lbfraud/ironchip_lbfraud.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 _lbfraudPlugin = IronchipLbfraud();
String apiKey =
"APIKEY"; // Replace APIKEY with the desired generated api key.
bool _isLbfraudInitialized = false;
Environment selectedEnv = Environment.production;
String userID = "john.doe@gmail.com"; // User identifier
String _showResult = "";
bool _resultError = false;
@override
void initState() {
super.initState();
}
// LBFraud methods are asynchronous, so we initialize in an async method.
Future<void> initLBFraudState() async {
try {
// Replace APIKEY with the desired generated api key.
// By default our SDK target to the production environment.
// In case you desire to target a diferent enviroment:
// LBFraudSDK fraud = new LbfraudFlutterPlugin.initFraudSDK("APIKEY", env: Environment.testing);
await _lbfraudPlugin.initFraudSDK(apiKey, env: selectedEnv);
_isLbfraudInitialized = true;
} catch (e) {
_isLbfraudInitialized = false;
}
setState(() {
_isLbfraudInitialized = _isLbfraudInitialized;
});
}
String generateRandomTransactionID(int length) {
const chars =
'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz1234567890';
Random rnd = Random();
return String.fromCharCodes(Iterable.generate(
length, (_) => chars.codeUnitAt(rnd.nextInt(chars.length))));
}
Future<void> sendTransaction() async {
try {
// TransactionID (required,unique): transaction identifier request for fraud results
// UserID (required): User identifier
// extraData (optional): extra information for analysis
// You can omit the await in case the result of the action isn't necessary
var result = await _lbfraudPlugin
.sendTransaction(generateRandomTransactionID(10), userID, {});
_showResult = result!;
_resultError = false;
} catch (e) {
_showResult = e.toString();
_resultError = true;
}
setState(() {
_resultError = _resultError;
_showResult = _showResult;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Ironchip LBFraud example app'),
backgroundColor: const Color(0xFF005255),
),
body: Container(
margin: const EdgeInsets.all(40.0),
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
TextField(
onChanged: (value) {
apiKey = value;
},
decoration: const InputDecoration(labelText: "API Key"),
),
const SizedBox(height: 20),
DropdownButtonExample(
onChange: (Environment env) {
selectedEnv = env;
},
),
const SizedBox(height: 20),
Text(
_isLbfraudInitialized
? "LBFraud initialized"
: "LBFraud is not initialized",
style: TextStyle(
color: _isLbfraudInitialized
? const Color(0xFF95C11F)
: const Color(0xFFF44336),
),
),
const SizedBox(height: 20),
ElevatedButton(
onPressed: initLBFraudState,
style: ElevatedButton.styleFrom(
backgroundColor:
const Color(0xFF95C11F), // Set button color to #4eed56
),
child: const Text('Initialize LBFraud'),
),
TextField(
onChanged: (value) {
userID = value;
},
decoration: const InputDecoration(labelText: "User ID"),
),
const SizedBox(height: 20),
ElevatedButton(
onPressed: _isLbfraudInitialized ? sendTransaction : null,
style: ElevatedButton.styleFrom(
backgroundColor:
const Color(0xFF95C11F), // Set button color to #4eed56
),
child: const Text('Send Transaction'),
),
const SizedBox(height: 20),
Text(
_showResult,
style: TextStyle(
color: _resultError ? const Color(0xFFF44336) : Colors.black,
),
),
],
),
),
),
));
}
}
const List<String> list = <String>['Production', 'Testing', 'Development'];
class DropdownButtonExample extends StatefulWidget {
const DropdownButtonExample({super.key, this.onChange});
final void Function(Environment)? onChange;
@override
State<DropdownButtonExample> createState() => _DropdownButtonExampleState();
}
class _DropdownButtonExampleState extends State<DropdownButtonExample> {
String dropdownValue = list.first;
@override
Widget build(BuildContext context) {
return DropdownButton<String>(
value: dropdownValue,
icon: const Icon(Icons.arrow_downward),
elevation: 16,
style: const TextStyle(color: Color(0xFF95C11F)),
underline: Container(
height: 2,
color: const Color(0xFF95C11F),
),
onChanged: (String? value) {
// This is called when the user selects an item.
if (widget.onChange != null) {
switch (value) {
case 'Production':
widget.onChange!(Environment.production);
break;
case 'Testing':
widget.onChange!(Environment.testing);
break;
case 'Development':
widget.onChange!(Environment.develoment);
break;
}
}
setState(() {
dropdownValue = value!;
});
},
items: list.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
);
}
}