import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_svg/flutter_svg.dart'; import 'package:go_router/go_router.dart'; import '../../../../core/theme/app_colors.dart'; import '../../data/providers/auth_provider.dart'; class LoginPage extends ConsumerStatefulWidget { const LoginPage({super.key}); @override ConsumerState createState() => _LoginPageState(); } class _LoginPageState extends ConsumerState { final _formKey = GlobalKey(); final _emailController = TextEditingController(); final _passwordController = TextEditingController(); @override Widget build(BuildContext context) { final authState = ref.watch(authStateProvider); return Scaffold( backgroundColor: AppColors.background, body: Center( child: Padding( padding: const EdgeInsets.all(32), child: Form( key: _formKey, child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ SvgPicture.asset( 'assets/icons/logo.svg', width: 100, height: 100, ), const SizedBox(height: 8), const Text( 'Operations Intelligent Agent', style: TextStyle(color: AppColors.textSecondary), ), const SizedBox(height: 48), TextFormField( controller: _emailController, keyboardType: TextInputType.emailAddress, decoration: const InputDecoration( labelText: 'Email', prefixIcon: Icon(Icons.email), ), validator: (value) { if (value == null || value.isEmpty) { return 'Please enter your email'; } if (!value.contains('@')) { return 'Please enter a valid email'; } return null; }, ), const SizedBox(height: 16), TextFormField( controller: _passwordController, obscureText: true, decoration: const InputDecoration( labelText: 'Password', prefixIcon: Icon(Icons.lock), ), validator: (value) { if (value == null || value.isEmpty) { return 'Please enter your password'; } return null; }, ), if (authState.error != null) ...[ const SizedBox(height: 16), Text( authState.error!, style: const TextStyle(color: Colors.red), ), ], const SizedBox(height: 24), SizedBox( width: double.infinity, child: FilledButton( onPressed: authState.isLoading ? null : _handleLogin, child: authState.isLoading ? const SizedBox( height: 20, width: 20, child: CircularProgressIndicator( strokeWidth: 2, color: Colors.white, ), ) : const Text('Login'), ), ), ], ), ), ), ), ); } Future _handleLogin() async { if (!_formKey.currentState!.validate()) return; final success = await ref.read(authStateProvider.notifier).login( _emailController.text.trim(), _passwordController.text, ); if (success && mounted) { context.go('/dashboard'); } } @override void dispose() { _emailController.dispose(); _passwordController.dispose(); super.dispose(); } }