import 'package:flutter/material.dart';
import 'package:alphyn_sdk/alphyn_sdk.dart';
class SecureApp extends StatefulWidget {
_SecureAppState createState() => _SecureAppState();
class _SecureAppState extends State<SecureApp> {
SecurityLevel _securityLevel = SecurityLevel.unknown;
List<String> _threats = [];
bool _isMonitoring = false;
Future<void> _initializeSecurity() async {
// Check if SDK is initialized
final isInitialized = await AlphynSdk.isInitialized();
if (isInitialized != true) {
_securityLevel = SecurityLevel.initializationError;
// Perform initial security check
await _performSecurityCheck();
// Start continuous monitoring
_startContinuousMonitoring();
print('Security initialization error: $e');
_securityLevel = SecurityLevel.initializationError;
Future<void> _performSecurityCheck() async {
// getRejectReasons() returns a newline-separated string; split it into a list
final rejectReasonsRaw = await AlphynSdk.getRejectReasons();
final rejectReasons = rejectReasonsRaw.split('\n').where((line) => line.trim().isNotEmpty).toList();
final result = await AlphynSdk.getScanResult();
_threats = rejectReasons;
_securityLevel = _determineSecurityLevel(rejectReasons, result);
_applySecurityPolicy(_securityLevel);
print('Security check error: $e');
SecurityLevel _determineSecurityLevel(List<String> threats, String? result) {
// Device is trusted if no threats detected
return SecurityLevel.trusted;
} else if (threats.length < 3) {
return SecurityLevel.flagged;
return SecurityLevel.untrusted;
void _applySecurityPolicy(SecurityLevel level) {
case SecurityLevel.trusted:
print('✅ Security Policy: Full Access');
case SecurityLevel.flagged:
print('⚠️ Security Policy: Restricted Access');
case SecurityLevel.untrusted:
print('🚨 Security Policy: Critical Restriction');
case SecurityLevel.unknown:
case SecurityLevel.initializationError:
print('❓ Security Policy: Unable to determine');
void _startContinuousMonitoring() {
if (_isMonitoring) return;
Timer.periodic(const Duration(minutes: 5), (timer) {
void _showSecurityWarning() {
builder: (context) => AlertDialog(
Icon(Icons.warning_amber, color: Colors.orange),
'Security concerns detected:\n\n${_threats.join('\n')}',
onPressed: () => Navigator.pop(context),
child: const Text('Acknowledge'),
void _showCriticalAlert() {
barrierDismissible: false,
builder: (context) => AlertDialog(
Icon(Icons.error, color: Colors.red),
Text('Critical Security Alert'),
'Critical security violations detected:\n\n${_threats.join('\n')}\n\n'
'Sensitive features have been disabled for your protection.',
onPressed: () => Navigator.pop(context),
child: const Text('I Understand'),
Widget build(BuildContext context) {
title: const Text('Secure Application'),
backgroundColor: _getSecurityColor(),
mainAxisAlignment: MainAxisAlignment.center,
color: _getSecurityColor(),
const SizedBox(height: 16),
_securityLevel.displayName,
style: Theme.of(context).textTheme.headlineMedium,
if (_threats.isNotEmpty) ...[
const SizedBox(height: 24),
'Threats detected: ${_threats.length}',
style: Theme.of(context).textTheme.bodyLarge,
Color _getSecurityColor() {
switch (_securityLevel) {
case SecurityLevel.trusted:
case SecurityLevel.flagged:
case SecurityLevel.untrusted:
IconData _getSecurityIcon() {
switch (_securityLevel) {
case SecurityLevel.trusted:
return Icons.verified_user;
case SecurityLevel.flagged:
case SecurityLevel.untrusted:
case SecurityLevel.trusted:
case SecurityLevel.flagged:
case SecurityLevel.untrusted:
case SecurityLevel.unknown:
case SecurityLevel.initializationError:
return 'Initialization Error';