This commit is contained in:
2026-03-20 20:50:26 +03:00
parent 50a34bf277
commit e2c6e38560
10 changed files with 213 additions and 3 deletions
+37
View File
@@ -0,0 +1,37 @@
import 'package:local_auth/local_auth.dart';
import 'package:shared_preferences/shared_preferences.dart';
class BiometricService {
static final _auth = LocalAuthentication();
static const _key = 'biometric_enabled';
static Future<bool> isAvailable() async {
final canCheck = await _auth.canCheckBiometrics;
final isSupported = await _auth.isDeviceSupported();
return canCheck && isSupported;
}
static Future<bool> isEnabled() async {
final prefs = await SharedPreferences.getInstance();
return prefs.getBool(_key) ?? false;
}
static Future<void> setEnabled(bool value) async {
final prefs = await SharedPreferences.getInstance();
await prefs.setBool(_key, value);
}
static Future<bool> authenticate() async {
try {
return await _auth.authenticate(
localizedReason: 'Confirm your identity to open Casha',
options: const AuthenticationOptions(
biometricOnly: false,
stickyAuth: true,
),
);
} catch (_) {
return false;
}
}
}
+106
View File
@@ -4,6 +4,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import 'package:intl/intl.dart';
import '../../core/constants.dart';
import '../../core/services/biometric_service.dart';
import '../../shared/utils/currency_utils.dart';
import '../../shared/providers/amount_format_provider.dart';
import '../dashboard/provider.dart';
@@ -387,6 +388,8 @@ class _SettingsScreenState extends ConsumerState<SettingsScreen> {
),
const SizedBox(height: 16),
const _BiometricSection(),
Container(
padding: const EdgeInsets.all(20),
decoration: BoxDecoration(
@@ -543,3 +546,106 @@ class _SettingsScreenState extends ConsumerState<SettingsScreen> {
);
}
}
class _BiometricSection extends StatefulWidget {
const _BiometricSection();
@override
State<_BiometricSection> createState() => _BiometricSectionState();
}
class _BiometricSectionState extends State<_BiometricSection> {
bool _available = false;
bool _enabled = false;
bool _loading = true;
@override
void initState() {
super.initState();
_load();
}
Future<void> _load() async {
final available = await BiometricService.isAvailable();
final enabled = await BiometricService.isEnabled();
if (mounted) {
setState(() {
_available = available;
_enabled = enabled;
_loading = false;
});
}
}
Future<void> _onToggle(bool val) async {
if (val) {
final ok = await BiometricService.authenticate();
if (!ok) return;
}
await BiometricService.setEnabled(val);
if (mounted) setState(() => _enabled = val);
}
@override
Widget build(BuildContext context) {
if (_loading || !_available) return const SizedBox.shrink();
final isDark = Theme.of(context).brightness == Brightness.dark;
return Column(
children: [
Container(
padding: const EdgeInsets.all(20),
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.surface,
borderRadius: BorderRadius.circular(16),
border: isDark ? null : Border.all(color: const Color(0xFFDDDDEE), width: 1),
),
child: Row(
children: [
Container(
padding: const EdgeInsets.all(10),
decoration: BoxDecoration(
color: AppColors.accent.withOpacity(0.15),
borderRadius: BorderRadius.circular(12),
),
child: const Icon(
Icons.fingerprint,
color: AppColors.accent,
size: 20,
),
),
const SizedBox(width: 12),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Biometric Lock',
style: Theme.of(context).textTheme.titleMedium?.copyWith(
fontWeight: FontWeight.w600,
color: Theme.of(context).colorScheme.onSurface,
),
),
Text(
'Require fingerprint on app launch',
style: Theme.of(context).textTheme.bodySmall?.copyWith(
color: Theme.of(context).colorScheme.onSurface.withOpacity(0.6),
),
),
],
),
),
Switch(
value: _enabled,
onChanged: _onToggle,
activeColor: const Color(0xFF7C6DED),
),
],
),
),
const SizedBox(height: 16),
],
);
}
}
+11 -1
View File
@@ -1,6 +1,8 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:go_router/go_router.dart';
import 'package:google_fonts/google_fonts.dart';
import '../../core/services/biometric_service.dart';
class SplashScreen extends StatefulWidget {
const SplashScreen({super.key});
@@ -28,9 +30,17 @@ class _SplashScreenState extends State<SplashScreen> with SingleTickerProviderSt
_controller.forward();
_controller.addStatusListener((status) {
_controller.addStatusListener((status) async {
if (status == AnimationStatus.completed) {
if (mounted) {
final isEnabled = await BiometricService.isEnabled();
if (isEnabled) {
final authenticated = await BiometricService.authenticate();
if (!authenticated) {
SystemNavigator.pop();
return;
}
}
context.go('/dashboard');
}
}