ironchip_lbfraud 2.0.16 ironchip_lbfraud: ^2.0.16 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/foundation.dart';
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.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();
}
Future _determinePosition() async {
bool serviceEnabled;
LocationPermission permission;
// Test if location services are enabled.
serviceEnabled = await Geolocator.isLocationServiceEnabled();
if (!serviceEnabled) {
// Location services are not enabled don't continue
// accessing the position and request users of the
// App to enable the location services.
return Future.error('Location services are disabled.');
}
permission = await Geolocator.checkPermission();
if (permission == LocationPermission.denied) {
permission = await Geolocator.requestPermission();
if (permission == LocationPermission.denied) {
// Permissions are denied, next time you could try
// requesting permissions again (this is also where
// Android's shouldShowRequestPermissionRationale
// returned true. According to Android guidelines
// your App should show an explanatory UI now.
return Future.error('Location permissions are denied');
}
}
if (permission == LocationPermission.deniedForever) {
// Permissions are denied forever, handle appropriately.
return Future.error(
'Location permissions are permanently denied, we cannot request permissions.');
}
// When we reach here, permissions are granted and we can
// continue accessing the position of the device.
var pos = await Geolocator.getCurrentPosition();
if (kDebugMode) {
print("lat: ${pos.latitude}, long: ${pos.longitude}");
}
}
// 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);
// If your device is configured with the proxy, set the configuration to the plugin:
// await _lbfraudPlugin.setProxy("xxx.xxx.xxx.xxx", 8080);
_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, <String, Object>{});
_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: Center(
child: SingleChildScrollView(
child: Container(
margin: const EdgeInsets.all(40.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ElevatedButton(
onPressed: _determinePosition,
style: ElevatedButton.styleFrom(
backgroundColor: const Color(
0xFF95C11F), // Set button color to #4eed56
),
child: const Text('Request Location Permission'),
),
const SizedBox(height: 20),
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(),
);
}
}