185 lines
5.3 KiB
Dart
185 lines
5.3 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter/foundation.dart' show defaultTargetPlatform, kIsWeb;
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
import '../services/api_service.dart';
|
|
import '../entities/auth_token.dart';
|
|
import 'dashboard_page.dart';
|
|
|
|
class LoginPage extends StatefulWidget {
|
|
const LoginPage({super.key});
|
|
|
|
@override
|
|
State<LoginPage> createState() => _LoginPageState();
|
|
}
|
|
|
|
class _LoginPageState extends State<LoginPage> {
|
|
final _emailController = TextEditingController();
|
|
final _passwordController = TextEditingController();
|
|
bool _loading = false;
|
|
bool _rememberMe = false;
|
|
String? _error;
|
|
|
|
bool get isMobile {
|
|
final platform = defaultTargetPlatform;
|
|
return platform == TargetPlatform.android ||
|
|
platform == TargetPlatform.iOS ||
|
|
(kIsWeb && MediaQuery.of(context).size.width < 600);
|
|
}
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_checkRememberedUser();
|
|
}
|
|
|
|
Future<void> _checkRememberedUser() async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
final token = prefs.getString('token');
|
|
if (token != null && prefs.getBool('rememberMe') == true) {
|
|
AuthToken.instance.saveToken(token);
|
|
if (mounted) {
|
|
Navigator.pushReplacement(
|
|
context,
|
|
MaterialPageRoute(builder: (_) => const DashboardPage()),
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
Future<void> _login() async {
|
|
setState(() {
|
|
_loading = true;
|
|
_error = null;
|
|
});
|
|
|
|
try {
|
|
final token = await ApiService.login(
|
|
_emailController.text.trim(),
|
|
_passwordController.text,
|
|
);
|
|
await AuthToken.instance.saveToken(token);
|
|
final prefs = await SharedPreferences.getInstance();
|
|
if (_rememberMe) {
|
|
await prefs.setString('token', token);
|
|
await prefs.setBool('rememberMe', true);
|
|
} else {
|
|
await prefs.remove('token');
|
|
await prefs.setBool('rememberMe', false);
|
|
}
|
|
|
|
if (!mounted) return;
|
|
Navigator.pushReplacement(
|
|
context,
|
|
MaterialPageRoute(builder: (_) => const DashboardPage()),
|
|
);
|
|
} catch (e) {
|
|
setState(() {
|
|
_error = e.toString();
|
|
_loading = false;
|
|
});
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final content = Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
const Icon(Icons.eco, size: 64, color: Color(0xFF388E3C)),
|
|
const SizedBox(height: 12),
|
|
const Text(
|
|
'Gestione Vigna',
|
|
style: TextStyle(
|
|
fontSize: 26,
|
|
fontWeight: FontWeight.bold,
|
|
color: Color(0xFF2E7D32)),
|
|
),
|
|
const SizedBox(height: 24),
|
|
TextField(
|
|
controller: _emailController,
|
|
decoration: const InputDecoration(
|
|
labelText: 'Email o Username',
|
|
border: OutlineInputBorder(),
|
|
prefixIcon: Icon(Icons.person),
|
|
),
|
|
),
|
|
const SizedBox(height: 16),
|
|
TextField(
|
|
controller: _passwordController,
|
|
obscureText: true,
|
|
decoration: const InputDecoration(
|
|
labelText: 'Password',
|
|
border: OutlineInputBorder(),
|
|
prefixIcon: Icon(Icons.lock),
|
|
),
|
|
),
|
|
const SizedBox(height: 12),
|
|
Row(
|
|
children: [
|
|
Checkbox(
|
|
value: _rememberMe,
|
|
activeColor: const Color(0xFF43A047),
|
|
onChanged: (value) {
|
|
setState(() {
|
|
_rememberMe = value ?? false;
|
|
});
|
|
},
|
|
),
|
|
const Text("Ricorda accesso"),
|
|
],
|
|
),
|
|
if (_error != null)
|
|
Text(
|
|
_error!,
|
|
style: const TextStyle(color: Colors.red),
|
|
),
|
|
const SizedBox(height: 10),
|
|
_loading
|
|
? const CircularProgressIndicator()
|
|
: SizedBox(
|
|
width: double.infinity,
|
|
child: ElevatedButton.icon(
|
|
icon: const Icon(Icons.login),
|
|
style: ElevatedButton.styleFrom(
|
|
backgroundColor: const Color(0xFF43A047),
|
|
padding: const EdgeInsets.symmetric(vertical: 14),
|
|
shape: RoundedRectangleBorder(
|
|
borderRadius: BorderRadius.circular(8)),
|
|
),
|
|
label: const Text('Login', style: TextStyle(fontSize: 18)),
|
|
onPressed: _login,
|
|
),
|
|
),
|
|
],
|
|
);
|
|
|
|
return Scaffold(
|
|
backgroundColor: Colors.white,
|
|
body: LayoutBuilder(
|
|
builder: (context, constraints) {
|
|
return Center(
|
|
child: SingleChildScrollView(
|
|
padding: const EdgeInsets.all(24),
|
|
child: ConstrainedBox(
|
|
constraints: BoxConstraints(
|
|
maxWidth: isMobile ? double.infinity : 400,
|
|
),
|
|
child: Card(
|
|
elevation: isMobile ? 0 : 8,
|
|
shape: RoundedRectangleBorder(
|
|
borderRadius: BorderRadius.circular(16)),
|
|
child: Padding(
|
|
padding: const EdgeInsets.symmetric(
|
|
horizontal: 24, vertical: 32),
|
|
child: content,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
},
|
|
),
|
|
);
|
|
}
|
|
}
|