124 lines
4.3 KiB
Dart
124 lines
4.3 KiB
Dart
import 'dart:convert';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:odoo_rpc/odoo_rpc.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
import '../services/odoo_service.dart';
|
|
import 'loyalty_dashboard.dart';
|
|
import '../theme/app_theme.dart';
|
|
|
|
class LoginScreen extends StatefulWidget {
|
|
const LoginScreen({super.key});
|
|
|
|
@override
|
|
State<LoginScreen> createState() => _LoginScreenState();
|
|
}
|
|
|
|
class _LoginScreenState extends State<LoginScreen> {
|
|
final _usernameController = TextEditingController();
|
|
final _passwordController = TextEditingController();
|
|
bool _isLoading = false;
|
|
|
|
final String _odooUrl = 'https://erp.mapan.co.id';
|
|
final String _odooDb = 'mapangroup_o19';
|
|
|
|
void _login() async {
|
|
setState(() => _isLoading = true);
|
|
try {
|
|
final service = OdooService();
|
|
service.connect(_odooUrl);
|
|
final session = await service.login(
|
|
_odooDb,
|
|
_usernameController.text.trim(),
|
|
_passwordController.text.trim(),
|
|
);
|
|
|
|
final prefs = await SharedPreferences.getInstance();
|
|
await prefs.setString('odoo_url', _odooUrl);
|
|
final sessionJson = json.encode({
|
|
'id': session.id,
|
|
'user_id': session.userId,
|
|
'partner_id': session.partnerId,
|
|
'user_login': session.userLogin,
|
|
'user_name': session.userName,
|
|
'user_lang': session.userLang,
|
|
'user_tz': session.userTz,
|
|
'is_system': session.isSystem,
|
|
'server_version': session.serverVersion,
|
|
});
|
|
await prefs.setString('odoo_session', sessionJson);
|
|
|
|
if (mounted) {
|
|
Navigator.pushReplacement(
|
|
context,
|
|
MaterialPageRoute(builder: (_) => LoyaltyDashboard(partnerId: session.partnerId)),
|
|
);
|
|
}
|
|
} on OdooException catch (e) {
|
|
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('Login failed: ${e.message}')));
|
|
} catch (e) {
|
|
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('Error: $e')));
|
|
} finally {
|
|
if (mounted) setState(() => _isLoading = false);
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
body: SafeArea(
|
|
child: SingleChildScrollView(
|
|
padding: const EdgeInsets.symmetric(horizontal: 24.0, vertical: 48.0),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
const SizedBox(height: 64),
|
|
Text(
|
|
'Mie Mapan\nMembresia', // Editorial high-end entry
|
|
style: Theme.of(context).textTheme.displayMedium?.copyWith(
|
|
color: AppTheme.primary,
|
|
),
|
|
),
|
|
const SizedBox(height: 12),
|
|
Text(
|
|
'Sign in to access your culinary loyalty tier and discover exclusive offers.',
|
|
style: Theme.of(context).textTheme.bodyLarge,
|
|
),
|
|
const SizedBox(height: 56),
|
|
TextField(
|
|
controller: _usernameController,
|
|
decoration: const InputDecoration(labelText: 'Email / Username'),
|
|
),
|
|
const SizedBox(height: 20),
|
|
TextField(
|
|
controller: _passwordController,
|
|
decoration: const InputDecoration(labelText: 'Password'),
|
|
obscureText: true,
|
|
),
|
|
const SizedBox(height: 48),
|
|
SizedBox(
|
|
height: 56,
|
|
child: _isLoading
|
|
? const Center(child: CircularProgressIndicator())
|
|
: Container(
|
|
decoration: BoxDecoration(
|
|
gradient: AppTheme.primaryGradient,
|
|
borderRadius: BorderRadius.circular(12),
|
|
),
|
|
child: ElevatedButton(
|
|
onPressed: _login,
|
|
style: ElevatedButton.styleFrom(
|
|
backgroundColor: Colors.transparent,
|
|
shadowColor: Colors.transparent,
|
|
),
|
|
child: const Text('Access Membership', style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold)),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|