import 'package:flutter/material.dart'; import '../models/school.dart'; import '../services/auth_storage.dart'; import '../widgets/yogibook_background.dart'; class SettingsPage extends StatefulWidget { final String token; final School school; final String? userFirstName; const SettingsPage({ super.key, required this.token, required this.school, this.userFirstName, }); @override State createState() => _SettingsPageState(); } class _SettingsPageState extends State { static const Color kGreen = Color(0xFF10B981); final _auth = AuthStorage.instance; bool _loading = true; bool _rememberMe = false; bool _biometric = false; bool _biometricAvailableOnDevice = false; // evita doppi tap durante il salvataggio bool _busy = false; @override void initState() { super.initState(); _load(); } Future _load() async { final remember = await _auth.isRememberMeEnabled(); final bio = await _auth.isBiometricEnabled(); final canBio = await _auth.canUseBiometrics(); if (!mounted) return; setState(() { _rememberMe = remember; _biometric = bio; _biometricAvailableOnDevice = canBio; _loading = false; }); } void _snack(String msg) { if (!mounted) return; ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(msg))); } // "Ricordami": se acceso salva SUBITO il token corrente in cassaforte, // così funziona già da questa sessione (non solo dal prossimo login). Future _onRememberChanged(bool value) async { if (_busy) return; setState(() => _busy = true); try { await _auth.setRememberMe(value); if (value) { // salva il token che questa pagina ha in mano await _auth.saveTokenIfRemember(widget.token); } // spegnere ricordami spegne anche la biometria (dipendenza) final bioNow = await _auth.isBiometricEnabled(); if (!mounted) return; setState(() { _rememberMe = value; _biometric = bioNow; }); _snack( value ? 'Accesso rapido attivato.' : 'Accesso rapido disattivato.', ); } finally { if (mounted) setState(() => _busy = false); } } Future _onBiometricChanged(bool value) async { if (_busy) return; // non attivabile senza impronte registrate / hardware if (value && !_biometricAvailableOnDevice) { _snack( 'Nessuna impronta o volto registrati sul dispositivo. ' 'Configurali nelle impostazioni del telefono, poi riprova.', ); return; } setState(() => _busy = true); try { await _auth.setBiometric(value); if (!mounted) return; setState(() => _biometric = value); _snack( value ? 'Sblocco biometrico attivato.' : 'Sblocco biometrico disattivato.', ); } finally { if (mounted) setState(() => _busy = false); } } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( backgroundColor: Colors.transparent, elevation: 0, centerTitle: true, title: const Text( 'Impostazioni', style: TextStyle(fontWeight: FontWeight.w800, fontSize: 18), ), ), body: YogibookBackground( child: SafeArea( top: false, child: _loading ? const Center(child: CircularProgressIndicator()) : ListView( padding: const EdgeInsets.fromLTRB(16, 8, 16, 24), children: [ _sectionTitle('Accesso'), const SizedBox(height: 10), _accessCard(), const SizedBox(height: 16), _infoNote(), ], ), ), ), ); } Widget _sectionTitle(String t) => Text( t, style: const TextStyle(fontSize: 15, fontWeight: FontWeight.w900), ); Widget _accessCard() { // la biometria è attivabile solo se "ricordami" è ON final biometricEnabled = _rememberMe; return Container( padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 2), decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.circular(18), boxShadow: const [ BoxShadow( blurRadius: 18, color: Color(0x12000000), offset: Offset(0, 10), ), ], ), child: Column( children: [ SwitchListTile( activeColor: kGreen, value: _rememberMe, onChanged: _busy ? null : _onRememberChanged, secondary: const Icon(Icons.vpn_key_rounded), title: const Text( 'Ricordami', style: TextStyle(fontWeight: FontWeight.w800), ), subtitle: const Text( 'Resta connesso su questo dispositivo senza reinserire la password.', ), ), const Divider(height: 1), SwitchListTile( activeColor: kGreen, value: _biometric && biometricEnabled, onChanged: (_busy || !biometricEnabled) ? null : _onBiometricChanged, secondary: Icon( Icons.fingerprint_rounded, color: biometricEnabled ? null : Colors.black26, ), title: Text( 'Sblocco con impronta / volto', style: TextStyle( fontWeight: FontWeight.w800, color: biometricEnabled ? null : Colors.black38, ), ), subtitle: Text( biometricEnabled ? 'Chiedi la biometria all\'avvio per accedere.' : 'Attiva prima "Ricordami" per usare lo sblocco biometrico.', style: TextStyle(color: biometricEnabled ? null : Colors.black38), ), ), ], ), ); } Widget _infoNote() { return Container( padding: const EdgeInsets.all(14), decoration: BoxDecoration( color: const Color(0xFFF6F6FB), borderRadius: BorderRadius.circular(14), ), child: Row( crossAxisAlignment: CrossAxisAlignment.start, children: [ const Icon(Icons.info_outline, size: 20, color: Colors.black45), const SizedBox(width: 10), Expanded( child: Text( _biometricAvailableOnDevice ? 'Il token di accesso è salvato in forma cifrata sul dispositivo. ' 'Con lo sblocco biometrico attivo, viene usato solo dopo il riconoscimento.' : 'Questo dispositivo non ha impronta o volto configurati: ' 'lo sblocco biometrico non è disponibile, ma "Ricordami" funziona lo stesso.', style: const TextStyle(fontSize: 12, color: Colors.black54), ), ), ], ), ); } }