odoo-mobile-app/lib/screens/login_screen.dart

163 lines
6.4 KiB
Dart

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../services/odoo_service.dart';
import '../utils/theme.dart';
import '../utils/constants.dart';
class LoginScreen extends StatefulWidget {
const LoginScreen({super.key});
@override
State<LoginScreen> createState() => _LoginScreenState();
}
class _LoginScreenState extends State<LoginScreen> {
final _formKey = GlobalKey<FormState>();
final _urlController = TextEditingController(text: AppConstants.defaultOdooUrl);
final _dbController = TextEditingController(text: AppConstants.dbName);
final _emailController = TextEditingController(text: 'alaguraj0361@gmail.com');
final _passwordController = TextEditingController(text: 'Alaguraj@123');
bool _isLoading = false;
void _login() async {
if (_formKey.currentState!.validate()) {
setState(() { _isLoading = true; });
try {
final odoo = Provider.of<OdooService>(context, listen: false);
odoo.updateUrl(_urlController.text);
await odoo.authenticate(
_dbController.text,
_emailController.text,
_passwordController.text,
);
// Navigation handled by auth state listener in main
} catch (e) {
if (!mounted) return;
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Login failed: $e'),
backgroundColor: AppTheme.secondaryColor,
),
);
} finally {
if (mounted) setState(() { _isLoading = false; });
}
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
decoration: const BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [Color(0xFF8fe0d6), Color(0xFFffecb5)], // --bg-gradient-main
),
),
child: Center(
child: SingleChildScrollView(
padding: const EdgeInsets.all(24),
child: Card(
elevation: 8,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20)),
child: Padding(
padding: const EdgeInsets.all(32),
child: Form(
key: _formKey,
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
const Text(
'Odoo Connect',
style: TextStyle(
fontSize: 28,
fontWeight: FontWeight.bold,
color: AppTheme.secondaryColor,
),
),
const SizedBox(height: 10),
Text(
'Welcome Back',
style: TextStyle(
fontSize: 16,
color: Colors.grey[600],
),
),
const SizedBox(height: 30),
TextFormField(
controller: _urlController,
decoration: const InputDecoration(
labelText: 'Server URL',
prefixIcon: Icon(Icons.link, color: AppTheme.secondaryColor),
),
validator: (v) => v!.isEmpty ? 'Enter URL' : null,
),
const SizedBox(height: 16),
TextFormField(
controller: _dbController,
decoration: const InputDecoration(
labelText: 'Database',
prefixIcon: Icon(Icons.storage, color: AppTheme.secondaryColor)
),
validator: (v) => v!.isEmpty ? 'Enter DB Name' : null,
),
const SizedBox(height: 16),
TextFormField(
controller: _emailController,
decoration: const InputDecoration(
labelText: 'Email',
prefixIcon: Icon(Icons.email, color: AppTheme.secondaryColor)
),
validator: (v) => v!.isEmpty ? 'Enter Email' : null,
),
const SizedBox(height: 16),
TextFormField(
controller: _passwordController,
decoration: const InputDecoration(
labelText: 'Password',
prefixIcon: Icon(Icons.lock, color: AppTheme.secondaryColor)
),
obscureText: true,
validator: (v) => v!.isEmpty ? 'Enter Password' : null,
),
const SizedBox(height: 32),
Container(
width: double.infinity,
height: 50,
decoration: BoxDecoration(
gradient: AppTheme.primaryGradient,
borderRadius: BorderRadius.circular(10),
boxShadow: [
BoxShadow(
color: AppTheme.primaryColor.withOpacity(0.3),
blurRadius: 12,
offset: const Offset(0, 4),
)
],
),
child: ElevatedButton(
onPressed: _isLoading ? null : _login,
style: ElevatedButton.styleFrom(
backgroundColor: Colors.transparent,
shadowColor: Colors.transparent,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
),
child: _isLoading
? const SizedBox(width: 20, height: 20, child: CircularProgressIndicator(color: Colors.white, strokeWidth: 2))
: const Text('LOGIN', style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold, color: Colors.white)),
),
),
],
),
),
),
),
),
),
),
);
}
}