Files
uWinePortal/lib/pages/login_page.dart
T
2025-07-11 14:10:39 +02:00

85 lines
2.2 KiB
Dart

import 'package:flutter/material.dart';
import '../services/api_service.dart';
import 'dashboard_page.dart';
import '../entities/auth_token.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;
String? _error;
void _login() async {
setState(() {
_loading = true;
_error = null;
});
final email = _emailController.text.trim();
final password = _passwordController.text;
try {
final token = await ApiService.login(email, password);
auth_token().token = token;
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Login riuscito! Token: $token')),
);
if (!mounted) return;
Navigator.of(context).pushReplacement(
MaterialPageRoute(builder: (_) => const DashboardPage()),
);
} catch (e) {
setState(() {
_error = e.toString();
});
} finally {
setState(() {
_loading = false;
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Login')),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
TextField(
controller: _emailController,
decoration: const InputDecoration(labelText: 'Email'),
),
const SizedBox(height: 12),
TextField(
controller: _passwordController,
obscureText: true,
decoration: const InputDecoration(labelText: 'Password'),
),
const SizedBox(height: 24),
if (_error != null)
Text(
_error!,
style: const TextStyle(color: Colors.red),
),
_loading
? const CircularProgressIndicator()
: ElevatedButton(
onPressed: _login,
child: const Text('Login'),
),
],
),
),
);
}
}