flutter_app_security 1.0.0
flutter_app_security: ^1.0.0 copied to clipboard
A comprehensive Flutter security module providing multiple layers of protection against common mobile app attacks including root/jailbreak detection, emulator detection, tampering detection, SSL pinni [...]
import 'package:flutter/material.dart';
import 'package:flutter_app_security/flutter_app_security.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// Initialize security with production config
await AppSecurity.instance.initialize(
SecurityConfig.production(
onThreatDetected: (result) {
debugPrint('Security threat detected: ${result.detectedThreats}');
},
),
);
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter App Security Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue),
useMaterial3: true,
),
home: const SecurityDemoPage(),
);
}
}
class SecurityDemoPage extends StatefulWidget {
const SecurityDemoPage({super.key});
@override
State<SecurityDemoPage> createState() => _SecurityDemoPageState();
}
class _SecurityDemoPageState extends State<SecurityDemoPage> {
SecurityCheckResult? _result;
bool _isChecking = false;
bool _screenProtectionEnabled = false;
Map<String, dynamic>? _securityInfo;
@override
void initState() {
super.initState();
_runSecurityCheck();
}
Future<void> _runSecurityCheck() async {
setState(() => _isChecking = true);
final result = await AppSecurity.instance.runSecurityChecks();
final info = await AppSecurity.instance.getSecurityInfo();
setState(() {
_result = result;
_securityInfo = info;
_isChecking = false;
});
}
Future<void> _toggleScreenProtection() async {
if (_screenProtectionEnabled) {
await AppSecurity.instance.disableScreenSecurity();
} else {
await AppSecurity.instance.enableScreenSecurity();
}
setState(() {
_screenProtectionEnabled = !_screenProtectionEnabled;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Security Demo'),
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
),
body: SingleChildScrollView(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
// Security Status Card
Card(
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Icon(
_result?.isSecure ?? true
? Icons.security
: Icons.warning,
color: _result?.isSecure ?? true
? Colors.green
: Colors.red,
size: 32,
),
const SizedBox(width: 12),
Text(
_result?.isSecure ?? true
? 'Device Secure'
: 'Threats Detected',
style: Theme.of(context).textTheme.headlineSmall,
),
],
),
const SizedBox(height: 16),
if (_isChecking)
const Center(child: CircularProgressIndicator())
else if (_result != null) ...[
if (_result!.detectedThreats.isEmpty)
const Text(
'No security threats detected.',
style: TextStyle(color: Colors.green),
)
else
...(_result!.detectedThreats.map((threat) => Padding(
padding: const EdgeInsets.symmetric(vertical: 4),
child: Row(
children: [
const Icon(Icons.error,
color: Colors.red, size: 20),
const SizedBox(width: 8),
Expanded(
child: Text(
SecurityCheckResult.getThreatDescription(
threat),
),
),
],
),
))),
],
],
),
),
),
const SizedBox(height: 16),
// Actions Card
Card(
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Actions',
style: Theme.of(context).textTheme.titleMedium,
),
const SizedBox(height: 16),
ElevatedButton.icon(
onPressed: _isChecking ? null : _runSecurityCheck,
icon: const Icon(Icons.refresh),
label: const Text('Run Security Check'),
),
const SizedBox(height: 8),
ElevatedButton.icon(
onPressed: _toggleScreenProtection,
icon: Icon(_screenProtectionEnabled
? Icons.visibility_off
: Icons.visibility),
label: Text(_screenProtectionEnabled
? 'Disable Screen Protection'
: 'Enable Screen Protection'),
),
],
),
),
),
const SizedBox(height: 16),
// Device Info Card
if (_securityInfo != null)
Card(
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Device Info',
style: Theme.of(context).textTheme.titleMedium,
),
const SizedBox(height: 12),
if (_securityInfo!['device'] != null) ...[
_infoRow('Platform',
_securityInfo!['device']['platform'] ?? 'Unknown'),
_infoRow('Model',
_securityInfo!['device']['model'] ?? 'Unknown'),
_infoRow(
'Physical Device',
(_securityInfo!['device']['isPhysicalDevice'] ??
false)
.toString()),
],
],
),
),
),
const SizedBox(height: 16),
// Configuration Card
Card(
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Active Configuration',
style: Theme.of(context).textTheme.titleMedium,
),
const SizedBox(height: 12),
if (_securityInfo?['config'] != null) ...[
_configRow('Root/Jailbreak Check',
_securityInfo!['config']['checkRootJailbreak']),
_configRow('Emulator Check',
_securityInfo!['config']['checkEmulator']),
_configRow('Debug Mode Check',
_securityInfo!['config']['checkDebugMode']),
_configRow('Tampering Check',
_securityInfo!['config']['checkTampering']),
_configRow('Screenshot Prevention',
_securityInfo!['config']['preventScreenshot']),
],
],
),
),
),
],
),
),
);
}
Widget _infoRow(String label, String value) {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 4),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(label, style: const TextStyle(color: Colors.grey)),
Text(value, style: const TextStyle(fontWeight: FontWeight.w500)),
],
),
);
}
Widget _configRow(String label, bool? value) {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 4),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(label),
Icon(
value == true ? Icons.check_circle : Icons.cancel,
color: value == true ? Colors.green : Colors.grey,
size: 20,
),
],
),
);
}
}